Solved

Bash script question (about execution)

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

13 Experts available now in Live!

Get 1:1 Help Now