i have the error named in the tittle in shell scripts when i execute this:
if [ $[ $op1 % $op2 ] == 0 ] and [ $res -ne 0 ]
then
resp
else
divi
fi
thanks.
Your first line violates several shell syntax rules.
if [ $[ $op1 % $op2 ] == 0 ] and [ $res -ne 0 ]
and has no meaning in a shell. Just like in C the correct syntax is &&.
And math expressions use $(( ... )).
So you probably meant:
if [ $(( $op1 % $op2 )) == 0 ] && [ "$res" -ne 0 ]
I also added quoting to $res as otherwise expansion takes place. E.g. if $res equals *, it would not compare * to zero but it would expand *, replacing it with the first filename in the current directory and this could cause the test to be true despite $res being *. To prevent this, you must quote $res. You don't need quoting for $op1 and $op2 as in math expression no shell expansion is performed.
Related
I'm attempting to append to a file this pseudo code:
if [ `expr $MOD % 5` -eq 0 ]; then.
I did it on shell as:
echo if [ `expr $MOD % 5` -eq 0 ]; then
But it gives me a syntax error near unexpected token 'then'. I can't figure out where I'm going wrong.
Use single quotes:
$ echo 'if [ `expr $MOD % 5` -eq 0 ]; then'
if [ `expr $MOD % 5` -eq 0 ]; then
If I am reading this right, you are trying to append this actual text to a file?
if [ `expr $MOD % 5` -eq 0 ]; then
Then the issue is your command structure. You used this:
echo if [ `expr $MOD % 5` -eq 0 ]; then
This is actually two commands as far as the shell is concerned, due to the semicolon. So it is the same as:
echo if [ `expr $MOD % 5` -eq 0 ]
then
Since then is not normally a valid command, and this is outside the context of a shell conditional (for instance if), the shell doesn't know what to do with it.
What you need to do is quote the entire thing as a string and pass that to echo.
echo 'if [ `expr $MOD % 5` -eq 0 ]; then'
Using single quote (') rather than double quote (") is important. If you use double quote, then the bit in backticks (expr $MOD % 5) would be evaluated instead of passed to the file as-is.
I'm learning BASH through HackerRank.There's an exercise in which the lengths of the triangle is given and then you need to find whether the triangle is isosceles,scalene or equilateral.I wrote the following code:
read a
read b
read c
if [ [ "$a" -eq "$b" ] && [ "$b" -eq "$c" ] ]
then
echo "EQUILATERAL"
elif [ [ "$a" -eq "$b" ] || [ "$b" -eq "$c" ] ]
then
echo "ISOSCELES"
else
echo "SCALENE"
fi
But then I get the following error
solution.sh: line 4: [: too many arguments
solution.sh: line 7: [: too many arguments
solution.sh: line 7: [: too many arguments
Why is this happening? I tried long and hard to rectify it but nothing worked out
You can combine conditions either ommiting the surrounding brackets like this
if [ "$a" -eq "$b" ] && [ "$b" -eq "$c" ]
or by combining the conditions with -a/-o like this
if [ "$a" -eq "$b" -a "$b" -eq "$c" ]
see http://wiki.bash-hackers.org/commands/classictest#and_and_or
&& and || are Bash list operators. In a chain of commands, the next command is executed only if the previous command returned 0 (&&) or nonzero (||).
[ is an alias for the Bash internal test command and has arguments such as -eq or -ne. ] ends its command line. Type help test for more information.
So if you write a conditional expression, you do not put the list operators inside brackets.
Try, for example, this instead of the respective line in your code:
if [ "$a" -eq "$b" ] && [ "$b" -eq "$c" ]
then
[ isn't a grouping operator in bash, you can't use it to group tests.
there are a number of different ways to express the tests you want to make, numeric evaluation mode is probably easiest to read
if (( a == b && b == c ))
if (( a == b || b == c || c == a ))
This is going to break if you have decimal fractions, but will work fine for integers.
[ is a conditional command, like an alias for sh's test built-in command.
[[ is the same for bash which has more test options.
So make a choice between [ and [[ but not [ [ which means two command.
Example:
# [ [ -n 'test' ] ]
bash: [: too many arguments
# [ -n 'test' ] && echo $?
0
# [[ -n 'test' ]] && echo $?
0
I've seen two ways in tutorials to do syntax for if statements in BASH shell:
This one wouldn't work unless I put quotes around the variable and added additional [ and ]:
if [[ "$step" -eq 0 ]]
This one worked without putting quotes around the variable and the additional [ and ] weren't needed:
if [ $step -ge 1 ] && [ $step -le 52 ]
Which is correct and best practice? What are the differences? Thanks!
"When referencing a variable, it is generally advisable to enclose its name in double quotes" -- http://tldp.org/LDP/abs/html/quotingvar.html
if [ $step -ge 1 ] && [ $step -le 52 ] can be replaced as
if [ "$step" -ge 1 -a "$step" -le 52 ]
if [[ "$step" -eq 0 ]] can be replaced as if [ "$step" -eq 0 ]
Also, suppose you have the following script:
#!/bin/bash
if [ $x -eq 0 ]
then
echo "hello"
fi
You get this error when you run the script -- example.sh: line 2: [: -eq: unary operator expected
But using if [ "$x" -eq 0 ]
You get a different error when you run the script -- example.sh: line 2: [: : integer expression expected
Thus, it is always best to put variables inside quotes...
if [[ .... ]] syntax is particularly useful when you have regex in the condition statement -- http://honglus.blogspot.com/2010/03/regular-expression-in-condition.html
EDIT: When we deal with strings --
#!/bin/bash
if [ $x = "name" ]
then
echo "hello"
fi
You get this error when you run the script -- example.sh: line 2: [: =: unary operator expected
But, if you use if [ "$x" = "name" ] it runs fine (i.e. no errors ) and if statement is evaluated as false, as value of x is null which does not match name.
I need to implement something like:
if [ $i -ne $hosts_count - 1] ; then
cmd="$cmd;"
fi
But I get
./installer.sh: line 124: [: missing
`]'
What I am doing wrong?
The command [ can't handle arithmetics inside its test. Change it to:
if [ $i -ne $((hosts_count-1)) ]; then
Edit: what #cebewee wrote is also true; you must put a space in front of the closing ]. But, just doing that will result in yet another error: extra argument '-'
The ] must be a separate argument to [.
You're assuming you can do math in [.
if [ $i -ne $(($hosts_count - 1)) ] ; then
In bash, you can avoid both [ ] and [[ ]] by using (( )) for purely arithmetic conditions:
if (( i != hosts_count - 1 )); then
cmd="$cmd"
fi
The closing ] needs to be preceded by a space, i.e. write
if [ $i -ne $hosts_count - 1 ] ; then
cmd="$cmd;"
fi
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
I wrote a function in bash script. However, it's complaining about syntax. I really can't see what is it..... the error message is [: missing `]'
addem() {
if [ $# -eq 0] || [ $# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 + $1 ]
else
echo $[ $1 + $2 ]
fi
}
You need a space before the first ]. That is:
change:
if [ $# -eq 0] || [ $# -gt 2 ]
to:
if [ $# -eq 0 ] || [ $# -gt 2 ]
Try:
if [ $# -eq 0 ] || [ $# -gt 2 ]
(There was no space between 0 and ].)
indyK1ng: The "#" is not treated as a comment, since the "$" escapes the next character. The "$#" is an internal variable representing the number of positional parameters that exist at the current context. This can be thought of as the number of command line arguments to the shell script, but that array can be reset using the "set -- [args]" built in.
Joakim Elofsson: The overall structure of the if statement is correct, the ";" is only required before the "then" and before the "fi" if those are not listed on a separate line.
The problem is the space between the "0" and the bracket. Bash requires that brackets used to delimit conditional expressions be set off with at least a single space from the expression.
if [ $# -eq 0] || [ $# -gt 2 ] # Wrong
if [ $# -eq 0 ] || [ $# -gt 2 ] # Correct
On an additional note, the two conditional expressions can be combined. The operator association will ensure that everything works out.
if [ $# -eq 0 -a $# -gt 2 ] # Even Better
I tend to prefer the expanded features offered with double brackets for expression evaluation. Note that the combination of the two evaluations is done with a different operator. I find this to be more readable.
if [[ $# -eq 0 || $# -gt 2 ]] # My preference
Later in the script, the use of single brackets for integer addition is not recommended. The single brackets are evaluating an expression to a boolean. Double parens are used for integer math.
echo $[ $1 + $1 ] # Evaluation of an expression
echo $(( $1 + $1 )) # Integer math
Bash is sensitive to spaces. In your first line, replace if [ Y -eq X] with [ Y -eq X ] (space before the "]")
I would use extended test constructs (BASH) as demonstrated bellow. I think it would reduce the no of characters and increase readability (at least for programmers). :-)
addem() {
if (( $# == 0 || $# > 2 ))
then
echo -1
elif (( $# == 1 ))
then
echo (( $1 + $1 ))
else
echo (( $1 + $2 ))
fi
}
You should avoid brackets and use test instead:
if test $# -eq 0 || test $# -gt 2
then
echo -1
elif test $# -eq 1
then
echo $(( $1 + $1 ))
else
echo $(( $1 + $2 ))
fi
Getting a better shell style will make you much better. :)