Reading command line options from bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading command line options from bash script
# 8  
Old 03-19-2012
"$*" splits on spaces.

"$@" preserves the arguments as they were given, spaces or no.
# 9  
Old 03-19-2012
I am using them in this way. $@ for creating an array, whereas I use $* to create a string.
Code:
args=("$@")                                      # Set array containing all arguments.
argsUC=`echo "$*" | tr '[:lower:]' '[:upper:]'`  # Set list containing all arguments.

What do you mean exactly by
Code:
"$@" preserves the arguments as they were given, spaces or no.

---------- Post updated at 05:18 PM ---------- Previous update was at 05:12 PM ----------

I also want to get the directory name from the script that I run. I am using this line.
Any suggestions?

Code:
bashPath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Basically, I do as follows

Code:
cd /home/chrisd/tatsh/branches/test
/home/chrisd/tatsh/trunk/hstmy/bin/bash/raytrac.bash --brwsdir-log

Inside the script raytrac.bash, I want to capture the directory path from where the script resides (i.e. I want to get /home/chrisd/tatsh/trunk/hstmy/bin/bash)
# 10  
Old 03-19-2012
Quote:
Originally Posted by kristinu
What do you mean exactly by
Code:
"$@" preserves the arguments as they were given, spaces or no.

"$*" becomes a string. "$@" becomes a list.

as in, arguments "A" "B" "C" would evaluate to "A B C" for $*, and "A" "B" "C" for "$@"

Using "$@" to create an array is redundant, as I've shown you. The arguments are all there already and can be used directly. In BASH you can even access them numerically:

Code:
N=1
echo "${!N}" # First argument

I don't know why you'd need to cd into a directory to get its name. If you can cd into it, you obviously know its name already.
# 11  
Old 03-19-2012
I would need to run some other scripts inside raytrac.bash.

For example, from raytrac.bash I want to run the following:
Code:
/home/chrisd/tatsh/trunk/hstmy/bin/bash/browseDir.bash
/home/chrisd/tatsh/trunk/hstmy/bin/bash/printTable.bash
/home/chrisd/tatsh/trunk/hstmy/bin/prog/raytrac

I do not want the path hardwired, as I would need to change it whenever I am on another system.

By running my script /home/chrisd/tatsh/trunk/hstmy/bin/bash/raytrac.bash, I want to get the directory path before raytrac.bash. This will get me the bash directory path.

---------- Post updated at 05:58 PM ---------- Previous update was at 05:52 PM ----------

Quote:
Originally Posted by Corona688
"$*" becomes a string. "$@" becomes a list.

as in, arguments "A" "B" "C" would evaluate to "A B C" for $*, and "A" "B" "C" for "$@"

Using "$@" to create an array is redundant, as I've shown you. The arguments are all there already and can be used directly. In BASH you can even access them numerically:

Code:
N=1
echo "${!N}" # First argument

I don't know why you'd need to cd into a directory to get its name. If you can cd into it, you obviously know its name already.
Ok, so how would you code the section below in your opinion?

Code:
iarg=0
narg=$#                                          # Number of arguments passed.
args=("$@")                                      # Set array containing all arguments.
argsUC=`echo "$*" | tr '[:lower:]' '[:upper:]'`  # Set list containing all arguments.
opt_browseDir_flag=`echo $argsUC | awk '/BRWS/ {print 1}; ! /BRWS/ {print 0}'`

while (($iarg < $narg))
do

  arg=${args[$iarg]}    # Bash arrays are zero-based: first element is indexed with number 0.
  usrInputFlag=`echo $arg | awk '/=/ {print 1}; ! /=/ {print 0}'`
  opt=`echo $arg | awk 'BEGIN {FS="="} {print $1}' | tr '[:lower:]' '[:upper:]'`
  par=`echo $arg | awk 'BEGIN {FS="="} {print $2}'`

  case $opt in

  "--CMODIF"|"--CMOD-INFILE")
    arg_cmdInFile=$par
    opt_cmdInFile=1
  ;;

  "--DTAU")
    arg_dtau=$par
    opt_dtau=1
  ;;

  "--BRACDIST")
    arg_bracDist=$par
    opt_bracDist=1
  ;;

  "--RAYS"|"--RAYS-OUTFILE")
    arg_raysOutFile=$par
    opt_raysOutFile=1
  ;;

  *)
    arg_browseDir_fileLst="$arg_browseDir_fileLst $arg"
    opt_browseDir_fileLst=1
  ;;

  esac

  ((iarg++))

done   # which

---------- Post updated at 06:30 PM ---------- Previous update was at 05:58 PM ----------

Quote:
Originally Posted by Corona688
"$*" becomes a string. "$@" becomes a list.

as in, arguments "A" "B" "C" would evaluate to "A B C" for $*, and "A" "B" "C" for "$@"

Using "$@" to create an array is redundant, as I've shown you. The arguments are all there already and can be used directly. In BASH you can even access them numerically:

Code:
N=1
echo "${!N}" # First argument

I don't know why you'd need to cd into a directory to get its name. If you can cd into it, you obviously know its name already.
I came up with the piece below
Code:
iarg=1
narg=$#                                          # Number of arguments passed.
while (($iarg <= $narg))
do
  echo "Argument ${!iarg}"
  ((iarg++))
done

Is this what you were suggesting? I am getting bit confused about this as I had to do start from iarg=1 and then continue till iarg is narg. I thought it goes to one less.

---------- Post updated at 06:57 PM ---------- Previous update was at 06:30 PM ----------

I can get the list of all arguments using either "$@" or "$*". However, the best one to use is "$@" I think, as "$*" might fail in some instances.

Code:
argsUC=`echo "$@" | tr '[:lower:]' '[:upper:]'`  # Set list containing all arguments.
argsUC=`echo "$*" | tr '[:lower:]' '[:upper:]'`  # Set list containing all arguments.

# 12  
Old 03-20-2012
[QUOTE=kristinu;302609369]Ok, so how would you code the section below in your opinion?

Code:
iarg=0
narg=$#                                          # Number of arguments passed.
args=("$@")                                      # Set array containing all arguments.
argsUC=`echo "$*" | tr '[:lower:]' '[:upper:]'`  # Set list containing all arguments.
opt_browseDir_flag=`echo $argsUC | awk '/BRWS/ {print 1}; ! /BRWS/ {print 0}'`

No matter which $* of $@ you use, feeding it into echo is going to flatten it. tr can't tell the difference between spaces caused by spaces, and spaces caused by an argument being split, because they're both just spaces... You don't put arguments through a pipe, only bytes. If you want to feed it through pipes, you'll have to preserve the splitting yourself, probably by changing the separator into something strange, so that echo "$*" output a|b|c|d instead of a b c d. This will let us split it ourselves later without losing anything.

I'd redo the arguments splitting on | and =, so that I don't have to worry about the difference between --argument=asdf and --argument asdf. The IFS controls all splitting, and set -- sets arguments to any values you want, so we can shuffle the arguments creatively in one go.

I wouldn't transform everything into uppercase. That mangles filenames too, and UNIX filenames are case-sensitive -- they'd be ruined.

I also wouldn't use awk 9 times to process one argument when case can do everything the first try...

Code:
OLDIFS="$IFS"
# IFS is a special variable that controls splitting.  We split on | and = now, not whitespace.  "$*" will now become a string like a|b|c
IFS="|="

# Split on | and =.  $* will insert the | itself when IFS="|", so we
# get back what we put in, except everything's now split on = too, i.e.
# "a b" "c" --option=file
# becomes
# "a b" "c" --option file
set -- "$*"

# Return IFS to its normal whitespace.
IFS="$OLDIFS"

# Loop until all arguments are used up.
# Shift tosses the value of $1 and sets $1=$2, $2=$3, ...
# so doing this repeatedly will decrease the value of $# until
# we run out of arguments.
while [ "$#" -gt 0 ]
do
        case "$1" in
        "--"[cC][mM][oO][dD][iI][fF]|"--"[cC][mM][oO][dD][iI][nN][fF][iI][lL][eE])
                shift  # Skip ahead one to the next argument.
                arg_cmdInFile="${1}"
                ;;
        esac

        shift # Skip ahead to the next argument
done

Pure builtins with no backticks necessary. And this doesn't even need BASH -- this should work equally well in BASH, KSH, ZSH, or even a plain Bourne shell.

Last edited by Corona688; 03-20-2012 at 01:03 PM..
This User Gave Thanks to Corona688 For This Post:
# 13  
Old 03-20-2012
I was doing like the code below, I was only taking care to putting to upcase on the left hand side of the "=" sign. As you say, ideally it would be good to have the script accept --argument=asdf and --argument asdf.

Having to code as your scheme seems quite long rather than just taking the upper case, but then as you say, it will handle both --argument=asdf and --argument asdf as expected.
Considering all this, you think your new code is fine?


I am unsure what
set -- "$*" does. Would it be possible to explain better your comment

Code:
set --  sets arguments to any values you want, so we can shuffle the arguments creatively in one go.

"--"[cC][mM][oO][dD][iI][fF]|"--"[cC][mM][oO][dD][iI][nN][fF][iI][lL][eE])

What should for checking the other options "--CMODIF"|"--CMOD-INFILE", "--SRCSIF"|"--SRCS-INFILE"|"--SOURCES-INFILE" etc...

Code:
iarg=0
narg=$#                                          # Number of arguments passed.
args=("$@")                                      # Set array containing all arguments.

while (($iarg < $narg))
do

  arg=${args[$iarg]}
  usrInputFlag=`echo $arg | awk '/=/ {print 1}; ! /=/ {print 0}'`
  opt=`echo $arg | awk 'BEGIN {FS="="} {print $1}' | tr '[:lower:]' '[:upper:]'`
  par=`echo $arg | awk 'BEGIN {FS="="} {print $2}'`

  case $opt in

  "--RAYTRAC-PATH")
    arg_raytracPath=$par
    opt_raytracPath=1
  ;;

  "--CMODIF"|"--CMOD-INFILE")
    arg_cmdInFile=$par
    opt_cmdInFile=1
  ;;

  "--SRCSIF"|"--SRCS-INFILE"|"--SOURCES-INFILE")
    arg_srcsInFile=$par
    opt_srcsInFile=1
  ;;

  *)
    arg_browseDir_fileLst="$arg_browseDir_fileLst $arg"
    opt_browseDir_fileLst=1
  ;;

  esac

  ((iarg++))

done   # which

---------- Post updated at 03:03 PM ---------- Previous update was at 01:06 PM ----------

I have tried the following code but I am getting
Code:
$* = --cmodif=myfile.cmod
arg_cmdInFile =

Seems that arg_cmdInFile has not been set.

Code:
IFS="|="
set -- "$*"
IFS="$OLDIFS"

echo "\$* = $*"

while [ "$#" -gt 0 ]
do
  case "$1" in

  "--"[cC][mM][oO][dD][iI][fF]|"--"[cC][mM][oO][dD][iI][nN][fF][iI][lL][eE])
    shift  # Skip ahead one to the next argument.
    arg_cmodInFile="${1}"
  ;;

  esac

  shift # Skip ahead to the next argument
done

echo "arg_cmdInFile = $arg_cmodInFile"

---------- Post updated at 03:16 PM ---------- Previous update was at 03:03 PM ----------

I have put some comments. Looks like $1 contains all arguments.

Code:
OLDIFS="$IFS"      
IFS="|="
set -- "$*"
IFS="$OLDIFS"

echo "\$* = $*"

while [ "$#" -gt 0 ]
do

  echo "Current Argument is ${1}"
  case "$1" in

  "--"[cC][mM][oO][dD][iI][fF]|"--"[cC][mM][oO][dD][iI][nN][fF][iI][lL][eE])
    echo "Argument is ${1}"
    shift  # Skip ahead one to the next argument.
    arg_cmodInFile="${1}"
  ;;

  esac

  shift # Skip ahead to the next argument

done

echo "arg_cmdInFile = $arg_cmodInFile"

running ./raytrac.bash --cmodif=myfile.cmod --srcsif sources.srcs
the output is

Code:
$* = --cmodif=myfile.cmod|--srcsif|sources.srcs
Current Argument is --cmodif=myfile.cmod|--srcsif|sources.srcs
arg_cmdInFile =


Last edited by kristinu; 03-20-2012 at 04:57 PM..
# 14  
Old 03-20-2012
There was a minor error in my code. You want

set -- $* not set -- "$*" because the quotes will prevent it from splitting.

When I replace that, your code does this:

Code:
$ ./arg.sh --cmodif=myfile.cmod --srcsif sources.srcs

$* = --cmodif myfile.cmod --srcsif sources.srcs
Current Argument is --cmodif
Argument is --cmodif
Current Argument is --srcsif
Current Argument is sources.srcs
arg_cmdInFile = myfile.cmod

$

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to read multiple options from file, line by line

Hi all I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis. The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here... (1 Reply)
Discussion started by: haggismn
1 Replies

2. Homework & Coursework Questions

how to create new options of "dd command" using bash script?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Creating bash script the dd command and its new options (-l -U) -l options to lowercase contents of file -U... (2 Replies)
Discussion started by: katherineskye1
2 Replies

3. Shell Programming and Scripting

Reading command options one by one

Hi, Just some questions on the script below...? Given: bash-2.03$ command -a option1 name1 name2 ParseOptions() { local Len=${#@} local Ctr=2 #always start at 2 local Name=() local Iter=0 while ; do if <- Is this correct? so I can get the $2... (2 Replies)
Discussion started by: h0ujun
2 Replies

4. UNIX for Dummies Questions & Answers

Running set options from the command line and bash command

I'm reading about debugging aids in bash and have come across the set command. It says in my little book that an addition to typing set you can also use them "on the command line when running a script..." and it lists this in a small table: set -o option Command Line... (5 Replies)
Discussion started by: Straitsfan
5 Replies

5. Shell Programming and Scripting

awk script file command line options

Being new to awk I have a really basic question. It just has to be in the archives but it didn't bite me when I went looking for it. I've written an awk script, placed it in a file, added the "#!/usr/bin/awk -f" at the top of the script and away I go. "% myAwk <inputfile>" gives me exactly what... (2 Replies)
Discussion started by: tomr2k
2 Replies

6. Shell Programming and Scripting

Run perl script, with command-line options

Hello everyone, I have a perl script which takes various command line options from user like : test.pl -i <input_file> -o <output_file> -d <value> -c <value> Now I have multiple input files in a directory: <input_file_1> <input_file_2> <input_file_3> <input_file_4> ..... .... ...... (6 Replies)
Discussion started by: ad23
6 Replies

7. Shell Programming and Scripting

Using perl to get options from command line

Hi all, I want to get options from command line by perl. usage() options: -h Show this help message and exit -t Name of tester --timeout Set the timeout -l ... (1 Reply)
Discussion started by: Damon_Qu
1 Replies

8. Shell Programming and Scripting

Help with shell script to run the commands reading options from local file

I have to use shell script to run series of commands on another unix box by connecting through SSH and giving user credentials. For running commands on remote machine I have to use options reading from a local file. Process: Connecting to remote unix server <host1.ibm.com> through ssh Login: ... (2 Replies)
Discussion started by: itsprout
2 Replies

9. Shell Programming and Scripting

how to? launch command with string of command line options

my description from another thread... here's my code: #!/bin/bash IFS=$'\n' function OutputName() { input=$1 echo $input input=`echo "$input" | sed -e 's/.//'` input=`echo "$input".avi` output_name=$input } if ]; then echo... (5 Replies)
Discussion started by: TinCanFury
5 Replies

10. Shell Programming and Scripting

Bash: Reading 2 arguments from a command line

If no arguments are entered I wanna be able to read 2 arguments, i have done like this but it doesnt work: x=0 until #loop starts do if ; then echo No arguments were entered, please enter 2 arguments. read $1 $2 elif || ; then echo $#... (0 Replies)
Discussion started by: Vozx
0 Replies
Login or Register to Ask a Question