Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
i=0
token1=0
token2=0
token3=0
OLD_IP_FILE=/tmp/text.tmp
for token in `cat text.tmp`
do
i=`expr $i + 1`
token$i=$token #The error in this line
done
ifconfig eth$token1:$token2 $token3 up #and another error in this line because of the variables
my text.tmp file contains one line of data as follows:
0 5 230.40.12.200
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
i="0"
token1="0"
token2="0"
token3="0"
OLD_IP_FILE=/tmp/text.tmp
for token in `cat /tmp/text.tmp`
do
i=`expr $i + 1`
eval token$i=$token
done
echo "ifconfig eth$token1:$token2 $token3 up"
Running this will produce the following:
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).
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.
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).
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.