Register
It is currently Sun Nov 23, 2014 11:11 pm

Arguments


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Apr 02, 2009 8:47 pm   

Joined: Thu Apr 02, 2009 8:34 pm
Posts: 1
Location: Sri Lanka
I want to write a script which runs with command line arguments. The script can be passed with several arguments (the number may vary). I want to assign those arguments to a variable and do some modifications. Here is part of my code

Code:
i=0
total_arguments=$#

while [ $i -le $total_arguments ]
do
   opt=$i
   
   echo $opt

   let i=$i+1
done


Here I get the output as 0 to the number of arguments. That's because "$i" returns the value of the variable "i". I want to print the argument number "i".

How can I do this ?


Top
 Profile WWW YIM  
 PostPosted: Thu Apr 02, 2009 11:40 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
usually the easiest way to handle this type of script is to use the shift function.

lets say you ran the script with 3 arguments.. test1 test2 test3
Code:
#!/bin/bash
x=0
while [ ! -z $1 ]; do
    $argv[$x] = $1
    let x=x+1
    shift
done


this would create an array with the following content:
$argv[0] = test1
$argv[1] = test2
$argv[2] = test3

This happens because shift moves the entire argument list on step in. $1 will disappear and $2 will become $1, $3 will become $2.

You don't need to use an array in the while loop, you can do a case or anything you wish, the shift statement will still do what it supposed to :)

ps. while [ ! -z $1 ] means while $1 is not empty keep on going ds.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon Apr 06, 2009 3:53 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
I suggest you also check out the 'getopts' function in the bash man page :)


Top
 Profile  
 PostPosted: Wed Apr 22, 2009 9:37 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
getopts rules for things like this! Here's a quick example from my "functions" file:

Code:
## Define a "usage" function
USAGE () {
  echo "Usage: $0 -c<client> -p<passwd>"
  echo "Usage: You must supply a Client name and Password to create an account"
}

## If no command line args then echo usage and exit
if [ $# -eq 0 ]; then
  USAGE
  exit 1
fi

## Here's where we stuff the command line arguments into variables
while getopts "c:p:" options; do
  case $options in
    c ) client=$OPTARG;;
    p ) passwd=$OPTARG;;
    h ) USAGE ; exit 1;;
   \? ) USAGE ; exit 1;;
    * ) USAGE ; exit 1;;
  esac
done


Hope this helps!


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Yahoo [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP