Passing options to a bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing options to a bash script
# 1  
Old 05-21-2008
Passing options to a bash script

I'm just not sure where to start looking into this. I want to be able to create switches for my script. Back in the day I'd make my scripts interactive...you know:

Code:
echo "what report do you want"
echo "A)boxes with errors"
echo "B)boxes with more than 5 errors"
echo "C)Service groups that have thrown errors"
read $1
if ...

but now that seems kind of amateurish. I'd rather have a script where errorreport.sh -c made a list of boxes, errorreport.sh -m listed boxes with multiple errors and so on.

I'm not asking anyone to give me a step by step on how to do this, I'd rather learn by doing, but I don't even know where to start. I've tried googling "adding switches to bash scripts" and the like, and I've been scanning my o'reiley books, but at quick glance I don't see what I'm looking for.

If anyone could tell me where I might at least start reading up on this I'd appreciate it.
# 2  
Old 05-21-2008
I think that you want "getopts", which is a bash built-in command.
# 3  
Old 05-21-2008
You can do something as the following, you can also add additional logic to force a user to enter good input, so the script does not exit, but I'll leave that to you.

Code:
#!/bin/sh

echo "What report do you want?"
echo "A) boxes with errors"
echo "B) boxes with more than 5 errors"
echo "C) Service groups that have thrown errors"
read CHOICE

case $CHOICE in
        A|a)
                echo "Choice was $CHOICE"
                ;;
        B|b)
                echo "Choice was $CHOICE"
                ;;
        C|c) 
                echo "Choice was $CHOICE"
                ;;
          *)
                echo "Valid Choices are A,B,C"
                exit 1
                ;;
esac


Last edited by primp; 05-21-2008 at 02:48 AM..
# 4  
Old 05-21-2008
This might also help you..Look out for $1=-g option.


Code:
# # # # # # # # # # # #
#     main section
# # # # # # # # # # # #

  print "script $(basename $0) executing $(date +%Y/%m/%d\ %H:%M:%S) \n"

#
#  only allow this script to be run by the root user
#
  if ! id | grep -q "uid=0(root)" ; then
    print "ERROR:  must be root in order to run this script"
    exit 1
  fi

  if [[ "$PLEX_ROOT" = "/claims/amxw" ]]; then
    if [[ "$NLROOTDIR" != "/claims/amxw" ]]; then
      print "ERROR: NLROOTDIR must be set to /claims/amxw in order to create a plex"
      exit 1
    fi
  fi

  if [[ "$1" = "-g" ]]; then
    shift
    print "You are obviously a plex god and have chosen to skip the approved naming convention.  Please proceed, your eminence"
    print
    mode=GOD
  else
    mode=MORTAL
  fi

  plexname=$1

  if [[ -z $plexname ]]; then
    print "ERROR: plex name isn't specified"
    exit 1
  fi

  if [[ "$mode" = "MORTAL" ]]; then
    if ! print $plexname | grep -q ^plex ; then
      print "ERROR: plex name must begin with 'plex' and be at least 9 characters"
      exit 1
    fi
  fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running options in bash script

Hello UNIX & Linux Forums community! Long time Linux daily user hobbyist, new to shell scripting.... I'm working on a script that does all the "work" in one script, and makes calls to a second script to display info to the user via mostly expanding variables in heredocs. I'm contemplating... (6 Replies)
Discussion started by: Cody Learner
6 Replies

2. Web Development

Passing variable from PHP to bash script

I am totally new to PHP and I am trying to create a script that will as a user for a hostname and then use the "hostname" variable to generate a report using REST API. I am able to create the html script and php script to GET the "hostname" but I am having trouble passing the hostname variable... (10 Replies)
Discussion started by: kieranfoley
10 Replies

3. Shell Programming and Scripting

Passing variable from bash to perl script

Hi All, I need to pass a variable from bash script to perl script and in the perl script i am using those variables in the sql query but its giving error : Use of uninitialized value $ENV{"COUNTRYCD"} in concatenation (.) or string at /GIS_ROOT/custom/tables/DBread_vendor.pl line 50. Can ... (6 Replies)
Discussion started by: NileshJ
6 Replies

4. Shell Programming and Scripting

Passing arguments to a bash script

Hi, I wanted to pass an argument to a bash script. So that the argument is used inside the awk command inside the bash script. I know the noraml way of passing argument to a bash script as below : sh myScript.sh abc Inside the bash script i can use like this myArg1=$1 wc $myArg But... (8 Replies)
Discussion started by: shree11
8 Replies

5. Shell Programming and Scripting

Passing string from SQL to a BASH script

OS Solaris 10, DB oracle 10g Hello, We currently have a BASH script that runs and moves image files from a remote server to the local db server. A snippet of the code shows that we are picking up all Images that are 'mtime -1' some code... for file in `ssh user@10.200.200.10 'find... (3 Replies)
Discussion started by: JonP
3 Replies

6. Shell Programming and Scripting

Reading command line options from bash script

I have the following code and I am calling it using ./raytrac.bash -u and getting problems. For some reason opt_usage is still 0. opt_usage=0 iarg=0 narg=$# while (($iarg < $narg)) do (( iarg = $iarg + 1 )) arg=$argv usrInputFlag=`echo $arg | awk '/=/ {print 1}; ! /=/... (22 Replies)
Discussion started by: kristinu
22 Replies

7. UNIX for Advanced & Expert Users

how to add new options in bash script

what i want to be is that i would like to reinvent new options that have the same functionality as ... -u is the same functionality of conv=ucase and -l have the same functionality as conv=lcase... is there a way that i can put these in my script so that whenever i will typed in command... (12 Replies)
Discussion started by: aphryllyn1
12 Replies

8. Shell Programming and Scripting

bash script to compile multiple .c files with some options

I'm trying to write a bash script and call it "compile" such that running it allows me to compile multiple files with the options "-help," "-backup," and "-clean". I've got the code for the options written, i just can't figure out how to read the input string and then translate that into option... (5 Replies)
Discussion started by: travis.batzer
5 Replies

9. Shell Programming and Scripting

Passing options into a script

Afternoon all, I have been writing a script to do some selects on a table dependent on what options are selected when the script is run: #!/bin/ksh set -x set -m if then echo "usage: msglog.ksh -da <date and time> -i <interface> -m <msg> -di <direction> -mi <MIR>" exit 1 fi... (3 Replies)
Discussion started by: chris01010
3 Replies

10. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies
Login or Register to Ask a Question