bash if loop for checking multiple parameters | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

bash if loop for checking multiple parameters

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-23-2009
szittyafergeteg szittyafergeteg is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 8 February 2011, 7:13 AM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb bash if loop for checking multiple parameters

Hello,

I've got next problem:
I want to examine at the beginning of a script in an if loop that:
1. Is there 4 parameters given
2. If first state is true then: is there switches -e and -d?
3. At the end, how can i indentify them as variebles regardlees to its order.

I was thinking like this if [ "$#" == 4 && "$1" == -e && "$3" == -d ] ; then ...

Can anyone help me?
If loop could be here the right choice, at all?

Regards


Sponsored Links
    #2  
Old 02-23-2009
jaduks's Avatar
jaduks jaduks is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 5 January 2012, 7:47 AM EST
Location: India
Posts: 185
Thanks: 0
Thanked 5 Times in 3 Posts
You can use getopts here.

e.g.


Code:
[ $# -ne 4 ] && <some usage function> && exit

while getopts e:d: OPTION
do
    case $OPTION in
        e)  somevar=$OPTARG ;;
        d)  somevar1=$OPTARG ;;
    esac
done

Sponsored Links
    #3  
Old 02-23-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 31 August 2014, 7:32 PM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts
Quote:
Originally Posted by szittyafergeteg View Post
I've got next problem:
I want to examine at the beginning of a script in an if loop that:

There is no such thing as an if loop; if chooses between one or more sets of commands to execute depening on one or more conditions.
Quote:
1. Is there 4 parameters given

Code:
if [ $# -eq 4 ]
then
   : do whatever
else
  printf "4 parameters required\n" >&2
  exit 1 ## or whatever
fi

Quote:
2. If first state is true then: is there switches -e and -d?

They are called options, not switches.
Quote:
3. At the end, how can i indentify them as variebles regardlees to its order.

Regard what as variables?
Quote:

I was thinking like this if [ "$#" == 4 && "$1" == -e && "$3" == -d ] ; then ...

Can anyone help me?
If loop could be here the right choice, at all?

As jaduks pointed out, the usual method is to use getopts in a while loop.

However, it is easy enough to roll your own:


Code:
while [ $# -gt 0 ]
do
  case $1 in
    -e) var_e=$2
        shift 2
        ;;
    -d) var_d=$2
        shift 2
        ;;
     *) shift ;; ## ignore other arguments
  esac
done

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
for loop logic with multiple parameters ankimo Shell Programming and Scripting 1 10-09-2008 06:39 AM
For Loop with Strings as parameters kadishmj Shell Programming and Scripting 6 10-06-2008 03:57 AM
bash and ksh: variable lost in loop in bash? estienne Shell Programming and Scripting 2 08-25-2008 02:09 PM
Parameters in loop mgirinath Shell Programming and Scripting 3 03-18-2008 10:20 AM
BASH: how to launch a program with parameters TinCanFury Shell Programming and Scripting 5 03-15-2008 03:02 AM



All times are GMT -4. The time now is 03:57 PM.