Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

"unexpected operator" error in sh script

Posted on 2010-11-25
3
Medium Priority
?
1,620 Views
Last Modified: 2012-05-10
I made a wrapper script so my password isn't logged into bash_history and also so I don't have to do so much typing for a few common "vmrun" commands.

It works fine but, unfortunately, I am getting the following error:

$ ./vvmrun -p list
./vvmrun: line 40: [: -v: unary operator expected

On another computer the error is "unexpected operator."

Help appreciated...

Thanks,
Frank

#!/bin/sh
# Define Version Number & Description
version="1.0"
desc="Wrapper for vmrun"
program=`echo $0 |sed -e 's#^\./##'`
# Define help
help="
$program $version, $desc\n
Usage: $program \[Options\] command\n
Example: $program -p > command\n
\n
Options \n
\n
-s Show the command that will be run--no action taken.\n
-p Prompt for password.\n
-H Remote hostname (default=local hostname).\n
"   # see wget -h for "help" layout ideas

# define variables
host=`hostname`

# if no arguments then print help and exit
if [ $# = 0 ]; then
    echo $help && exit
fi

# options followed by a ":" take arguments--";" optional arguments
while getopts "hspH:" option; do
    case "$option" in
	h) echo $help && exit;;
	s) show=1;;
	p) passwd=1;;
	H) host="$OPTARG";;
	[?]) echo "Exiting due to illegal option" && exit;;
    esac
done
shift `expr $OPTIND - 1`

# do stuff here
if ! [ -v $passwd ] ; then 
    stty -echo
    read -p "Password: " passw; echo
    stty echo
    command="vmrun -T server -h http://$host:8222/sdk -u frank -p $passw"
else
    command="vmrun -T server -h http://$host:8222/sdk"
fi

if ! [ -v $show ] ; then 
    echo $command $@
else 
    $command $@
fi
exit 0

Open in new window

0
Comment
Question by:ibanja
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 23

Assisted Solution

by:Maciej S
Maciej S earned 200 total points
ID: 34213780
There is no such test like -v.
Refer to "man test" for possible tests.
0
 
LVL 48

Accepted Solution

by:
Tintin earned 800 total points
ID: 34213909
if ! [ -v $passwd ] ; then


should be

if  [  "$passwd" -eq 1 ] ; then

or

if  [ -n "$passwd" ] ; then

0
 

Author Closing Comment

by:ibanja
ID: 34214303
Not sure where I got the "-v" from.

Thanks!
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question