bash if loop for checking multiple parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash if loop for checking multiple parameters
# 1  
Old 02-23-2009
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


# 2  
Old 02-23-2009
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

# 3  
Old 02-23-2009
Quote:
Originally Posted by szittyafergeteg
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking File record equal to multiple of 70 or nearest number to multiple of 70

Hello, I have a file with below content - Example 3 6 69 139 210 345 395 418 490 492 I would like the result as - Multiple of 70 or nearest number in the file less than the multiple of 70 69 139 (5 Replies)
Discussion started by: Mannu2525
5 Replies

2. Shell Programming and Scripting

Loop through multiple files in bash script

Hi Everybody, I'm a newbie to shell scripting, and I'd appreciate some help. I have a bunch of .txt files that have some unwanted content. I want to remove lines 1-3 and 1028-1098. #!/bin/bash for '*.txt' in <path to folder> do sed '1,3 d' "$f"; sed '1028,1098 d' "$f"; done I... (2 Replies)
Discussion started by: BabyNuke
2 Replies

3. Shell Programming and Scripting

Record count checking for multiple files through for-loop

Hi Friends, I wrote one shell script to check the record count in two files and that will send us the notification activity if found zero record count. What i did is I created for loop and checking the count for both of the files but what is happening is for first file has data then it's... (13 Replies)
Discussion started by: victory
13 Replies

4. Shell Programming and Scripting

Detail on For loop for multiple file input and bash variable usage

Dear mentors, I just need little explanation regarding for loop to give input to awk script for file in `ls *.txt |sort -t"_" -k2n,2`; do awk script $file done which sorts file in order, and will input one after another file in order to awk script suppose if I have to input 2 or... (4 Replies)
Discussion started by: Akshay Hegde
4 Replies

5. UNIX for Dummies Questions & Answers

Problem with multiple grep in bash loop

Hello, I am trying to create a matrix of 0's and 1's depending on whether a gene and sample name are found in the same line in a file called results.txt. An example of the results.txt file is (tab-delimited): Sample1 Gene1 ## Gene2 ## Sample2 Gene2 ## Gene 4 ## Sample3 Gene3 ... (2 Replies)
Discussion started by: InfoSeeker2
2 Replies

6. Shell Programming and Scripting

Multiple condition checking in bash

Hi All, I am trying to check if two variables have value assigned to it. i am doing it like if ] then echo "Please specify either single hostname or host file for the report" usage exit fi But its not working for it.Even i specify values for both variables it dont go... (6 Replies)
Discussion started by: kailash19
6 Replies

7. Shell Programming and Scripting

checking the positional parameters

Hi all, I have one small requirment... I have prepared one script. we have to pass two possitional parameters to the script. What I want to do is if the parameters are not passed then i dont want the script to start the process... For ex: $ ./a.sh parm1 parm2 #Here, it can start... (7 Replies)
Discussion started by: raghu.iv85
7 Replies

8. Shell Programming and Scripting

for loop logic with multiple parameters

hi, unix wizards, i have a question about the logic of my inner for loop below. first, what i am trying to do is to write a script called create_account that automatically creates mysql accounts. the user can provide a user_name or a group_id as an argument (and the script can take multiple... (1 Reply)
Discussion started by: ankimo
1 Replies

9. Shell Programming and Scripting

For Loop with Strings as parameters

I'm trying to create a script with a for loop that take strings in as the parameters. Some of the strings have spaces in them, and I can't get it to work properly. For example: #!/bin/ksh INFILE=snapshot.log OUTFILE=summary.out a="Lock waits" b="Deadlocks detected" for PARAM in... (6 Replies)
Discussion started by: kadishmj
6 Replies

10. Shell Programming and Scripting

Parameters in loop

Hi, I am trying to write a script which will read inputs form user and process those files, I have issue reading the input parameters in a loop. Following is the script... I run the script as ./Script.sh 3 table1 table 2 table3 NumberOfTables=$1 let TableCount=1 while do ... (3 Replies)
Discussion started by: mgirinath
3 Replies
Login or Register to Ask a Question