Solved

Bash script question (about execution)

Posted on 2007-03-30
4
493 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A short film showing how OnPage and Connectwise integration works.

914 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now