Link to home
Start Free TrialLog in
Avatar of rares_dumitrescu
rares_dumitrescu

asked on

Bash script question (about execution)

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
ASKER CERTIFIED SOLUTION
Avatar of MikeOM_DBA
MikeOM_DBA
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rares_dumitrescu
rares_dumitrescu

ASKER

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
"-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.