LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 07-23-2004, 09:40 AM   #1
linsson
LQ Newbie
 
Registered: Jul 2004
Posts: 12

Rep: Reputation: 0
Indicating external argument into loops in SCRIPTs


hallo everyone!

Here is a simplified version of my problem:

I have a script and would like it to handle several arguments: a1,a2,a3,a4..................
I usually indicate them with $1, $2, $3.......
i want it to print on screen a2,a3.........


for (( i = 2 ; i < $# ; i++ ))
do
echo ????????????????
done

if I write $i it does not work because it prints "i" values and not a1,a2..........
i tried various things but none worked

$[$i] ; $("$"$i"") ; $ $i ; $"$i"

I think there must be an easy answer to write external arguments in a loop but I couldn't find it.

Can anyone help?

Thanks a lot

PS Does anyone know the bash scripting command which plays the role of the BREAK command in c++ ??
 
Old 07-24-2004, 02:44 AM   #2
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi linsson,
In bash, break is "break"! If all you want is to show the parameters on the screen, you have the shift command
Code:
x=0
while [ "$#" != "0" ]
do
  x=`expr $x + 1`
  echo \$$x = $1
  shift
done
 
Old 07-25-2004, 03:52 PM   #3
bruce ford
Member
 
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43

Rep: Reputation: 15
hi linsson,
if you just want to print your args you can use
echo $*
but I thinks that's not what you wanted
If you dislike using shift (e.g. because you need the arguments in place after your print loop)
you can use an array for temporary storage of your args:

Code:
]
args=($*)
for (( i=0 ; i<${#args[*]} ; i++ ))
do
  echo ${args[i]}
done
So long...
bruce ford
 
Old 07-25-2004, 05:39 PM   #4
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi Bruce,
Wonderful, I never used arrays in shell and I just learned one more trick. Probably, this is what linsson wants.
 
Old 07-25-2004, 06:20 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Re: Indicating external argument into loops in SCRIPTs

Quote:
Originally posted by linsson
if I write $i it does not work because it prints "i" values and not a1,a2..........
i tried various things but none worked

$[$i] ; $("$"$i"") ; $ $i ; $"$i"
This would have worked: ${!i}
 
Old 07-28-2004, 10:00 AM   #6
linsson
LQ Newbie
 
Registered: Jul 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Sorry for the delay i answer you guys;
Well thank you so much!!

I have learned more than i expected to do.

I just woud like to ask osvaldomarques how his script works:

It seems to me that the shift command alone is able to cut one by one an argument from the list but if this were all the story the script

while [ "$#" != "0" ]
echo $1
shift
done

should work as well; but that's not the case.
Could you explain why?
Thanks a lot everyone again
bye
 
Old 07-28-2004, 10:38 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,486

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
it works for me, after inserting the missing "do" on the second line.
 
Old 07-28-2004, 12:55 PM   #8
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Yes, it is the "do" miracle!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
VPN: Debian Scripts -> Mandriva 2006 Scripts Undefined Mandriva 0 11-30-2005 12:10 PM
Cannot run scripts on my external hd eireflows Linux - Hardware 3 07-17-2005 08:57 AM
while loops + while : blizunt7 Linux - General 3 12-04-2004 05:27 PM
bash scripting - referring to external arguments into loops linsson Linux - General 2 07-23-2004 12:24 PM
loops JMK Linux - Newbie 11 04-09-2004 05:30 PM


All times are GMT -5. The time now is 05:50 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration