Unexpected output in bash shell script - linux

For the below script I am expecting the output to be msg_y and msg_z. But it is printing msg_x and msg_z. Can somebody explain to me what is going on?
#!/bin/bash
set -x
vr=2
echo $vr
if [ $vr > 5 ]
then
echo "entered 1st if"
echo "msg_x"
echo "out of 1st if"
if [ $vr < 8 ]; then
echo "in of 2nd if"
echo "msg_y"
else
echo "msg_z"
fi
else
if [ $vr > 1 ]; then echo "msg_y"
else echo "msg_z"
fi
fi

This expression
[ $vr > 5 ]
is being parsed as an output redirection; check to see if you have a file named "5" now. The output redirection is vacuously true. Note that the usual admonition to quote parameters inside a test expression would not help here (but it's still a good idea).
You can escape the > so that it is seen as an operator in the test command:
if [ "$vr" \> 5 ]; then
or you can use the integer comparison operator -gt
if [ "$vr" -gt 5 ]; then.
Since you are using bash, you can use the more robust
conditional expression
if [[ $vr > 5 ]]; then
or
if [[ $vr -gt 5 ]]; then
or use an arithmetic expression
if (( vr > 5 )); then
to do your comparisions (likewise for the others).
Note: although I showed how to make > work as a comparison operator even when surrounded by integers, don't do this. Most of the time, you won't get the results you want, since the arguments are compared lexicographically, not numerically. Try [ 2 \> 10 ] && echo What? Either use the correct integer comparison operators (-gt et al.) or use an arithmetic expression.

Related

Read string and convert to INT (BASH)

I have a simple script in Bash to read a number in a file and then compare it with a different threshold. The output is this:
: integer expression expected
: integer expression expected
OK: 3
My code is this:
#!/bin/bash
wget=$(wget http://10.228.28.8/ -O /tmp/wget.txt 2>/dev/null)
output=$(cat /tmp/wget.txt | awk 'NR==6')
#output=7
echo $output
if [ $output -ge 11 ];then
echo "CRITICAL: $output"
exit 2
elif [ $output -ge 6 ] && [ $output -lt 11 ];then
echo "WARNING: $output"
exit 1
else
echo "OK: $output"
exit 0
fi
rm /tmp/wget.txt
I know what is the problem, I know that I'm reading a string and I try to compare a int. But I don't know how can I do to read this file and convert the number to read in a int var..
Any ideas?
The problem occurs when $output is the empty string; whether or not you quote the expansion (and you should), you'll get the integer expression required error. You need to handle the empty string explictly, with a default value of zero (or whatever default makes sense).
wget=$(wget http://10.228.28.8/ -O /tmp/wget.txt 2>/dev/null)
output=$(awk 'NR==6' < /tmp/get.txt)
output=${output:-0}
if [ "$output" -ge 11 ];then
echo "CRITICAL: $output"
exit 2
elif [ "$output" -ge 6 ];then
echo "WARNING: $output"
exit 1
else
echo "OK: $output"
exit 0
fi
(If you reach the elif, you already know the value of $output is less than 11; there's no need to check again.)
The problem also occurs, and is consistent with the error message, if output ends with a carriage return. You can remove that with
output=${output%$'\r'}
There are a couple of suggestions from my side regarding your code.
You could explicitly tell bash the output is an integer
declare -i output # See [1]
Change
output=$(cat /tmp/wget.txt | awk 'NR==6') # See [2]
may be better written as
output=$(awk 'NR==6' /tmp/wget.txt )
Change
if [ $output -ge 11 ]
to
if [ "0$output" -ge 11 ] # See [4]
or
if (( output >= 11 )) # Better See [3]
References
Check bash [ declare ].
Useless use of cat. Check [ this ]
Quoting [ this ] answer :
((...)) enable you to omit the dollar signs on integer and array variables and include spaces around operators for readability. Also empty variable automatically defaults to 0 in such a statement.
The zero in the beginning of "0$output" help you deal with empty $output
Interesting
Useless use of cat is a phrase that has been resounding in SO for long. Check [ this ]
[ #chepner ] has dealt with the empty output fiasco using [ bash parameter expansion ] in his [ answer ], worth having a look at.
A simplified script:
#!/bin/bash
wget=$(wget http://10.228.28.8/ -O /tmp/wget.txt 2>/dev/null)
output=$(awk 'NR==6' </tmp/wget.txt )
output="$(( 10#${output//[^0-9]} + 0 ))"
(( output >= 11 )) && { echo "CRITICAL: $output"; exit 2; }
(( output >= 6 )) && { echo "WARNING: $output"; exit 1; }
echo "OK: $output"
The key line to cleanup any input is:
output="$(( 10#${output//[^0-9]} + 0 ))"
${output//[^0-9]} Will leave only digits from 0 to 9 (will remove all non-numeric chars).
10#${output//[^0-9]} Will convert output to a base 10 number.
That will correctly convert numbers like 0019
"$(( 10#${output//[^0-9]} + 0 ))" Will produce a zero for a missing value.
Then the resulting number stored in output will be compared to limits and the corresponding output will be printed.
In BASH, It is a good idea to use double brackets for strings:
if [[ testing strings ]]; then
<whatever>
else
<whatever>
fi
Or double parenthesis for integers:
if (( testing ints )); then
<whatever>
else
<whatever>
fi
For example try this:
var1="foo bar"
if [ $var1 == 'foo bar' ]; then
echo "ok"
fi
Result:
$ bash: [: too many arguments
Now, this:
var2="foo bar"
if [[ $a == "foo bar" ]]; then
echo "ok"
fi
Result:
ok
For that, your code in BASH:
if [[ $output -ge 11 ]]; then
echo "CRITICAL: $output"
exit 2
elif [[ $output -ge 6 ]]; then
echo "WARNING: $output"
exit 1
else
echo "OK: $output"
exit 0
fi

Bash: Why does my If-Statement always evaluate to true?

I'm trying to write what would seem to be a simple if statement in most languages, however in bash this doesnt seem to work at all.
When I run the script it always enters the first if statement. Can anyone offer help me as to what I am doing wrong?
PERC=.5
if [ "$PERC" > "1.00" ]
then
echo "Entered first statement"
else
if [ "$PERC" < "1.00" ]
then
echo "Entered second statement"
fi
fi
Thanks for your help.
> and < compare strings, not numbers (and must be backslashed or quoted in single [...]). Use -gt, -lt etc. to compare numbers, or use arithmetic conditions:
if (( a < b || b <= c )) ; then
Note, however, that bash only handles integer arithmetics. To compare floats, you can use bc:
if [[ 1 == $( bc <<< '1.5 < 1.00' ) ]] ; then
> and < are the I/O redirection operators. So
if [ "$PERC" > "1.0" ]
is executing the command [ "$PERC ], redirecting the output to the file 1.0, and then if is testing whether the command succeeded. [ "$PERC" ] simply tests whether "$PERC" is a non-empty string.
To use them as operators in the test command, you need to quote or escape them:
if [ "$PERC" '>' "1.0" ]
You could also use bash's [[ conditional syntax instead of the [ command:
if [[ $PERC > "1.0" ]]

Detect number of argument and the value passed into the bash

I want to make sure my bash script can correctly detect user's input argument. Specifically, user can only pass 1 or 2 or 3 into the script, otherwise the script will be terminated. I wrote the following code:
#!/bin/bash
for args in $#
do
echo $args
done
if [ "$#" != 1 ] && [ "$#" -ne 1 ]; then
echo "Illegal number of parameters"
exit 1
fi
This script can only capture when user does not give any input, but cannot check whether user indeed input the number 1 not other values.
By the way, I am not sure how to express "input argument can accept number 1 or 2 or 3".
$# is an integer, so you have to use integer comparison. You can for example say:
if [ "$#" -ne 1 ]; then
echo "illegal number of parameters"
exit 1
fi
To check that the parameter is either 1, 2 or 3, you can use this regular expression (see something related):
if [[ ! $1 =~ ^(1|2|3)$ ]]; then
echo "the number is not 1, 2 or 3"
exit 1
fi
To express "input argument can accept number 1 or 2 or 3" I would for example say "we can just accept the argument being either 1, 2 or 3".
First off you can detect if the string is null or empty simply by doing the following:
if [ -z "$1" ]
then
echo "Argument $1 contains nothing"
fi
That I would say is your first step, and will allow you to filter out args that have no content.
Following on from that, you'd most likely need to do some comparison work on $1, $2 & $3
I'll just check something and come back to this in a moment.
Update
Just had to go find one of my scripts and check something... :-)
One way I've handled the checking of parms in the past is something like the following
#!/bin/sh
while [ $# -gt 0 ] || [ "$#" -le 4 ]; do
case "$1" in
*[!1-9]*) echo "Text: $1";;
*) echo "Number: $1"
esac
case "$2" in
*[!1-9]*) echo "Text: $2";;
*) echo "Number: $2"
esac
case "$3" in
*[!1-9]*) echo "Text: $3";;
*) echo "Number: $3"
esac
shift
done
Basically a simple regex, if I have more than 0 parameters or less than 4 parameters then I allow it through to a case statement, which then checks the content of each parameter.
This one just has an echo in, but you could just as easy set some flags, and then decide how to continue based on those flags.
For simple range checking however, you might just want to use a one liner similar to the following:
if [[ $# -gt 0 && $# -lt 4 ]]; then echo "Correct number of parameters"; fi
Again setting a flag to use later rather than echoing the results.
I assume you mean the input can only be 1, 2 or 3, so using a case statement is the best way. $1 is the variable that stores your argument, if it is equal to 1 case will execute the code in the block corresponding to the value 1 and so on.
case "$1" in
1) ...
;;
2) ...
;;
3) ...
;;
*) echo "Invalid argument"
;;
esac

Why does [$value -lt 10] in shell result in command not found? [duplicate]

This question already has answers here:
"Command not found" when attempting integer equality in bash
(3 answers)
Closed 8 years ago.
Output 1:
Enter your value: 12
./testscript.sh: line 4: 12: command not found
Your value is more than 10
Output 2:
Enter your value: 5
./testscript.sh: line 4: 12: command not found
Your value is more than 10
I need to know what is wrong. My Linux test is just tomorrow:(
#!/bin/bash
echo -n "Enter your value: "
read value
if [$value -lt 10]
then
echo "Your value is less than 10"
else
echo "Your value is more than 10"
fi
your if statement should be as below. A space before ']' and space after '['
if [ $value -lt 10 ]
EDIT
As per the comments
you can always add optional ; at the end of the line. The below script would work fine.
a=20;
if [ $a -gt 10 ];
then
echo "true";
else
echo "false";
fi
you need to put your condition in the [ condition ]. The below one does not work.
( condition )
It would say command not found. As '(' is not command. where as '[' is a command in order to check a condition
And the more appropriate way is to use [[ ]] over [ ] when in Bash since you can avoid word splitting and pathname expansion with it. Other conditions can be added as well:
if [[ ! $value =~ [0-9]+ ]]; then
echo "Invalid input."
elif [[ value -lt 10 ]]; then
echo "Your value is less than 10."
elif [[ value -eq 10 ]]; then
echo "Your value is 10."
else
echo "Your value is more than 10."
fi
To throw yet another option into the mix: bash has (( ... )) -- arithmetic conditions:
if (( $value < 10 )); then ...
http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs
One thing you can do in bash with arithmetic expressions is to drop the $.
if (( value < 10 )); then
This is documented a bit obscurely in Shell arithmetic: "Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax."
This may appeal to aficionados of C-like languages, but it's a bit out of step with the rest of the languages, and it doesn't apply to all variables (such as special paramaters ($#)
and array elements (${foo[3]})).

Test if string is "*"

For some reason I just cannot get an if statement to test if a string is literally equal to an asterisk. I have tried every combination I can think of and I don't want to mess with file globbing. Please help.
if [ $VAR = "\*" ]; then
* UPDATE *
Both of those suggestions work. The issue is apparently not with the * comparison, but with the other part of the if statement. This is supposed to compare whether or not $VAR is between 0 and 20 or is a wildcard.
if [ "$VAR" -gt 0 ] && [ "$VAR" -lt 20 ] || [ "$VAR" = "*" ]; then
This other part of the IF statement if apparently goofing up the last comparison.
* UPDATE *
Just tested it again and checked my syntax. When $VAR is between 0 and 20 it works great (true), when $VAR is over 20 it also works (reports false), however as soon as I try to set $VAR to an * the if statement freaks and pops out:
line 340: [: *: integer expression expected
Another version using bash's double brackets:
if [[ $VAR = "*" || ($VAR -gt 0 && $VAR -lt 20) ]]; then
The double brackets allow you to use && and ||. Also, bash doesn't perform word splitting or glob expansion on arguments to [[, so $VAR doesn't need to be quoted and ( doesn't need to be escaped.
[[ also works in zsh and ksh, if you need (some) portability.
$ VAR="*"
$ if [ "$VAR" = "*" ] ; then echo Star ; fi
Star
Quote variables when they could contain glob patterns, whitespace or other interpretable sequences you don't want interpreted. This also avoids syntax errors if $VAR is empty.
For your second problem, [ "$VAR" -gt 0 ] doesn't make sense if $VAR is anything but a number. So you must avoid having that test evaluated in that case. Simply exchange your tests - || and && are short-circuiting (in bash at least, not sure if that's POSIX):
if [ "$VAR" = "*" ] || [ "$VAR" -gt 0 -a "$VAR" -lt 20 ] ; then

Resources