Linux shell bash script if condition - linux

When I run this on my server it works.
if [[ "${Month}" -gt 12 ]] || [[ "${Day}" -gt 31 ]] || [[ "${Month}" -lt 0 ]] || [[ "${Day}" -lt 0 ]] || [[ "${Year}" -lt 0 ]] || [[ "${Year}" -gt 2050 ]]; then
echo "Please enter Valid date choice."
exit 1
fi
But when my customer runs in their environment it generates a syntax error.
bash-4.1$ ./port_generator.sh "/opt/device/server/scripts/Results" "2017-01-29"
./port_generator.sh: line 31: syntax error near unexpected token `||'
./port_generator.sh: line 31: `|| [[ "${Year}" -lt 0 ]] || [[ "${Year}" -gt 2050 ]]; then'
Release on server success is : Red Hat Enterprise Linux Server release 6.5 (Santiago)
Release on server fail is : Red Hat Enterprise Linux Server release 6.7 (Santiago)

Does your script have a shebang? What shell is the customer running the script in? Do you use the same shell?
netikras#netikras-xps ~ $ bash
netikras#netikras-xps ~ $ if [[ 3 -gt 1 ]]; then echo OK; fi
OK
netikras#netikras-xps ~ $ csh
% if [[ 3 -gt 1 ]]; then echo OK; fi
[[: No match.
% exit
Not all shells have the same syntax. Make sure you and your client are using the same shell. Or update the answer with more info

Your script should have a shebang line at the top: #!/bin/bash or #!/usr/bin/env bash. But that's not the problem. If it were, the error would point to the first ||, not the fourth one.
Do you have the if statement on two separate lines in your actual script? This would generate the message you're seeing:
if [[ "${Month}" -gt 12 ]] || [[ "${Day}" -gt 31 ]] || [[ "${Month}" -lt 0 ]] || [[ "${Day}" -lt 0 ]]
|| [[ "${Year}" -lt 0 ]] || [[ "${Year}" -gt 2050 ]]; then
If that's what you did, don't. Or if you do want the line split up, end the first line with || so that the parser knows the command isn't finished.
if [[ "${Month}" -gt 12 ]] || [[ "${Day}" -gt 31 ]] || [[ "${Month}" -lt 0 ]] || [[ "${Day}" -lt 0 ]] ||
[[ "${Year}" -lt 0 ]] || [[ "${Year}" -gt 2050 ]]; then

Related

How to compare number with string by -gt in shell programming

I don't know if exist a var $b ,make the value of the expression :
[[ "$a" -gt "$b" ]] && [[ "$b" -gt 88888888 ]]
is true?...
It's not terribly well documented (IIRC), but -gt (and the other integer comparison operators) forces the comparison to be made in an arithmetic context, which means an empty value of $b is treated the same as 0.
$ [[ 3 -gt $undefined ]] && echo true
true
$ [[ -3 -lt $undefined ]] && echo true
true
$ [[ 0 -eq $undefined ]] && echo true
true
This is more clearly seen using the fact that strings are recursively expanded as variables until an integer is found as well:
$ foo=bar
$ bar=3
$ [[ 3 -eq bar ]] && echo true
true
$ [[ 3 == bar ]] || echo false
false

Bash: Check if a string contain specific alphabets and comma

I am trying to parse and validate a string in Bash which is comma separated. The expected input is: X4,Y1,Z5
Conditions: The string should have only X,Y or Z alphabets, followed by any number. The string should not have any special characters other than comma. Please suggest.
X4,Y1,Z5 (This is OK)
Z2,y6,X1 (This is OK)
X3Y6,Z8 (This is not OK)
A1,B2,X8 (This is not OK)
N1P8* (This is not OK)
I have tried the following but this is not working as expected.
if [[ ! $str =~ ['!##$%^&*()_+'] ]] && [[ $str =~ [XYZxyz] ]]; then
echo "OK"
else
echo "Not OK"
fi
I suppose there are additional conditions of the problem that were implied but not emphasized, such as:
The numbers may have more then one digit.
Each of X,Y,Z letters should be used exactly once.
With that in mind, I think this code will do:
if [[ "$1" =~ ^[XxYyZz][0-9]+(,[XxYyZz][0-9]+){2}$ ]] &&
[[ "$1" =~ .*[Xx].* ]] &&
[[ "$1" =~ .*[Yy].* ]] &&
[[ "$1" =~ .*[Zz].* ]]
then
echo OK
else
echo Not OK
fi
Test cases:
#!/usr/bin/env bash
check() {
[[ "$1" =~ ^[XxYyZz][0-9]+(,[XxYyZz][0-9]+){2}$ ]] &&
[[ "$1" =~ .*[Xx].* ]] &&
[[ "$1" =~ .*[Yy].* ]] &&
[[ "$1" =~ .*[Zz].* ]]
}
test_check() {
# code - expected exit code
# value - string to test
while read code value; do
check "$value"
if [[ $? == $code ]]; then
echo -e "\e[1;32mPassed:\e[0m $value"
else
echo -e "\e[1;31mFailed:\e[0m $value"
fi
done
}
test_check <<EOF
0 x1,y2,z3
0 X1,Y2,Z3
1 x,y,z
1 1,2,3
1 1x,2y,3z
0 z1,x2,y3
1 a1,b2,c3
1 x1
1 x1,y2 z1
1 x1,x2
1 x1;y2;z3
1 x1,y2
1 x1,y2,y3
0 x100,Y500,z0
0 x011,y015,z0
1 x1,x2,y3,z4
1 x1,y1,z1 .
EOF
P.S.
If any of the X,Y,Z may appear in the string more than once or not appear at all, then [[ "$str" =~ ^[XxYyZz][0-9]+(,[XxYyZz][0-9]+)*$ ]] should work. I added here + for digits to appear one or more times after the letter, and quoted "$str" in case if there's a space in it (or, to be precise, any character from $IFS variable).

Number of arguments in bash script

I want to test the number of arguments passed to a Linux shell script. If the number of arguments is not 2 or 4, it should print something. Unfortunately it does not work. Can anyone explain what I am doing wrong?
#!/bin/bash
if [[ $# -ne 2 ]] || [[ $# -ne 4 ]];
then
echo "here";
fi
You should replace logical OR by logical AND, so :
#!/bin/bash
if [[ $# -ne 2 && $# -ne 4 ]]; then
echo "here"
fi
In arithmetic form:
#!/bin/bash
if (($# != 2 && $# != 4)); then
echo "here"
fi
As you can see, no need to use 2 [[ ]]
Logic.
if [[ $# -ne 2 ]] && [[ $# -ne 4 ]]; then
echo "here"
fi

Make one-liner if statement more fancy

My code is as below:
if [[ $? -eq 0 ]]; then
$command1;
$command2;
$command3;
fi
Can I have a one liner to do the same? I have this but looking for something better and fancy.
if [[ $? -eq 0 ]]; then $command1;$command2;$command3; fi
Your attempted oneliner works just fine, as in this example:
commnd1='echo foo'
commnd2='echo bar'
commnd3='echo baz'
true
if [[ $? -eq 0 ]];then $commnd1;$commnd2;$commnd3;fi
You can also use && and command grouping:
[[ $? -eq 0 ]] && { cmd1; cmd2; cmd3; }

How to check two possibilities in an if statement in a shell script?

I need to check two variables, count and count1, for equality with 2.
I tried the following code, but it didn't work:
if [ $count -eq 2 || $count1 -eq 2 ]; then
echo "Condition passsed"
fi
How can I fix it?
That type of conditional doesn't recognize ||. You either need to use -o (or), or use a [[ test:
if [ $count -eq 2 -o $count1 -eq 2 ]; then
echo "Condition passsed"
fi
if [[ $count -eq 2 || $count1 -eq 2 ]]; then
echo "Condition passsed"
fi
You need to use [[:
if [[ $count -eq 2 || $count1 -eq 2 ]]; then echo "Condition passsed"; fi

Resources