bash script question [: too many arguments - bash

This is the bash script I tested :
#!/bin/bash
if [ 8 -gt 6 ] && [ 10 -eq 10 ];
then
echo "Conditions are true"
fi
but get a error [: too many arguments.
I tried adding two square brackets, the error is syntax error near unexpected token `[['
#!/bin/bash
if [[ 8 -gt 6 ]] && [[ 10 -eq 10 ]];
then
echo "Conditions are true"
fi
I modified the code to be like this :
if [ 8 -gt 6 ];
then
echo "Conditions are true"
fi
This is okay .
How to solve this problem? thanks.

Related

While loop in a shell script gives error : [: too many arguments. How to resolve this issue?

function read_num(){
echo "Enter a lower limit"
read lower_limit
echo "Enter a upper limit"
read upper_limit
while [ [ $lower_limit -lt 1 ] || [ $lower_limit -gt $upper_limit ] ]
do
echo "Please enter again."
read_num
done
}
read_num
when I enter the two numbers lower and upper limit it gives the following output.
check.sh: line 6: [: too many arguments
And line number 6 is while loop
while [ [ $lower_limit -lt 1 ] || [ $lower_limit -gt $upper_limit ] ]
Here you go, this works for me:
#!/bin/bash
function read_num(){
echo "Enter a lower limit"
read lower_limit
echo "Enter a upper limit"
read uper_limit
while [[ $lower_limit -lt 1 ]] || [[ $lower_limit -gt $upper_limit ]]
do
echo "Please enter again."
read_num
done
}
read_num
Reference: Bash scripting, multiple conditions in while loop

need help in bash script for if and greater than [duplicate]

This question already has an answer here:
Why do some people put a semicolon after an if condition in shell scripts? [duplicate]
(1 answer)
Closed 3 years ago.
i have
syntax error near unexpected token `elif'
`elif [[$# -gt 31]]
when i execute following script (its part from script by "Fred Weinhaus" for text-cleaner) by python 3
if [ $# -eq 0 ] then # help information
echo ""
usage2
exit 0 elif [ $# -gt 31 ] then errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---" else while [ $# -gt 0 ] do # get parameter values case "$1" in
-h|-help) # help information
echo ""
usage2
exit 0
;;
how i solve this problem?
Your script has many mistakes. My fix may not be correct.
Define "usage2".
#!/bin/bash
if [ $# -eq 0 ]; then # help information
echo ""
#usage2
exit 0
elif [ $# -gt 31 ]; then
errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else while [ $# -gt 0 ]; do # get parameter values
case "$1" in
-h|-help) # help information
echo ""
#usage2
exit 0
;;
esac
done
fi
There are no errors in lines no 201 and 206.
Try this code.
#!/bin/bash
usage2(){
# dummy
:
}
if [[ $# -eq 0 ]]
then
echo ""
usage2
exit 0
elif [[ $# -gt 31 ]]
then
errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else
while [[ $# -gt 0 ]]
do
:
done
fi

script8.sh: line 9: [: missing `]' [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 3 years ago.
I'm trying to do test automation with a bash script using if then else statements but I'm running into a few errors. For one, when I try to execute it I'm doing something wrong with the variable assignment with j and k, because it tells me that the command j and the command k aren't found when I try to execute. How do you correctly create variables?
The most confusing thing though is when I try to execute the script I get an error telling me I have an unexpected token near fi, and then it just says 'fi'. What am I doing wrong here?
#!/bin/bash
j = 0
k = 0
echo Test1:
echo -ne "0\nIn\nUG\n" | /u/cgi_web/Tuition/cost
echo Test2:
echo -ne "0\nOut\nUG\n" | /u/cgi_web/Tuition/cost
echo Test3:
echo -ne "0\nIn\nGR\n" | /u/cgi_web/Tuition/cost
echo Test4:
echo -ne "0\nOut\nGR\n" | /u/cgi_web/Tuition/cost
for i in {1..17}
do
echo Test$((i+4)):
if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then
$j = 1
echo -ne "$i\nIn\nUG\n" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 0 ] then
$k = 1
echo -ne "$i\nIn\nGR\n" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 1 ] then
$j = 0
echo -ne "$i\nOut\nUG\n" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 0 ] && [ "$k" -eq 1 ] then
$k = 0
echo -ne "$i\nOut\nGR\n" | /u/cgi_web/Tuition/cost
fi
done
EDIT: I figure out the variable issue with j and k, I had to remove the spaces in the statement.
Bash if statements require a semi-colon before the then:
if [ condition ] || [ condition ]; then
# code
elif [ condition ] && [ condition ]; then
# code
fi
For example.
To help anyone who might look at this for help in the future, I figured I'd answer my own question with all the syntax errors I found from my own testing and with the helpful responses of others.
To start the variable assignment:
j = 0
you can't have spaces in between, so it would be:
j=0
Also if statements need a space between if and the bracket and need a semicolon after the last bracket before then. Therefore my incorrect if statement
if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then
becomes
if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]; then
or instead of a semicolon you can have a new line between the bracket, so it would become
if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]
then

Unary operator expected error in while loop execution in shell scripting(UNIX)

I wrote a code to print
5 4 3 2 1
using while loop in shell script.
But error of expr: syntax error
scriptprog3.sh: line 3: [: -gt: unary operator expected
is showing. Please help out. Here is the code
a=5
while [ $a -gt 0 ]
do
echo $a
a=` expr $a -1 `
done
This is what you actually may want:
$ a=5
$ while (( a > 0 ));do echo $a ; (( a-- ));done
5
4
3
2
1
(( code )) makes the math over the a var without returning any value back to the caller.
I was executing the following code and it gave me the "[: -lt: unary operator expected"
error.
#while
i = 10
echo "This is a while loop"
while [ $i -lt 15 ]
do
echo $i
i=$[ $i+1 ]
done
The problem was using while loop as while [ $i -lt 15 ] like this,
so I changed it towhile [[ $i -lt 15 ]] and it worked (notice the double parentheses).
Below is the working example.
#while
i = 10
echo "This is a while loop"
while [[ $i -lt 15 ]]
do
echo $i
i=$[ $i+1 ]
done

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

Resources