LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script unexpected operator (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-unexpected-operator-4175497349/)

postcd 03-07-2014 03:22 AM

Bash script unexpected operator
 
Script output
Quote:

root@***'s password:
-----------Select (em) or (e) or (cm)---------
em
[: 24: em: unexpected operator
[: 27: em: unexpected operator

Script code:
Code:

echo "-----------Select (em) or (e) or (cm)---------"
read dom
if [ "$dom" == "em" ];then
mplayer sshfs/Hudba/*
exit
fi
if [ "$dom" == "e" ];then
exit
fi

i want to ask why it sends unexpected operator when i type "em"?

grail 03-07-2014 03:32 AM

Try putting set -xv at the top of the script and see what is actually being processed.

rtmistler 03-07-2014 07:52 AM

Always verify your test criteria first. Since I don't know what lines 24 and 27 exactly are in your script, I can't say anything specific about them.

Why does it show something appearing to be root password being entered? You should not be running scripts as root, and if there's a command in that script requiring root privileges, then you should be using sudo.

I'm guessing that the unexpected operator has more to do with your call to mplayer versus your test, because if you examine the tests, they work.

Consider using elif.

Consider using exit a bit less:
Code:

#!/bin/sh

echo "-----------Select (em) or (e) or (cm)---------"
read dom
if [ "$dom" == "em" ];then
    echo "user entered em"
elif [ "$dom" == "e" ];then
    echo "user entered e"
elif [ "$dom" == "cm" ]; then
    echo "user entered cm"
else
    echo "user entered something else"
fi

exit 0;

Sample outputs:
Code:

~/testcode$ mplayer.sh
-----------Select (em) or (e) or (cm)---------
em
user entered em
~/testcode$ mplayer.sh
-----------Select (em) or (e) or (cm)---------
e
user entered e
~/testcode$ mplayer.sh
-----------Select (em) or (e) or (cm)---------
cm
user entered cm
~/testcode$ mplayer.sh
-----------Select (em) or (e) or (cm)---------
other
user entered something else


s.verma 03-08-2014 02:46 AM

Quote:

Originally Posted by postcd
Code:

echo "-----------Select (em) or (e) or (cm)---------"
read dom
if [ "$dom" == "em" ];then
mplayer sshfs/Hudba/*
exit
fi
if [ "$dom" == "e" ];then
exit
fi


This worked for me flawlessly, except I have replaced "mplayer sshfs/Hudba/*" with "echo Hello".
So may be it is something related with mplayer.

Kenhelm 03-08-2014 10:13 AM

Try using "=" instead of "==".
"unexpected operator" is not a bash error message. However dash gives that message if '==' is used instead of '=' in a test. Bash supports '==' but dash doesn't.
If dash is the default shell and the script was started with sh then the script will be running in dash, not bash.

Error message strings can be extracted from shells with od
Code:

# dash contains "unexpected operator"
od -An --strings /bin/dash | grep 'unexpected'

%s unexpected (expecting %s)
%s unexpected
unexpected operator

Code:

# bash does not contain "unexpected operator"
od -An --strings /bin/bash | grep 'unexpected'

syntax error near unexpected token `%s'
syntax error: unexpected end of file
unexpected EOF while looking for matching `%c'
unexpected EOF while looking for `]]'
syntax error in conditional expression: unexpected token `%s'
unexpected token `%s', expected `)'
unexpected argument `%s' to conditional unary operator
unexpected argument to conditional unary operator
unexpected token `%s', conditional binary operator expected
unexpected argument `%s' to conditional binary operator
unexpected argument to conditional binary operator
unexpected token `%c' in conditional command
unexpected token `%s' in conditional command
unexpected token %d in conditional command
unexpected EOF while looking for matching `)'
syntax error: `;' unexpected


postcd 03-10-2014 04:23 AM

you were probably right, i did:

[ $variable = value ]

instead of:

[ "$variable" == "value" ]

and started working

grail 03-10-2014 07:08 AM

If you wish to keep formatting so everyone can see the difference in what you have typed, you will need to use [code][/code] tags around your code / data.

Remember to mark as SOLVED once you have a solution.


All times are GMT -5. The time now is 11:11 PM.