LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-27-2006, 02:09 PM   #1
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Rep: Reputation: 30
bash script 'for each command line argument'


how do i traverse through the command line arguments? is there something which represents a list or array of the arguments (sort of like $# is the number of arguments). this is what i was trying, but its not working to well..

Code:
CTR=$#

while [ $CTR -gt 0 ]
do
        echo $$CTR
        CTR=$(expr $CTR - 1)
done

any help?
 
Old 03-27-2006, 02:12 PM   #2
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
just figured it out...

Code:
for ARG in $*
do
        echo $ARG
done
 
Old 04-08-2008, 07:38 PM   #3
mizzouece
LQ Newbie
 
Registered: Apr 2008
Posts: 1

Rep: Reputation: 0
A more better solution (IMHO) is:

Code:
#!/bin/bash

foo ()
{
    for ARG in "$@"
    do
        echo $ARG
    done
}

# try it out
foo a b "c d" 'e f'
The output produced is:
Code:
a
b
c d
e f
If you instead use $*, the output changes as follows:
Code:
foo ()
{
    for ARG in $*
    do
        echo $ARG
    done
}

foo a b "c d" 'e f'

Output:
a
b
c
d
e
f
 
Old 01-28-2009, 01:51 PM   #4
amamar5
LQ Newbie
 
Registered: Jan 2009
Posts: 1

Rep: Reputation: 1
Another way to handle command line arguments

I know this is an old topic but just in case you come across this page through a search...

I know this doesn't use for each but its a nice way to handle command line arguments. I found this example elsewhere (can't remember) and I tweaked it a bit. It's not complete but it should get you started.


Code:
#IF NO ARGUMENTS WERE PROVIDED
function USAGE ()
{
    echo ""
    echo "USAGE: "
    echo "    ineff.sh [-?dh]"
    echo ""
    echo "OPTIONS:"
    echo "    -d  date of logfiles (format: yyyymmdd)"
    echo "    -h  logfile (format: 12 or 1[2-4])"
    echo "    -?  this usage information"
    echo ""
    echo "EXAMPLE:"
    echo "    ineff.sh -d 20060801 -h 2[12]"
    echo ""
    exit $E_OPTERROR    # Exit and explain usage, if no argument(s) given.
}

#PROCESS ARGS
while getopts ":d:h:?" Option
do
    case $Option in
        d    ) DT=$OPTARG;;
        h    ) HR=$OPTARG;;
        ?    ) USAGE
               exit 0;;
        *    ) echo ""
               echo "Unimplemented option chosen."
               USAGE   # DEFAULT
    esac
done


shift $(($OPTIND - 1))
#  Decrements the argument pointer so it points to next argument.
#  $1 now references the first non option item supplied on the command line
#+ if one exists.
 
  


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
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 09:38 AM
looking for string on particular line - bash script tara Linux - General 9 12-14-2005 05:43 PM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM


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