LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2006, 03:19 PM   #1
dipenchaudhary
Member
 
Registered: Jan 2006
Location: india
Distribution: FEDORA CORE 3
Posts: 103

Rep: Reputation: 15
expanding variable in shell script


hii
i m new to shell scripting and i have a situation which i dont even know if is possible..
suppose i have a variable ... can i expand it to use to make another variable .. i suppsoe this shud be possible
for eg i m tryin to reverse the arguments supplied

#!/bin/bash
echo number of arguments are $#
a=1
while `test $a -le $#`
do
echo ${$a}
a=`expr $a + 1`
done

my problem is in echo line ... i m trying hit n trial to expand $a to a numeric ... and then refrencing the argument as we normally do.. like $1 , $2 and so on

so my question is can i use 2 $ sign ... obviously inner $ shud be expanded to its value ..

i wanna refer to $1 by assigning $a to another $ something like
$$a
where $a shud expand to some integer ... and $<integer> shud be my argument ..

plzz write back .. i m certainly new in scripting ...
thnks
 
Old 02-07-2006, 03:31 PM   #2
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 14.2
Posts: 1,491

Rep: Reputation: 50
dipen,

i am no beginner at shell scripting, but i really not clear what you are trying to do. maybe you can explain it a little more? sorry i'm sure others will agree. although its good that you are interesting in this...
 
Old 02-07-2006, 03:47 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I think I do understand.

Quote:
i wanna refer to $1 by assigning $a to another $ something like
$$a
where $a shud expand to some integer ... and $<integer> shud be my argument ..
Change the "echo line" to:
Code:
echo ${!a}
But in bash the script can be done simpler so you don't need to get the number from $a:
Code:
#!/bin/bash
echo Number of arguments = $#
echo $1
while shift ; do
	echo $1
done
 
Old 02-08-2006, 07:39 AM   #4
dipenchaudhary
Member
 
Registered: Jan 2006
Location: india
Distribution: FEDORA CORE 3
Posts: 103

Original Poster
Rep: Reputation: 15
kooool Hko
hurray it worked ....please give me a link to some good tutorial on shell script or some book which i can get from college library ..which explains such interesting stuffs

sorry satinet ... but i really write messed up english .. i have been pointed out a lot mant times of LQ .. i will try to improve !! satinet plzz suggest some good book or link to shell scripting

thnk u guys ... i will come back with other silly doubts
thnks again
 
Old 02-08-2006, 07:40 AM   #5
dipenchaudhary
Member
 
Registered: Jan 2006
Location: india
Distribution: FEDORA CORE 3
Posts: 103

Original Poster
Rep: Reputation: 15
kooool Hko
hurray it worked ....please give me a link to some good tutorial on shell script or some book which i can get from college library ..which explains such interesting stuffs

sorry satinet ... but i really write messed up english .. i have been pointed out a lot mant times on LQ .. i will try to improve !! satinet plzz suggest some good book or link to shell scripting

thnk u guys ... i will come back with other silly doubts
thnks again
 
Old 02-08-2006, 07:58 AM   #6
dipenchaudhary
Member
 
Registered: Jan 2006
Location: india
Distribution: FEDORA CORE 3
Posts: 103

Original Poster
Rep: Reputation: 15
and yehh hko .. wot does shift (after while) do in this script ..by the way we had to print in reverse .. n this script print in the same order as supplied .. i tried to change
#!/bin/bash
echo Number of arguments = $#
echo ${!#}
while shift ; do
echo ${!#}
done

output : last argument

then i tried ...
#!/bin/bash
echo Number of arguments = $#
echo ${!#}
while shift -1 ; do
echo ${!#}
done

output : wu , syntax error

can u give me link to some good book or tutorial .. or refer some good book? i really need it .. i m willin to spend time
thnks in advance
 
Old 02-08-2006, 08:19 AM   #7
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 14.2
Posts: 1,491

Rep: Reputation: 50
there is a good book called 'classic shell scripting' that is pretty good (IMHO) from O'Reilly. They also do a SED and awk book.

There are a few online tutorials, anyone recommend them? I'll get back to you on this...
 
Old 02-08-2006, 10:24 AM   #8
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 14.2
Posts: 1,491

Rep: Reputation: 50
i just saw this:

http://www.cactus.org/~dak/shellscript.html
 
Old 02-08-2006, 05:05 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/abs/html/index.html
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script and Dynamic variable xanthium Programming 11 07-12-2011 06:05 AM
can a C function return value to Shell Script variable yarnar Programming 17 06-02-2010 05:54 PM
Shell Script Random Variable Daniel Programming 14 12-03-2007 05:00 AM
Help with shell script, can't show variable stormrider_may Programming 5 01-27-2006 05:22 PM
Expanding shell variable in sed goofyheadedpunk Programming 4 09-11-2004 05:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:49 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