LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-05-2004, 03:13 AM   #1
philipina
Member
 
Registered: May 2003
Posts: 77

Rep: Reputation: 15
Problem to get arguments with ${var}


Hello,

I would like to use a FOR statement to display all arguments received by my script.

I'm using the following code:
-------------------------------------
#!/bin/bash

declare nArguments=$#

for (( i=1; i<$nArguments; i++ ));
do
echo "var = ${i}"
done
-------------------------------------


echo "var = ${i}" --> this is not working, I also tried echo "var = ${\$i}" but this is the same result.

Thanks in advance for you help.

Alain.
 
Old 07-05-2004, 04:42 AM   #2
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
I am not sure the C/Java-like for loop you use in your example actually works in sh/bash. You could use a for loop without the "in" part (usually you would type "for item in list do...") to iterate through a list of commandline arguments:
Code:
#!/bin/bash
declare nArguments=$#

echo "nArguments=$nArguments"

for a
do
	echo "$a "
done
 
Old 07-05-2004, 04:58 AM   #3
philipina
Member
 
Registered: May 2003
Posts: 77

Original Poster
Rep: Reputation: 15
No, this is not working.

Thank.

Alain.
 
Old 07-05-2004, 05:36 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Code:
#!/bin/bash

declare nArguments=$#

echo "nArguments=${nArguments}"

current_argument=${1}
argument_number=1
while [ ! -z "${current_argument}" ]
do
  echo "Argument number ${argument_number}: ${current_argument}"
  shift
  current_argument=${1}
  ((argument_number=argument_number+1))
done

exit 0
As hw-tph said, I don't believe C-style for loops wok in bash; nor do variable++ type statements. man bash for details.
 
Old 07-05-2004, 06:10 AM   #5
philipina
Member
 
Registered: May 2003
Posts: 77

Original Poster
Rep: Reputation: 15
Thank it works.

This "shift" command is very useful for me, I didn't know it before!

Thank again.
 
  


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
2.4 too few arguments Problem thomas55 Linux From Scratch 5 11-29-2005 12:00 PM
PHP var problem DropHit Linux - Software 1 06-01-2005 05:39 PM
Problem with passing arguments in Perl bahadur Programming 1 05-30-2005 01:47 AM
var problem keith1234 *BSD 24 05-09-2005 12:11 AM
Moving /var/adm and /var/lib - why does it hurt? J_Szucs Linux - General 1 09-15-2004 06:46 PM

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

All times are GMT -5. The time now is 02:14 PM.

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