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; }
Related
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
I have the bash script below:
#!/bin/bash
#
[ $# -eq 1 -a $1 = "--help" -o $# -eq 0 ] && {
echo Help will come here
}
When I run it:
$ ./script
./script: line 3: [: too many arguments
$ ./script --help
Help will come here
As you can see, when I don't pass parameters ( $# -eq 0 ) it fails with "too many arguments".
So, I tested it directly in terminal:
$ a=1;b=2;c=3
$ [ $a -eq 1 -a $b -eq 2 -o $c -eq 3 ] && echo ok
ok
$ [ $a -eq 0 -a $b -eq 2 -o $c -eq 3 ] && echo ok
ok
$ [ $a -eq 0 -a $b -eq 0 -o $c -eq 3 ] && echo ok
ok
$ [ $a -eq 0 -a $b -eq 0 -o $c -eq 0 ] && echo ok
$ [ $a -eq 0 -a $b -eq 2 -o $c -eq 0 ] && echo ok
$ [ $a -eq 1 -a $b -eq 2 -o $c -eq 0 ] && echo ok
ok
So, if it works perfectly in terminal why doesn't it work passing parameters?
Thanks,
Express your condition like this :
[ $# -eq 1 ] && [ "$1" = "--help" ] || [ $# -eq 0 ]
Actually, [ is a command, and the following elements in the commands are subject to word splitting. If an argument is empty (or contains whitespace and is unquoted), you can run into surprises. Using -a and -o is deprecated.
Please note that, if you want to use the && logical operator (instead of an if statement) before your echo statement, you will need to enclose the above inside braces, else the operator precedence (coupled with lazy evaluation) may yield incorrect results.
{ [ $# -eq 1 ] && [ "$1" = "--help" ] || [ $# -eq 0 ] ; } && { echo...
If you do not mind using Bash-specific syntax, you could also write :
[[ $# -eq 1 && $1 = "--help" || $# -eq 0 ]]
Note that in this case, double-quoting $1 is not required, because the [[ ]] construct is special shell syntax, not a command, and what is inside is not subject to word splitting. Because there is a single test, you do not need to enclose it inside braces before your && { echo....
Your entire expression can be simplified to:
function help () {
printf "%s\n" "help is on it's way."
}
[[ $# -eq 0 || "$*" = "--help" ]] && help ; echo "done." && exit 0 ;
This checks if the total sum of arguments is zero, or the argument(s) equals "--help". If either of those two things are true then it proceeds to the help function, otherwise echo "done" and exit.
When you are executing the script without parameter, you are getting the error because your condition is matching with blank character, see below -
$sh -x kk.sh
+ '[' 0 -eq 1 -a = --help -o 0 -eq 0 ']'
kk.sh: line 3: [: too many arguments
As you can see that there is no value to match.
When you will execute below command in your terminal -
$[ $a -eq 1 -a $b -eq 2 -o $c -eq 3 ] && echo ok
-bash: [: too many arguments
$a=1;b=2;c=3
$[ $a -eq 1 -a $b -eq 2 -o $c -eq 3 ] && echo ok
ok ####it is printing this value bcoz you have set the variable to match, it doesn't matter condition is wrong or right but there is something to match.
To resolve this issue you can use one if condition in the beginning to assign a dummy value if there is no value.
Try this :-
[ $# -eq 1 ] || [ $1 = "--help" ] || [ $# -eq 0 ]
This way it will automatically display if --help is provided or 1 is typed.
I think $# is creating the problem as both there is $# for first and second conditions
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
In the following code I want to compare the command line arguments with the parameters but I am not sure what is the current syntax to compare the arguments with parameters..i.e "==" or "-eq".
#!/bin/bash
argLength=$#
#echo "arg = $1"
if [ argLength==0 ]; then
#Running for the very first
#Get the connected device ids and save it in an array
N=0
CONNECTED_DEVICES=$(adb devices | grep -o '\b[A-Za-z0-9]\{8,\}\b'|sed -n '2,$p')
NO_OF_DEVICES=$(echo "$CONNECTED_DEVICES" | wc -l)
for CONNECTED_DEVICE in $CONNECTED_DEVICES ; do
DEVICE_IDS[$N]="$CONNECTED_DEVICE"
echo "DEVICE_IDS[$N]= $CONNECTED_DEVICE"
let "N= $N + 1"
done
for SEND_DEVICE_ID in ${DEVICE_IDS[#]} ; do
callCloneBuildInstall $SEND_DEVICE_ID
done
elif [ "$1" -eq -b ]; then
if [ $5 -eq pass ]; then
DEVICE_ID=$3
./MonkeyTests.sh -d $DEVICE_ID
else
sleep 1h
callCloneBuildInstall $SEND_DEVICE_ID
fi
elif [ "$1" -eq -m ]; then
echo "Check for CloneBuildInstall"
if [ "$5" -eq pass ]; then
DEVICE_ID=$3
callCloneBuildInstall $SEND_DEVICE_ID
else
echo "call CloneBuildInstall"
# Zip log file and save it with deviceId
callCloneBuildInstall $SEND_DEVICE_ID
fi
fi
function callCloneBuildInstall {
./CloneBuildInstall.sh -d $SEND_DEVICE_ID
}
From help test:
[...]
STRING1 = STRING2
True if the strings are equal.
[...]
arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne,
-lt, -le, -gt, or -ge.
But in any case, each part of the condition is a separate argument to [.
if [ "$arg" -eq 0 ]; then
if [ "$arg" = 0 ]; then
Why not use something like
if [ "$#" -ne 0 ]; then # number of args should not be zero
echo "USAGE: "
fi
When/how to use “==” or “-eq” operator in test?
To put it simply use == when doing lexical comparisons a.k.a string comparisons but use -eq when having numerical comparisons.
Other forms of -eq (equal) are -ne (not equal), -gt (greater than), -ge (greater than or equal), -lt (lesser than), and -le (lesser than or equal).
Some may also suggest preferring (( )).
Examples:
[[ $string == "something else" ]]
[[ $string != "something else" ]] # (negated)
[[ $num -eq 1 ]]
[[ $num -ge 2 ]]
(( $num == 1 ))
(( $num >= 1 ))
And always use [[ ]] over [ ] when you're in Bash since the former skips unnecessary expansions not related to conditional expressions like word splitting and pathname expansion.
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