Shell Script Error - bash

Can any one tell what wrong with this script ?. Because I am getting the error like
./timer: line 9: [13: command not found
./timer: line 12: [13: command not found
My script look like
#!/bin/bash
while :
do
HOUR=$(date +%H)
MINUTE=$(date +%M)
SECOND=$(date +%S)
if [$HOUR == 13] && [$MINUTE == 12] && [$SECOND == 1]
then ./binary
elif [$HOUR == 18] && [$MINUTE == 30] && [$SECOND == 1]
then ./binary
fi
done

put a space between the [ ... ]
Example:
if [$HOUR == 13] && [$MINUTE == 12] && [$SECOND == 1]
Should become
if [ $HOUR == 13 ] && [ $MINUTE == 12 ] && [ $SECOND == 1 ]

I think you must use "${VARIABLE}" and respect spaces for square brackets
This would give :
if [ "${HOUR}" == 13 ] && [ "${HOUR}" == 12 ] && [ "${HOUR}" == 1 ]
Hope that helps !

the test operators in bash need to have a space by the opening and closing bracket, try
[ $HOUR == 13 ] && [ $MINUTE == 12 ] && [ $SECOND == 1 ]
and
[ $HOUR == 18 ] && [ $MINUTE == 30 ] && [ $SECOND == 1 ]
here is a link that you might find useful
http://tldp.org/LDP/abs/html/testconstructs.html

Several things:
You have to put a space after [ and before ].
It's a good practice to protect your variables using double quotes.
You have to use the -eq operator to compare numeric values (see Bash conditional operators).
Like so:
if [ "$HOUR" -eq 13] && [ "$MINUTE" -eq 12 ] && [ "$SECOND" -eq 1 ]
elif [ "$HOUR" -eq 18 ] && [ "$MINUTE" -eq 30 ] && [ "$SECOND" -eq 1 ]

Related

Bash: if then match on multiple conditions if all are true

I'm writing a script that checks for 3 different variables and matches on true. However I need it to only return true if ALL conditions match. Right now its returning true if only one or 2 conditions match from my list of operators.
if [ -z "$VAR3" ] || [ "$VAR1" -ge 10 ] || [ "$VAR2" != "WORKING" ]; then
echo "app failed "$VAR1" check-ins"
exit 2
elif [ -n "$VAR3" ] || [ "$VAR1" -le 10 ] || [ "$VAR2" == "WORKING" ]; then
echo "$VAR3" pid active connection is "$VAR2"
exit 0
fi
Should I just replace the || with && ??
Yes, || represents the OR logical operator, it returns true if at least one of the operators is true. && represents the AND logical operator, it returns true if and only if all of the operators are true
if [ -z "$VAR3" ] && [ "$VAR1" -ge 10 ] && [ "$VAR2" != "WORKING" ]; then
echo "app failed "$VAR1" check-ins"
exit 2
elif [ -n "$VAR3" ] && [ "$VAR1" -le 10 ] && [ "$VAR2" == "WORKING" ]; then
echo "$VAR3" pid active connection is "$VAR2"
exit 0
fi

Shellscript missing ]

I have a shellscript that tells me missing ] in the line
if [ $status != "2" && $status != "3" && `echo "$temp1 > $upperLimit" | bc` = "1" ]
and also missing ] in the line
if [ $status = "2" && `cat motionsensordate` \> `date +%s` ]
Why is that?
The single [ doesn't support logical operators inside the brackets. You have to use them outside
if [ "$status" != 2 ] && [ "$status" != 3 ] ...
Use double quotes for variables in single brackets to prevent unary operator expected error when the variable is empty.
Or, switch to double brackets:
if [[ $status != 2 && $status != 3 ... ]]
Also, status different to 2 and 3 can be expressed by a pattern:
if [[ $status != [23] && ... ]]
And if you would like to (in addition to the answers here) group together conditions:
if [[ ( COND1 || COND2 ) && COND3 ]]
then
echo "$cmd"
break
fi

Assign new value to variable fails

why this do not work:
if [ $_TYPE == 1 ] || [ $_TYPE == 2 ]; then
$_TYPE=$(echo "Outbound");
fi
or
if [ $_TYPE == 1 ] || [ $_TYPE == 2 ]; then
$_TYPE=echo "Outbound";
fi
or
if [ $_TYPE == 1 ] || [ $_TYPE == 2 ]; then
$_TYPE="Outbound";
fi
I'm receiving this error: line 251: 2=Outbound: command not found
In POSIX shells such as Bash, $ is not part of the variable-name, it's just the notation for expanding the variable (to obtain its value); so, for example, echo "$_TYPE" prints the value of the variable _TYPE. You don't use the $ when you're assigning to the variable. So you just need:
if [[ "$_TYPE" = 1 || "$_TYPE" = 2 ]] ; then
_TYPE=Outbound
fi
$ is used to access the value, but if you have to assign a value, the syntax is :
_TYPE="newValue"

Bash if statement with multiple conditions throws an error

I'm trying to write a script that will check two error flags, and in case one flag (or both) are changed it'll echo-- error happened. My script:
my_error_flag=0
my_error_flag_o=0
do something.....
if [[ "$my_error_flag"=="1" || "$my_error_flag_o"=="2" ] || [ "$my_error_flag"="1" && "$my_error_flag_o"="2" ]]; then
echo "$my_error_flag"
else
echo "no flag"
fi
Basically, it should be, something along:
if ((a=1 or b=2) or (a=1 and b=2))
then
display error
else
no error
fi
The error I get is:
line 26: conditional binary operator expected
line 26: syntax error near `]'
line 26: `if [[ "$my_error_flag"=="1" || "$my_error_flag_o"=="2" ] || [ "$my_error_flag"="1" && "$my_error_flag_o"="2" ]]; then'
Are my brackets messed up?
Use -a (for and) and -o (for or) operations.
tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
Update
Actually you could still use && and || with the -eq operation. So your script would be like this:
my_error_flag=1
my_error_flag_o=1
if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ] || ([ $my_error_flag -eq 1 ] && [ $my_error_flag_o -eq 2 ]); then
echo "$my_error_flag"
else
echo "no flag"
fi
Although in your case you can discard the last two expressions and just stick with one or operation like this:
my_error_flag=1
my_error_flag_o=1
if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ]; then
echo "$my_error_flag"
else
echo "no flag"
fi
You can use either [[ or (( keyword. When you use [[ keyword, you have to use string operators such as -eq, -lt. I think, (( is most preferred for arithmetic, because you can directly use operators such as ==, < and >.
Using [[ operator
a=$1
b=$2
if [[ a -eq 1 || b -eq 2 ]] || [[ a -eq 3 && b -eq 4 ]]
then
echo "Error"
else
echo "No Error"
fi
Using (( operator
a=$1
b=$2
if (( a == 1 || b == 2 )) || (( a == 3 && b == 4 ))
then
echo "Error"
else
echo "No Error"
fi
Do not use -a or -o operators Since it is not Portable.
Please try following
if ([ $dateR -ge 234 ] && [ $dateR -lt 238 ]) || ([ $dateR -ge 834 ] && [ $dateR -lt 838 ]) || ([ $dateR -ge 1434 ] && [ $dateR -lt 1438 ]) || ([ $dateR -ge 2034 ] && [ $dateR -lt 2038 ]) ;
then
echo "WORKING"
else
echo "Out of range!"
You can get some inspiration by reading an entrypoint.sh script written by the contributors from MySQL that checks whether the specified variables were set.
As the script shows, you can pipe them with -a, e.g.:
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
...
fi

Consolidate multiple if statements in Ksh

How can I consolidate the following if statements into a single line?
if [ $# -eq 4 ]
then
if [ "$4" = "PREV" ]
then
print "yes"
fi
fi
if [ $# -eq 3 ]
then
if [ "$3" = "PREV" ]
then
print "yes"
fi
fi
I am using ksh.
Why does this give an error?
if [ [ $# -eq 4 ] && [ "$4" = "PREV" ] ]
then
print "yes"
fi
Error:
0403-012 A test command parameter is not valid.
Try this:
if [[ $# -eq 4 && "$4" == "PREV" ]]
then
print "yes"
fi
You can also try putting them all together like this:
if [[ $# -eq 4 && "$4" == "PREV" || $# -eq 3 && "$3" == "PREV" ]]
then
print "yes"
fi
Do you just want to check if the last argument is "PREV"? If so, you can also do something like this:
for last; do true; done
if [ "$last" == "PREV" ]
then
print "yes"
fi
'[' is not a grouping token in sh. You can do:
if [ expr ] && [ expr ]; then ...
or
if cmd && cmd; then ...
or
if { cmd && cmd; }; then ...
You can also use parentheses, but the semantics is slightly different as the tests will run in a subshell.
if ( cmd && cmd; ); then ...
Also, note that "if cmd1; then cmd2; fi" is exactly the same as "cmd1 && cmd2", so you could write:
test $# = 4 && test $4 = PREV && echo yes
but if your intention is to check that the last argument is the string PREV, you might consider:
eval test \$$# = PREV && echo yes
Try this :
if [ $# -eq 4 ] && [ "$4" = "PREV" ]
then
print "yes"
fi

Resources