This question already has an answer here:
Bash scripting unexpected operator
(1 answer)
Closed 6 years ago.
What is wrong in my code?
#!/bin/sh
LOOK_FOR="$1"
for i in `find $2 -name "*jar"`; do
echo "Looking in $i ..."
#jar tvf $i | grep $LOOK_FOR > /dev/null
jar tvf "$i" | grep "$LOOK_FOR"
if [ $? == 0 ] ; then
echo "==> Found \"$LOOK_FOR\" in $i"
fi
done #line 13
Output
wk#wk-laptop:$ sh lookjar.sh org/apache/axis/message/addressing/EndpointReference /media/0C06E20B06E1F61C/uengine/uengine
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/build/uengine_settings.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/WebContent/uengine-web/lib/FCKeditor/WEB-INF/lib/commons-fileupload.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/WebContent/uengine-web/lib/FCKeditor/WEB-INF/lib/FCKeditor-2.3.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/WebContent/uengine-web/processmanager/signedmetaworks.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/hsqldb/lib/hsqldb.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/hsqldb/lib/servlet.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/src/lib/commons-discovery.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/src/lib/google.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/src/lib/jxl.jar ...
You need to use = instead of == in the [ $? == 0 ] line.
You should change that to:
if [ $? -eq 0 ]; then
...
-eq does a numeric comparison.
You can also take advantage of the fact that in shell a return value of 0 is considered success and write your code like this:
if jar tvf "$i" | grep "$LOOK_FOR"; then
...
#!/bin/sh
LOOK_FOR="$1"
find $2 -name "*jar"`| while read -r file
echo "Looking in $file ..."
jar tvf "$file" | grep "$LOOK_FOR"
if [ $? -eq 0 ] ; then
echo "==> Found \"$LOOK_FOR\" in $file"
fi
done
Try:
if [[ $? == 0 ]]; then
echo "==> Found \"$LOOK_FOR\" in $i"
fi
Related
I am not sure why my code below generates this error (standard_in) 1: syntax error -bash: [: -eq: unary operator expected . Can someone please help me figure out the problem here? Thanks!
#!/bin/bash
BAMLINES=4.47264e+09
FQ1LINES=4000000
FQ2LINES=4000000
DEBUG=1
if [ ! -z ${DEBUG} ]; then
echo "${BAMLINES} lines in .bam"
echo "${FQ1LINES} lines in all ${FQ_OUT1} files"
echo "${FQ2LINES} lines in all ${FQ_OUT2} files"
if [ $(echo "scale=2;${FQ1LINES}/${BAMLINES} > 0.40" | bc) -eq 0 ]; then
echo "Warning, FQ1 file contains ${FQ1LINES} lines - less than 40% of the number of reads of .bam file"
fi
if [ $(echo "scale=2;${FQ2LINES}/${BAMLINES} > 0.4" | bc) -eq 0 ]; then
echo "Warning, FQ2 file contains ${FQ2LINES} lines - less than 40% of the number of reads of .bam file"
fi
fi
$ echo "scale=2;($FQ1LINES/$BAMLINES) > 0.40"
scale=2;(4000000/) > 0.40
# ..............^^
You want to use either BAMLINE or BAMLINES but not both.
$ echo "scale=2;($FQ1LINES/$BAMLINES) > 0.40" | bc
(standard_in) 1: parse error
Because of that error, the output of $(echo ... | bc) is empty, and then [ only gets 2 arguments. When [ gets 2 arguments, the first operator is expected to be a unary operator (like -z is) -- -eq is not a unary operator.
You need to quote any variables/command expansions within [...]. In this case you'd get a different but more meaningful error:
$ [ "$(echo "scale=2;${FQ1LINES}/${BAMLINES} > 0.40" | bc)" -eq 0 ]
(standard_in) 1: parse error
bash: [: : integer expression expected
Or use [[...]] and you'll just see the bc error
$ [[ $(echo "scale=2;${FQ1LINES}/${BAMLINES} > 0.40" | bc) -eq 0 ]]
(standard_in) 1: parse error
how to execute if statement line by line in bash??
for i in $(cat $install_script)
do
if [ "$i" = "$check" ]; then
count=$((count+1))
echo whiptail --title "Install SmartBoard" --yesno "${scripts_name[$count]}" 7 $((${#scripts_name[$count]} + 5))
if (( $? == 0 )); then
skip=false
else
skip=true
fi
fi
if [ "$i" == "$check wifi" ]; then
break
fi
if [ $skip == true ]; then
continue
else
sh -c $i ## process line by line
fi
done
error !!
sh: 1: Syntax error: end of file unexpected (expecting "then")
sh: 1: Syntax error: "then" unexpected
sh: 1: Syntax error: "else" unexpected
sh: 1: Syntax error: "fi" unexpected
i'm not good english so if my posts word be rude Sorry that. :(
This question already has an answer here:
Bash scripting unexpected operator
(1 answer)
Closed 6 years ago.
What is wrong in my code?
#!/bin/sh
LOOK_FOR="$1"
for i in `find $2 -name "*jar"`; do
echo "Looking in $i ..."
#jar tvf $i | grep $LOOK_FOR > /dev/null
jar tvf "$i" | grep "$LOOK_FOR"
if [ $? == 0 ] ; then
echo "==> Found \"$LOOK_FOR\" in $i"
fi
done #line 13
Output
wk#wk-laptop:$ sh lookjar.sh org/apache/axis/message/addressing/EndpointReference /media/0C06E20B06E1F61C/uengine/uengine
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/build/uengine_settings.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/WebContent/uengine-web/lib/FCKeditor/WEB-INF/lib/commons-fileupload.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/WebContent/uengine-web/lib/FCKeditor/WEB-INF/lib/FCKeditor-2.3.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/WebContent/uengine-web/processmanager/signedmetaworks.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/hsqldb/lib/hsqldb.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/hsqldb/lib/servlet.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/src/lib/commons-discovery.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/src/lib/google.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/src/lib/jxl.jar ...
You need to use = instead of == in the [ $? == 0 ] line.
You should change that to:
if [ $? -eq 0 ]; then
...
-eq does a numeric comparison.
You can also take advantage of the fact that in shell a return value of 0 is considered success and write your code like this:
if jar tvf "$i" | grep "$LOOK_FOR"; then
...
#!/bin/sh
LOOK_FOR="$1"
find $2 -name "*jar"`| while read -r file
echo "Looking in $file ..."
jar tvf "$file" | grep "$LOOK_FOR"
if [ $? -eq 0 ] ; then
echo "==> Found \"$LOOK_FOR\" in $file"
fi
done
Try:
if [[ $? == 0 ]]; then
echo "==> Found \"$LOOK_FOR\" in $i"
fi
I have this simple bash script that keeps failing with the following messages:
./servo.sh: line 7: syntax error near unexpected token then'
./servo.sh: line 7: if[ "$level" -eq 1 ]; then'
And my bash script:
#!/bin/bash
level=1
while :
do
if[ $level -eq 1 ]; then
echo "hello"
else
echo "else"
fi
done
What am I doing wrong?
add a space between the if and the [
I'm trying to write a shell script, but it's giving me a syntax error at the following command:
if [[ -n ${array[$x1]} -a [ expr length "$x1" -gt 2 ] ]]
This is the error message:
./project: line 45: syntax error in conditional expression
./project: line 45: syntax error near `-a'
./project: line 45: ` if [[ -n ${array[$x1]} -a [ expr length "$x1" -gt 2 ] ]]'
What am I doing wrong?
Use && not -a in [[ ]]
Also, expr length won't do what you expect here. The better approach, since you're already using bash extensions, is to use the ${#param} expansion to get the length of $param, and evaluate that within a math context, like so:
if [[ -n ${array[$x1]} ]] && (( ${#x1} > 2 )); then
...
fi