Why?? can not use variables with shell script
Hi All,
I have wrot a small shell script to read values from a file and then assgin the values to variables, and then uses these variables with another command, but I am facing a problem and getting error, could any body tell me what is the problem in my code? Code:
#!/bin/bash 0 5 230.40.12.200 Thanks for support regards Bassam |
This is a bit tricky, but you can use/mix numerical and non-numerical variables inside a (bash/ksh) script. To make sure that the shell knows how it should interpret a variable you can use eval:
Code:
#!/bin/bash ifconfig eth0:5 230.40.12.200 up I echoed the ipconfig statement to make sure it's correct before actually doing it (need to remove the echo and the 2 double quotes). Hope this helps. |
you can't assign vairables in that way... you're thinkiong variable$i will mean variable0, vairable1 in turn will be assigned? not at all possible, and when you think about it it really doens't make any sense, hoever nice it might first seem. use an array instead.
|
Hi druuna
I tested the code, the statment inside the loop is working now, but still getting error in the last statment even after removing the echo and the double quots and also I used the eval statment but still getting error. I need to lunch the ifconfig statment in order to creat an alias for my Ethernet card and not echoing it. Thanks Bassam |
First things first: The remark by acid_kewpie is correct, the way the script is set up is kinda 'ugly'. You should try using array'. He's incorrect about it not working.
What is the error that is generated by ifconfig? Are you root? (you need to be root to execute the ifconfig statement). |
Yeah I am root, and the errors that I am getting for the last statement are:
SIOCSIFADDR: Invalid argument SIOCSIFFLAGS: Cannot assign requested address SIOCSIFFLAGS: Cannot assign requested address if this case doesn't work then how to use the array as acid_kewpie has suggested !!!!???? |
The errors shown are ifconfig related and not 'array' related.
If I try the command given by you on 2 machines (suse and lfs) it fails. If I leave out the :5 part it works as designed. |
But is there different enable me to pass my variables to the ifconfig command so I can create the alias????
If I did not solve this problem this mean I will throw out my project :( :( Your support please Bassam |
Just did a little testing with the ifconfig command and found the following:
ifconfig eth0:1 1.2.3.4 up only works if eth0(:0) is already set.. |
Hi druuna,
Thanks for ur help, the code is working now. the wrong was with the IP address. I just changed from 230.40.12.200 to 192.168.8.1 insid the file. But it is really strange!!! Do u think because that the automatically extracted subnet mask from the IP 230.40.12.200 does not match the subnet mast of the original IP address of the Ethenet card? Well I don't think so. Any way I am so much happy now :) :) :) Now it is 9:41 pm and I am still in the company, I have to go back home and have my dinner hhmmmm. Take care Bassam |
All times are GMT -5. The time now is 10:39 PM. |