LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Why?? can not use variables with shell script (https://www.linuxquestions.org/questions/linux-general-1/why-can-not-use-variables-with-shell-script-139147/)

Bassam 01-27-2004 05:56 AM

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

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


Thanks for support

regards
Bassam

druuna 01-27-2004 06:09 AM

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).

Hope this helps.

acid_kewpie 01-27-2004 06:11 AM

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.

Bassam 01-27-2004 06:42 AM

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

druuna 01-27-2004 06:45 AM

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).

Bassam 01-27-2004 06:49 AM

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 !!!!????

druuna 01-27-2004 06:57 AM

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.

Bassam 01-27-2004 07:04 AM

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

druuna 01-27-2004 07:19 AM

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..

Bassam 01-27-2004 07:42 AM

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.