Hi,
This is most likely a dumb question but I could not find answer to it elsewhere.
I'm building a simple menu with case /esac and want to read user's input:
Please enter XYZ ; read XYZ
How do I take the value of XYZ and insert it as a variable $XYZ in file file.txt ?
I may need to explain that I have existing variable IP="address" in file.txt and I need to substitute address with XYZ from the user's input
vbe
2
What else is in file.txt?
A long script
I have
IP="address" as a variable on the 4-th line
The point is to be able to take user's input for IP address and insert it into this file so it is a valid IP to be used throughout the script
Why insert it? Why not just use it? This is a strange-sounding system.
You could export it, and set it only in that script if it's not already set.
# outside the script
read IPADDRESS
export IPADDRESS
./myscript
#inside myscript, set the variable if nothing else did
[ -z "$IPADDRESS" ] && IPADDRESS="default.ip.address"
That way you don't need to hack apart your scripts with sed all the time, which sounds a recipe for disaster if I ever heard one...
You may be right. But how do I do that?
My Main Menu script is in one file that I execute first to define the values which are then inserted and the variable in the other file
What is the alternative?
I may have stealth-edited underneath you, see my example.
vbe
7
${IPADDRESS:=ZZZ.XXX.YYY.AAA}
Will set the value of IPADDRESS to ZZZ.XXX.YYY.AAA if the variable had no value...
so that's what happens
I enter:
echo "Please enter a valid network adress (e.g 129.168.0.0/24)" ; read network;\
if [ `echo "$network" | awk '{print length($0)}'` -eq 14 ] ;\
then export $network ; ./script; \
else echo "Wrong address - use slash notation / for subnet!"; fi
and the result is:
-sh: export: 192.168.0.0/24: bad variable name
????? Confused
---------- Post updated at 12:01 PM ---------- Previous update was at 11:59 AM ----------
OK I see I should have used the ` around the export command, but then it changes my whole script file ....
vbe
9
then export $network ; ./script; \
Is wrong!!!
You export the variable NOT its value...
then export network ; ./script; \
vbe
10
Question:
Did you use the variable substitution as we have suggested? Where?