Solved

Bash script question (about execution)

Posted on 2007-03-30
4
496 Views
Last Modified: 2008-01-09
Hello
I want to make a bash script lets say the name will be: getpsy
I want to give some parameters when i will execute it, something like: getpsy -p 1234 -lang EN -l user pass -d directory
How can i read them in some variables, like the one from -p-> 1234 in $port? (-p means port, -lang means language, -l means login and password, -d the  directory)

Thanx
0
Comment
Question by:rares_dumitrescu
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 250 total points
ID: 18823465

man getopts

Example script parses and displays its arguments:

aflag=
bflag=
while getopts ab: name
do
    case $name in
    a)    aflag=1;;
    b)    bflag=1
          bval="$OPTARG";;
    ?)   printf "Usage: %s: [-a] [-b value] args\n" $0
          exit 2;;
    esac
done
if [ ! -z "$aflag" ]; then
    printf "Option -a specified\n"
fi
if [ ! -z "$bflag" ]; then
    printf 'Option -b "%s" specified\n' "$bval"
fi
shift $(($OPTIND - 1))
printf "Remaining arguments are: %s\n" "$*"

0
 
LVL 8

Assisted Solution

by:eager
eager earned 250 total points
ID: 18824076
Unfortunately, getops restricts arguments to be single characters.  
You can parse the command line manually.  The following code picks up each argument which starts with a '-'.  Arguments which do not start with '-' are saved in $OPTS.

while [ $# -gt 0 ] ; do
  case $1 in
    "-p"       )
        port=$2; shift; shift ;;
    "-lang" )
        lang=$2; shift; shift ;;
< etc.  ... for other args >
    -*                       )
      echo "Unrecognized option: $1"; exit ;;
    * )
      OPTS="$OPTS $1" ; shift ;;
  esac
done
0
 

Author Comment

by:rares_dumitrescu
ID: 18824746
eager .. and if i have -l user pass .. how will i put them?
i mean
"-l" )
    login= ?
and another thing .. what does shift stand for ? thanx
0
 
LVL 8

Expert Comment

by:eager
ID: 18825012
"-l" )
  login=$2;
  pass=$3;
  shift; shift; shift
  ;;

Arguments are stored in variables named $1, $2, etc.  The 'shift'' command removes $1 and shifts all of the other arguments left.  Essentially, $1 = $2; $2 = $3, $3 = $4, etc.  

The code snippet looks at the first argument ($1) and checks if it matches one of the options.  If it does, the values for the option are the following arguments ($2, $3).  Once those values are copied, the first shift gets rid of the option, the second (and third) shift eliminates the value(s).

Recommended:  Learning the Bash Shell, by Cameron Newham and Bill Rosenblat, O'Reilly books.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IPtables not running - RHEL7 64bit 5 246
inotifywait in linux 2 285
init script for deployment on linux machine 5 53
making an rpm that just copies the entire project 2 56
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question