LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-07-2014, 04:22 AM   #1
postcd
Member
 
Registered: Oct 2013
Posts: 400

Rep: Reputation: Disabled
Lightbulb 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"?
 
Old 03-07-2014, 04:32 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Try putting set -xv at the top of the script and see what is actually being processed.
 
Old 03-07-2014, 08:52 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
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
 
1 members found this post helpful.
Old 03-08-2014, 03:46 AM   #4
s.verma
Member
 
Registered: Oct 2013
Distribution: Debian Sid, Gentoo, Arch, Debian
Posts: 169
Blog Entries: 4

Rep: Reputation: 24
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.
 
Old 03-08-2014, 11:13 AM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
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

Last edited by Kenhelm; 03-08-2014 at 11:16 AM.
 
1 members found this post helpful.
Old 03-10-2014, 05:23 AM   #6
postcd
Member
 
Registered: Oct 2013
Posts: 400

Original Poster
Rep: Reputation: Disabled
you were probably right, i did:

[ $variable = value ]

instead of:

[ "$variable" == "value" ]

and started working
 
Old 03-10-2014, 08:08 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script: problematic operator: =~ stf92 Programming 16 07-18-2012 02:58 AM
unexpected operator/operand in Bourne script ocicat Programming 4 07-28-2007 06:44 PM
Unary Operator expected. Bash script Blackout_08 Programming 2 06-22-2006 03:21 PM
bash script and the , operator xviddivxoggmp3 Programming 1 08-14-2004 01:33 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration