Sponsored Content
Top Forums Shell Programming and Scripting bash if loop for checking multiple parameters Post 302290515 by jaduks on Monday 23rd of February 2009 01:27:58 PM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
getoptcvt(1)							   User Commands						      getoptcvt(1)

NAME
getoptcvt - convert to getopts to parse command options SYNOPSIS
/usr/lib/getoptcvt [-b] filename /usr/lib/getoptcvt DESCRIPTION
/usr/lib/getoptcvt reads the shell script in filename, converts it to use getopts instead of getopt, and writes the results on the standard output. getopts is a built-in Bourne shell command used to parse positional parameters and to check for valid options. See sh(1). It supports all applicable rules of the command syntax standard (see Rules 3-10, intro(1)). It should be used in place of the getopt command. (See the NOTES section below.) The syntax for the shell's built-in getopts command is: getopts optstring name [ argument...] optstring must contain the option letters the command using getopts will recognize; if a letter is followed by a colon (:), the option is expected to have an argument, or group of arguments, which must be separated from it by white space. Each time it is invoked, getopts places the next option in the shell variable name and the index of the next argument to be processed in the shell variable OPTIND. Whenever the shell or a shell script is invoked, OPTIND is initialized to 1. When an option requires an option-argument, getopts places it in the shell variable OPTARG. If an illegal option is encountered, ? will be placed in name. When the end of options is encountered, getopts exits with a non-zero exit status. The special option -- may be used to delimit the end of the options. By default, getopts parses the positional parameters. If extra arguments (argument ...) are given on the getopts command line, getopts parses them instead. So that all new commands will adhere to the command syntax standard described in intro(1), they should use getopts or getopt to parse posi- tional parameters and check for options that are valid for that command (see the NOTES section below). OPTIONS
The following option is supported: -b Makes the converted script portable to earlier releases of the UNIX system. /usr/lib/getoptcvt modifies the shell script in file- name so that when the resulting shell script is executed, it determines at run time whether to invoke getopts or getopt. EXAMPLES
Example 1: Processing the arguments for a command The following fragment of a shell program shows how one might process the arguments for a command that can take the options -a or -b, as well as the option -o, which requires an option-argument: while getopts abo: c do case $c in a | b) FLAG=$c;; o) OARG=$OPTARG;; ?) echo $USAGE exit 2;; esac done shift `expr $OPTIND - 1` Example 2: Equivalent code expressions This code accepts any of the following as equivalent: cmd -a -b -o "xxx z yy" filename cmd -a -b -o "xxx z yy" -filename cmd -ab -o xxx,z,yy filename cmd -ab -o "xxx z yy" filename cmd -o xxx,z,yy b a filename ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of getopts: LC_CTYPE, LC_MESSAGES, and NLSPATH. OPTIND This variable is used by getoptcvt as the index of the next argument to be processed. OPTARG This variable is used by getoptcvt to store the argument if an option is using arguments. EXIT STATUS
The following exit values are returned: 0 An option, specified or unspecified by optstring, was found. >0 The end of options was encountered or an error occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ |CSI |enabled | +-----------------------------+-----------------------------+ SEE ALSO
intro(1), getopts(1), sh(1), shell_builtins(1), getopt(3C), attributes(5) DIAGNOSTICS
getopts prints an error message on the standard error when it encounters an option letter not included in optstring. NOTES
Although the following command syntax rule (see intro(1)) relaxations are permitted under the current implementation, they should not be used because they may not be supported in future releases of the system. As in the EXAMPLES section above, -a and -b are options, and the option -o requires an option-argument. The following example violates Rule 5: options with option-arguments must not be grouped with other options: example% cmd -aboxxx filename The following example violates Rule 6: there must be white space after an option that takes an option-argument: example% cmd -ab oxxx filename Changing the value of the shell variable OPTIND or parsing different sets of arguments may lead to unexpected results. SunOS 5.10 7 Jan 2000 getoptcvt(1)
All times are GMT -4. The time now is 10:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy