Script to iterate all command line arguments


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script to iterate all command line arguments
# 1  
Old 06-14-2012
Bug Script to iterate all command line arguments

Hi guys,

I am having trouble with this script. What i want it to do is to iterate all command line arguments in reverse order. The code below does this fine but i need the output to print the words on separate lines instead of one line:

Code:
#!/bin/bash
#Takes in the arguments and displays them in reverse order
if [ $# -eq 0 ]
then
       echo "No arguments entered in script $0, try again"
       exit 1
fi
arguments=""
for word in $@
do
  arguments="$word $arguments"
done
echo $arguments


when i run this e.g. sh tutorial hello bye
I get the result: bye hello
I need the result to be:
Code:
bye
hello

Any ideas anyone?!

Moderator's Comments:
Mod Comment Please use code tags, thanks!

Last edited by zaxxon; 06-14-2012 at 06:07 AM.. Reason: code tags, see PM
# 2  
Old 06-14-2012
sed

Hi,

Try this one,

Code:
echo ${argument}|sed 's/ /\n/'

Cheers,
RangaSmilie
# 3  
Old 06-14-2012
Quote:
Originally Posted by rangarasan
Hi,

Try this one,

Code:
echo ${argument}|sed 's/ /\n/'

Cheers,
RangaSmilie
Hi,

This only works for two arguments given (hello bye), how will i get it working for X amount of arguments given?
# 4  
Old 06-14-2012
Quote:
Originally Posted by pipeline2012
Hi,

This only works for two arguments given (hello bye), how will i get it working for X amount of arguments given?
Code:
echo ${argument}|sed 's/ /\n/g'

g modifier for global substitution.

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 5  
Old 06-14-2012
One way to display the arguments in reverse order: Tested in ksh not bash.

Code:
#Takes in the arguments and displays them in reverse order
if [ $# -eq 0 ]
then
       echo "No arguments entered in script $0, try again"
       exit 1
fi
max=$#
counter=$max
while [ ${counter} -ge 1 ]
do
        eval arg=\$${counter}
        echo "Argument number ${counter} : ${arg}"
        counter=$(( ${counter} - 1 ))
done

./scriptname a b c
Argument number 3 : c
Argument number 2 : b
Argument number 1 : a

# 6  
Old 06-14-2012
Another way:

Code:
echo $@ | awk '{for(i=0;i<NF;i++) {print $(NF-i)}}'

# 7  
Old 06-14-2012
@jawsnn
Ingenious for normal script calls, but what if any of the parameters contains one or more space characters?
(This came up yesterday in another post).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing Command Line Arguments In C shell script

]I have a string like "/abc/cmind/def/pq/IC.2.4.6_main.64b/lnx86" and this string is given by user. But in this string instead of 64b user may passed 32 b an i need to parse this string and check wether its is 32b or 64 b and according to it i want to set appropriate flags. How will i do this... (11 Replies)
Discussion started by: codecatcher
11 Replies

2. Programming

How to pass the command line arguments to the shell script in c language?

hi, I am new in the shell script, and c programming with linux. I am looking to pass the arguments in c program that should be executed by the shell script. e.g. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { int i; for (i=1;i<argc; i++) { ... (2 Replies)
Discussion started by: sharlin
2 Replies

3. Shell Programming and Scripting

How to use case and command line arguments in shell script?

Hi... can anyone please help me out in using the CASE and command line argument in shell script... i am bit new to shell scripting...below i have explained my proble with example... say i have an executable file with name 'new1.sh' and there are 3 functions in it a(), b() and c()....and there... (5 Replies)
Discussion started by: swap21783
5 Replies

4. Shell Programming and Scripting

command line arguments

hi,,,, I want to create a command prompt, for example "prompt>", so my prompt need to handle commands, for example "prompt>cmd", so i want to know how to get arguments for my own commands cmd, i.e. default argc should contain arguments count and argv should point to the argument vector i.e, for... (2 Replies)
Discussion started by: vins_89
2 Replies

5. Shell Programming and Scripting

Shell script command line arguments

Hello All, i am known to the limitation of different shells while passing more than 9 command line arguments i just tried the example below i do see my current shell is tcsh echo $SHELL /bin/tcsh so if i make my script executable and run it output is ... (6 Replies)
Discussion started by: Deepak Dutt
6 Replies

6. UNIX for Dummies Questions & Answers

Bourne Shell Script that only takes command line arguments

Does anybody know how to Accept a “userid” as a command line argument on a Unix Bourne Shell Script? The output should be something like this: User userid has a home directory of /path/directory the default shell for this user is /path/shell (5 Replies)
Discussion started by: ajaira
5 Replies

7. UNIX for Advanced & Expert Users

Bourne Shell Script that only takes command line arguments

Does anybody know how to Accept a “userid” as a command line argument on a Unix Bourne Shell Script? The output should be something like this: User userid has a home directory of /path/directory the default shell for this user is /path/shell (1 Reply)
Discussion started by: ajaira
1 Replies

8. Shell Programming and Scripting

Bourne Shell Script that only takes command line arguments

Does anybody know how to Accept a “userid” as a command line argument on a Unix Bourne Shell Script? The output should be something like this: User userid has a home directory of /path/directory the default shell for this user is /path/shell (1 Reply)
Discussion started by: ajaira
1 Replies

9. Homework & Coursework Questions

I need to make a script that has 4 command line arguments to function properly.

I have no idea what the following means. The teacher is too advanced for me to understand fully. We literally went from running a few commands over the last few months to starting shell scripting. I am not a programmer, I am more hardware oriented. I wish I knew what this question was asking... (3 Replies)
Discussion started by: Wookard
3 Replies

10. Shell Programming and Scripting

KSH script: piping passes command-line arguments

Dear forum I have the following small script: #!/bin/ksh echo -e "abba-o" | awk -F '-' '{ print $2 }' | cut -b 1It needs to be ksh.. in bash I don't have this problem. If I run this on opensuse 10.2 I get this as output: e If I run this on suse enterprise 10 sp2 then I get this: o ... (1 Reply)
Discussion started by: gemtry
1 Replies
Login or Register to Ask a Question