Accepting command line arguments in bash


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Accepting command line arguments in bash
# 1  
Old 03-13-2012
Accepting command line arguments in bash

I have this tcsh code that I want to convert to a bash script. Basically it accepts command line arguments supplied by the user and stores them, so they can be used to run a C++ program.

Code:
set arg_browseDir_inFileLst = ""
set allArgsUpCase = `echo "$*" | tr '[:lower:]' '[:upper:]'`
set opt_browseDir_flag = `echo $allArgsUpCase | awk '/BRWS/ {print 1}; ! /BRWS/ {print 0}'`

set narg = $#argv
while ($iarg < $narg)

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

  switch ($opt)

  # -- Mandatory Arguments -------------------------------------------------------------------------

  case "--NXZ":                        # Mandatory
  case "--NX-NZ":
    set arg_nxz = $par
    set opt_nxz = 1
    breaksw

  case "--VARP":
    set arg_varp = $par
    set opt_varp = 1
    breaksw

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

  case "--DRWPTH":
  case "--TDARWIN-PATH":
    set arg_drwPath = $par
    set opt_drwPath = 1
    breaksw

  case "--BASEIF":
  case "--BASE-INFILE":
    set arg_baseInFile = $par
    set opt_baseInFile = 1
    breaksw

  case "-DATAIF":
  case "--DATA-INFILE":
    set arg_tdatInFile = $par
    set opt_tdatInFile = 1
    breaksw

  case "--CMODIF":
  case "--CMOD-INFILE":
    set arg_cmodInFile = $par
    set opt_cmodInFile = 1
    breaksw

  case "--CMODOF":
  case "--CMOD-OUTFILE":
    set arg_cmodOutFile = $par
    set opt_cmodOutFile = 1
    breaksw

  case "--RSTRIF":
  case "--RSTR-INFILE":
    set arg_rstrInFile = $par
    set opt_rstrInFile = 1
    breaksw

  case "--NPOP":
    set arg_npop = $par
    set opt_npop = 1
    breaksw

  case "--SG":
  case "--SIGMA":
    set arg_sigma = $par
    set opt_sigma = 1
    breaksw

  case "-DRW-V":
  case "-DRW-VERBOSE":
  case "-DRW-VRB-LEVEL":
  case "--TDARWIN-VERBOSITY-LEVEL":
    set arg_drwVrb = $par
    set opt_drwVrb = 1
    breaksw

 case "--BRWSDIR-CHECK":                       # Separates numbers from characters.
  case "--BRWSDIR-CHECK-COLPOS":
    set opt_browseDir_chkColPos = 1
    breaksw

  case "--BRWSDIR-SORT":
  case "--BRWSDIR-SORT-FIELDS":
    set arg_browseDir_sortPtn = $par
    set opt_browseDir_sortFlds = 1
    breaksw

  case "--BRWSDIR-GROUP":
  case "--BRWSDIR-GROUP-TABLE":
    set arg_browseDir_groupPtn = $par
    set opt_browseDir_groupTbl = 1
    breaksw

  case "--BRWSDIR-ALL":
  case "--BRWSDIR-ALL-FILES":
    set opt_browseDir_allFiles = 1
    breaksw

  case "--BRWSDIR-RAYTRAC":                      # Display files related to raytrac.
  case "--BRWSDIR-RAYTRAC-FILES":
    set opt_browseDir_raytracFiles = 1
    breaksw

  default:
    set arg_browseDir_inFileLst = "$arg_browseDir_inFileLst $arg"
    set opt_browseDir_inFileLst = 1
    breaksw

  endsw

end   # while

---------- Post updated at 08:54 AM ---------- Previous update was at 08:45 AM ----------

I am aware of getopts but do not think base can handle long options though.

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

# 2  
Old 03-13-2012
Here is a start, run through a basic converter script. There will undoubtedly be things that will need to be edited manually.

Code:
arg_browseDir_inFileLst=""
allArgsUpCase=`echo "$*" | tr '[:lower:]' '[:upper:]'`
opt_browseDir_flag=`echo $allArgsUpCase | awk '/BRWS/ {print 1}; ! /BRWS/ {print 0}'`

narg=$#
while (($iarg < $narg))

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

  # -- Mandatory Arguments -------------------------------------------------------------------------

  "--NXZ":                        # Mandatory)
  "--NX-NZ")
    arg_nxz=$par
    opt_nxz=1
    ;;

  "--VARP")
    arg_varp=$par
    opt_varp=1
    ;;

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

  "--DRWPTH")
  "--TDARWIN-PATH")
    arg_drwPath=$par
    opt_drwPath=1
    ;;

  "--BASEIF")
  "--BASE-INFILE")
    arg_baseInFile=$par
    opt_baseInFile=1
    ;;

  "-DATAIF")
  "--DATA-INFILE")
    arg_tdatInFile=$par
    opt_tdatInFile=1
    ;;

  "--CMODIF")
  "--CMOD-INFILE")
    arg_cmodInFile=$par
    opt_cmodInFile=1
    ;;

  "--CMODOF")
  "--CMOD-OUTFILE")
    arg_cmodOutFile=$par
    opt_cmodOutFile=1
    ;;

  "--RSTRIF")
  "--RSTR-INFILE")
    arg_rstrInFile=$par
    opt_rstrInFile=1
    ;;

  "--NPOP")
    arg_npop=$par
    opt_npop=1
    ;;

  "--SG")
  "--SIGMA")
    arg_sigma=$par
    opt_sigma=1
    ;;

  "-DRW-V")
  "-DRW-VERBOSE")
  "-DRW-VRB-LEVEL")
  "--TDARWIN-VERBOSITY-LEVEL")
    arg_drwVrb=$par
    opt_drwVrb=1
    ;;

 "--BRWSDIR-CHECK":                       # Separates numbers from characters.)
  "--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":                      # Display files related to raytrac.)
  "--BRWSDIR-RAYTRAC-FILES")
    opt_browseDir_raytracFiles=1
    ;;

  *)
    arg_browseDir_inFileLst="$arg_browseDir_inFileLst $arg"
    opt_browseDir_inFileLst=1
    ;;

  esac

end   # while

Converted with csh2sh.pl by Olivier Aubert

---------- Post updated at 15:00 ---------- Previous update was at 14:59 ----------

getopts cannot handle long options. Only the very latest ksh93 can do this...

Last edited by Scrutinizer; 03-13-2012 at 11:07 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-13-2012
I got csh2sh.pl by Olivier Aubert. How did you use it to create a bash file from a tcsh script?

Last edited by kristinu; 03-13-2012 at 11:22 AM..
# 4  
Old 03-13-2012
You would have to look, as I downloaded it some time ago, I do not recall where from. What does "csh2sh.pl" give in google?

I quickly changed this manually by the way:

Code:
narg=$#
while (($iarg < $narg))

  (( iarg = $iarg + 1 ))

the latter two statements are not shell but bash/ksh93

Last edited by Scrutinizer; 03-13-2012 at 11:57 AM..
# 5  
Old 03-13-2012
I converted my tcsh script using csh2sh-rca.pl

The converted file has as it's first line

Code:
#!/bin/sh

I am wondering whether the conversion create a sh rather than GNU Bourne-Again SHell which I can designate with .bash

I was going to replace it to

Code:
#!/bin/bash

# 6  
Old 03-13-2012
It will create a Bourne Shell script or maybe a POSIX shell script, but bash will have no trouble interpreting that, since it is a Bourne Shell derivative and POSIX compliant, so you should be able to change the shebang to bash.

Last edited by Scrutinizer; 03-13-2012 at 12:29 PM..
# 7  
Old 03-15-2012
In the bash conversion, the while statement is as follows

Code:
while (($iarg < $narg))
  .
  .
  .
done

However the bash while loop syntax is

Code:
while [ condition ]
do
   command1
   command2
   command3
done

Can you give me some clarification on this please?

---------- Post updated at 10:07 AM ---------- Previous update was at 09:37 AM ----------

I am also trying to do and if statement and getting problems:
Code:
if (($opt_usage -eq 1))
then
  ...
  ...
fi

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wanted: Help with escaping bash command line arguments

Please forgive me if this is the wrong forum. I want to execute some one liners with the groovy programming language and I'm having trouble escaping the special characters to accommodate bash. Here is one of the lines that is giving me trouble: groovy -e "(new... (1 Reply)
Discussion started by: siegfried
1 Replies

2. Shell Programming and Scripting

Passing arguments from a bash shell script to a command

I'm pretty new to bash scripting and I've found myself writing things like this (and the same with even more nesting): if $CATEGORIES; then if $LABEL_SLOTS; then $pyth "$wd/texify_grammar.py" "$input" "$texfile" "--label-slots" "--categories" "$CATEGORY_LIST" ... (9 Replies)
Discussion started by: burbly
9 Replies

3. 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

4. Shell Programming and Scripting

Accepting user input and arguments in PERL

Hi All, Can we pass arguments while calling the perl script and as well as ask user input during execution of the script? My program is as below: I am passing arg1 and arg2 as argements to test.pl ]./test.pl arg1 arg2 Inside the test.pl I have : print "Do you want a name ? (y/n) : ";... (2 Replies)
Discussion started by: jisha
2 Replies

5. UNIX for Dummies Questions & Answers

command line arguments

hi, can someone how to accept command line arguments as a variable using in script? like: ./scriptname arguments by accept arguments, I can use it in my script? thx! (1 Reply)
Discussion started by: ikeQ
1 Replies

6. Shell Programming and Scripting

Help parsing command line arguments in bash

Looking for a little help parsing some command line arguments in a bash script I am working on, this is probably fairly basic to most, but I do not have much experience with it. At the command line, when the script is run, I need to make sure the argument passed is a file, it exists in the... (3 Replies)
Discussion started by: Breakology
3 Replies

7. UNIX for Dummies Questions & Answers

Command line arguments.

I am working on a script wherein i need the user to enter the Build ID for eg:the command line will show enter the build ID Now on entering the build ID it should be assigned to @ARGV. How can this be done.? (1 Reply)
Discussion started by: Varghese
1 Replies

8. Shell Programming and Scripting

Accepting filename as command line param and writing to it

Hi, Is it possible to accept a filename as command line parameter and then write to that file using command redirection? i tried the below script. outputfile=`echo $1` echo "Writing to file" > 'echo $outputfile' exit $returncode but it isnt working. is there any other way to... (9 Replies)
Discussion started by: silas.john
9 Replies

9. UNIX for Dummies Questions & Answers

arguments in command line

Hi all, How many arguments can we pass while testing a prgm at command line.. I encountered an issue while passing 10 arguments. For $10 its taking argument passed for $1 followed by 'zero'. can we pass more than 9 arguments /Is there any other way. Thanks, rrs (6 Replies)
Discussion started by: rrs
6 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