Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Bash script question (about execution)

Posted on 2007-03-30
4
Medium Priority
?
500 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 1000 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 1000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

636 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