LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 01-27-2004, 05:56 AM   #1
Bassam
Member
 
Registered: Mar 2003
Location: Malaysia
Posts: 63

Rep: Reputation: 15
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
 
Old 01-27-2004, 06:09 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.

Last edited by druuna; 01-27-2004 at 06:10 AM.
 
Old 01-27-2004, 06:11 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 01-27-2004, 06:42 AM   #4
Bassam
Member
 
Registered: Mar 2003
Location: Malaysia
Posts: 63

Original Poster
Rep: Reputation: 15
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
 
Old 01-27-2004, 06:45 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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).
 
Old 01-27-2004, 06:49 AM   #6
Bassam
Member
 
Registered: Mar 2003
Location: Malaysia
Posts: 63

Original Poster
Rep: Reputation: 15
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 !!!!????
 
Old 01-27-2004, 06:57 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 01-27-2004, 07:04 AM   #8
Bassam
Member
 
Registered: Mar 2003
Location: Malaysia
Posts: 63

Original Poster
Rep: Reputation: 15
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
 
Old 01-27-2004, 07:19 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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..
 
Old 01-27-2004, 07:42 AM   #10
Bassam
Member
 
Registered: Mar 2003
Location: Malaysia
Posts: 63

Original Poster
Rep: Reputation: 15
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Setting environment variables from shell script theta Linux - General 5 09-02-2004 08:50 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM
Sub shell environment variables tnine9 Programming 1 06-08-2004 01:27 PM
Shell Variables. ducka Programming 5 09-03-2003 05:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration