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
# 1  
Old 03-19-2012
Reading command line options from bash script

I have the following code and I am calling it using

Code:
./raytrac.bash -u

and getting problems. For some reason opt_usage is still 0.

Code:
opt_usage=0
iarg=0
narg=$#
while (($iarg < $narg))
do

  (( iarg = $iarg + 1 ))
  arg=$argv[$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

  # -- Optional Arguments --------------------------------------------------------------------------

  "--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
  ;;

  "--RCVSIF"|"--RCVS-INFILE"|"--RECEIVERS_INFILE")
    arg_rcvsInFile=$par
    opt_rcvsInFile=1
  ;;

  "--PHSS"|"--PHASES")
    arg_phases=$par
    opt_phases=1
  ;;

  "--LV"|"--LEVEL")
    arg_level=$par
    opt_level=1
  ;;

  "--FMT"|"--FORMAT")
    arg_format=$par
    opt_format=1
  ;;

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

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

  "--TWPTDIST")
    arg_twptDist=$par
    opt_twptDist=1
  ;;

  "--MAXITERTP")
    arg_maxitertp=$par
    opt_maxitertp=1
  ;;

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

  "--TRVT"|"--TRVT-OUTFILE")
    arg_trvtOutFile=$par
    opt_trvtOutFile=1
  ;;

  "--V-raytrac"|"--VRB-RAYTRAC-LEVEL")
    arg_vrbRaytracLevel=$par
    opt_vrbRaytrac=1
  ;;

  "--PF"|"--PFILE")
    arg_pfile=$par
    opt_pfile=1
  ;;

  "--BG-RAYTRAC")
    opt_bgRaytrac=1
  ;;

  "-V"|"--VRB-LEVEL")
    arg_vrbLevel=$par
    opt_vrb_usrInputFlag=$usrInputFlag
    opt_verbose=1
  ;;

  "-Q"|"--QUIET")
    opt_verbose=0
  ;;

  "-U"|"--USAGE")
    opt_usage=1
  ;;

  "-E"|"--EXAMPLES")
    opt_examples=1
  ;;

  "-H"|"--HELP")
    opt_help=1
  ;;

  "--BRWSDIR-PATH")
    arg_browseDirPath=$par
    opt_browseDirPath=1
  ;;

  "--BRWSDIR-CHECK"|"--BRWSDIR-CHECK-COLPOS")
    opt_browseDir_chkColPos=1
  ;;

  "--BRWSDIR-SORT"|"--BRWSDIR-SORT-FIELDS")
    arg_browseDir_sortPtn=$par
    opt_browseDir_sortFlds=1
  ;;

  "--BRWSDIR-GROUP"|"--BRWSDIR-GROUP-TABLE")
    arg_browseDir_groupPtn=$par
    opt_browseDir_groupTbl=1
  ;;

  "--BRWSDIR-ALL"|"--BRWSDIR-ALL-FILES")
    opt_browseDir_allFiles=1
  ;;

  "--BRWSDIR-RAYTRAC"|"--BRWSDIR-RAYTRAC-FILES")
    opt_browseDir_raytrac=1
  ;;

  "--BRWSDIR-DRW"|"--BRWSDIR-DRW-FILES")
    opt_browseDir_drw=1
  ;;

  "--BRWSDIR-SMP"|"--BRWSDIR-SMP-FILES")
    opt_browseDir_smp=1
  ;;

  "--BRWSDIR-LOG"|"--BRWSDIR-LOG-FILES")
    opt_browseDir_logFiles=1
  ;;

  "--BRWSDIR-MSF"|"--BRWSDIR-MSF-FILES")
    opt_browseDir_msfFiles=1
  ;;

  "--BRWSDIR-V"|"--BRWSDIR-VRB-LEVEL")
    arg_browseDir_vrbLevel=$par
    opt_browseDir_verbose=1
    opt_browseDir_vrbLevel=$usrInputFlag
  ;;

  "--BRWSDIR-Q"|"--BRWSDIR-QUIET")
    opt_browseDir_quiet=1
    opt_browseDir_verbose=0
  ;;

  *)
    arg_browseDir_LstFiles="$arg_browseDir_FileLst $arg"
    opt_browseDir_LstFiles=1
  ;;

  esac

done   # which

---------- Post updated at 10:27 AM ---------- Previous update was at 09:33 AM ----------

Problem seems to be here, when I am trying to get the argument using argv.

Code:
arg=$argv[$iarg]

# 2  
Old 03-19-2012
FYI - Consider getopts for processing arguments.
# 3  
Old 03-19-2012
You missed below to give at start of your script to initialized array ... Smilie
Code:
argv=("$@")



Also your array syntax is wrong it should be as below instead and note you have incremented the "iarg"at wrong position that you code will ignore first argument ..
Code:
arg=$argv[$iarg]       ---->>>>     arg=${argv[$iarg]}

Try This code you will get your Mistakes ...
Code:
argv=("$@")
opt_usage=0
iarg=0
narg=$#
while (($iarg < $narg))
do
  arg=${argv[$iarg]}
  echo $arg
  ((iarg++))
done

--Shirish Shukla
# 4  
Old 03-19-2012
I have found BASH_ARGV. What do you thing of using it rather than
argv=("$@")
Code:
for a in ${BASH_ARGV[*]} ; do
  echo "BASH_ARGV = $a"
done

# 5  
Old 03-19-2012
That's a bit redundant if all you want is a loop:

Code:
for X in "$@"
do
        echo "argument $X"
done

I would not reccomend using BASH_ARGV. That's only going to exist under bash, for starters, and just doesn't seem necessary.
# 6  
Old 03-19-2012
There are multiple way ... it's depends on your use and logic that your mind can build at that time .. have provided base on your posted script mis....

--Shirish Shukla
# 7  
Old 03-19-2012
What is the difference between $* and $@
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