Sponsored Content
Top Forums Shell Programming and Scripting Errors in if conditions with to many OR conditions Post 302736957 by Don Cragun on Wednesday 28th of November 2012 07:49:39 AM
Old 11-28-2012
Quote:
Originally Posted by bipinajith
Don Cragun, I see quoting the variables is not helping:-
Code:
# cat test.sh
#!/bin/bash

var1=100
var3=1500

if [ "$var1" -gt 1500 -o "$var2" -gt 1000 -o "$var3" -gt 1000 ]
then
    echo "Success"
else
    echo "Failed"
fi

Code:
# ./test.sh
./test.sh: line 6: [: : integer expression expected
Failed

Code:
# uname
Linux GNU/Linux
# echo $SHELL
/bin/bash

Sorry, I should have said it wasn't a portable fix.

It works with ksh on OS X, but comparing an empty string using a numeric test operator is not required to work. However, I believe that even though it didn't give you what you wanted with bash on Linux, the diagnostic message would be easier to interpret. (At least I think it would be easier to try to find out what to look for when test says it expected an integer expression than it was to figure out what was wrong when it said too many arguments and meant that an unset variable caused a test operator to be misinterpreted as an operand.)

Of course the underlying problem of using variables that don't contain data of the type expected when they are expanded can only be fixed by verifying the format of data before it is used. And, we weren't shown the first 49 lines of the script that failed to set or verify that the variables used in this test contained numeric string values.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

multiple conditions in if/then

Hello, I am having trouble with the syntax with a conditional statement in a BASH script involving multiple conditions. Any suggestions would be greatly appreciated! if ; then array=("${array}" "$dnNum" ) fi i receive this error: ./testscript: ' (4 Replies)
Discussion started by: grandtheftander
4 Replies

2. UNIX for Dummies Questions & Answers

2 or more if conditions

Hello, I have a file as follows: col no:1 2 3 4 5 6 7 8 9 10 11 a 4 226 226 ch:95024048-95027592, 1y224 of 3545 223 224 ident b 53 235 235 ch:148398-148401255, 1y184 of 3187 180 186 ident awk... (3 Replies)
Discussion started by: dr_sabz
3 Replies

3. Shell Programming and Scripting

conditions

./script 89 The script will extract the last digit of the input parameter. example, that is 4. This will be compared to the last digit of the current day of the month ( like day 14; that is 4). A message will displayed on the screen indicating if the digits are the same or not. (1 Reply)
Discussion started by: singh is king
1 Replies

4. Shell Programming and Scripting

While with three conditions

Currently this is what I am trying while || && ]; do I want to continue if the first condition or both the second and third are true but I am getting a too many arguments error. Can someone help me out? (5 Replies)
Discussion started by: whdr02
5 Replies

5. Shell Programming and Scripting

IF OR with two conditions

I have this IF working fine, testing if a char is a digit: if ; then _VALUE=$_VALUE$_CHAR else _ISDIGIT="false" fi Then I add a second condition to test if the char is either a digit or a * if ]; then _VALUE=$_VALUE$_CHAR ... (11 Replies)
Discussion started by: Flavius
11 Replies

6. Shell Programming and Scripting

If conditions need

Dear Expert, Below code is for to take the backup of database by daily time stamp. I need vital help to make my script automatic sending me email if it sucess or fail. echo on @REM Seamonkey's quick date batch (MMDDYYYY format) @REM Setups %date variable @REM First parses month, day, and... (6 Replies)
Discussion started by: Alone
6 Replies

7. Shell Programming and Scripting

awk three conditions

I'm having a problem pulling UID's from data. The data outputs a user's UID in one of three ways: 1. Error User user_name already assigned with <UID> 2. Success <UID> reserved for user_name 3. <a load of crap because there was a db failure yet somehow the UID is still in there> I typically... (5 Replies)
Discussion started by: MaindotC
5 Replies

8. Shell Programming and Scripting

Errors in if conditions.....

#if if then echo $varNO >> AgriN.csv fi done < data The above script throws error such as integer expression expected. How do i rectify that?? (4 Replies)
Discussion started by: nikhil jain
4 Replies

9. Shell Programming and Scripting

Conditions in if

I'm using the below one.. #!/bin/ksh File=$3 if ; then echo "Script" elif ] ;then echo "Passed k or f option" else "Please check the Input passed" fi Command line argument is "k" or -f and file is exist then... (3 Replies)
Discussion started by: Roozo
3 Replies

10. Shell Programming and Scripting

How to match the below conditions?

Hello All, I have 2 files with around 2k records.. I am lost how to build a script to create another file with the below:confused::eek: Match Condition1: File1's 12th Columns data should exact match with File2's 19th Column's data Match Condition2: File1's 28th Column's data format is... (12 Replies)
Discussion started by: ailnilanjan
12 Replies
trap(1) 							   User Commands							   trap(1)

NAME
trap, onintr - shell built-in functions to respond to (hardware) signals SYNOPSIS
sh trap [argument n [n2]...] csh onintr [-| label] ksh *trap [arg sig [sig2...]] ksh93 +trap [-p] [action condition...] DESCRIPTION
sh The trap command argument is to be read and executed when the shell receives numeric or symbolic signal(s) (n). (Note: argument is scanned once when the trap is set and once when the trap is taken.) Trap commands are executed in order of signal number or corresponding symbolic names. Any attempt to set a trap on a signal that was ignored on entry to the current shell is ineffective. An attempt to trap on signal 11 (memory fault) produces an error. If argument is absent all trap(s) n are reset to their original values. If argument is the null string this signal is ignored by the shell and by the commands it invokes. If n is 0 the command argument is executed on exit from the shell. The trap command with no arguments prints a list of commands associated with each signal number. csh onintr controls the action of the shell on interrupts. With no arguments, onintr restores the default action of the shell on interrupts. (The shell terminates shell scripts and returns to the terminal command input level). With the - argument, the shell ignores all inter- rupts. With a label argument, the shell executes a goto label when an interrupt is received or a child process terminates because it was interrupted. ksh trap uses arg as a command to be read and executed when the shell receives signal(s) sig. arg is scanned once when the trap is set and once when the trap is taken. Each sig can be specified as a number or as the name of the signal. trap commands are executed in order of signal number. Any attempt to set a trap on a signal that was ignored on entry to the current shell is ineffective. If arg is omitted or is -, then the trap(s) for each sig are reset to their original values. If arg is the null (the empty string, for example, "") string then this signal is ignored by the shell and by the commands it invokes. If sig is ERR then arg are executed whenever a command has a non-zero exit status. If sig is DEBUG then arg are executed after each command. If sig is 0 or EXIT for a trap set outside any function then the command arg is executed on exit from the shell. The trap command with no arguments prints a list of commands associated with each signal number. On this manual page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a variable assignment. This means that tilde substitution is performed after the = sign and word splitting and file name genera- tion are not performed. ksh93 trap is a special built-in that defines actions to be taken when conditions such as receiving a signal occur. trap can also be used to dis- play the current trap settings on standard output. If action is -, trap resets each condition to the default value. If action is an empty string, the shell ignores each of the conditions if they arise. Otherwise, the argument action is read and executed by the shell as if it were processed by eval when one of the corresponding conditions arise. The action of the trap overrides any previous action associated with each specified condition. The value of $? is not altered by the trap execution. condition can be the name or number of a signal, or one of the following: EXIT Execute this trap when the shell exits. If defined within a function with the function reserved word, executes the trap in the caller's environment when the function returns. The trap action is restored to the value it had when it called the function. 0 Same as EXIT. DEBUG Execute before each simple command is executed but after the arguments are expanded. ERR Execute whenever set -e would cause the shell to exit. KEYBD Execute when a key is entered from a terminal device. Signal names are case insensitive and the sig prefix is optional. Signals that were ignored on entry to a non-interactive shell cannot trapped or reset although doing so does not report an error. The use of signal numbers other than 1, 2, 3, 6, 9, 14, and 15 are not porta- ble. Although trap is a special built-in, specifying a condition that the shell does not know about causes trap to exit with a non-zero exit status, but does not terminate the invoking shell. If no action or conditions are specified then all the current trap settings are written to standard output. The following options are supported by the trap built-in command in ksh93: -p Causes the current traps to be output in a format that can be processed as input to the shell to recreate the current traps. The trap built-in in ksh93 exits with one of the following values: 0 Successful completion. >0 An error occurred. On this manual page, ksh93(1) commands that are preceded by one or two + (plus signs) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. They are not valid function names. 5. Words, following a command preceded by ++ that are in the format of a variable assignment, are expanded with the same rules as a variable assignment. This means that tilde substitution is performed after the = sign and word splitting and file name genera- tion are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
csh(1), eval(1), exit(1), ksh(1), ksh93(1), sh(1), attributes(5) SunOS 5.11 20 Nov 2007 trap(1)
All times are GMT -4. The time now is 01:20 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy