"unexpected operator" error in sh script

Posted on 2010-11-25
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...


# Define Version Number & Description
desc="Wrapper for vmrun"
program=`echo $0 |sed -e 's#^\./##'`
# Define help
$program $version, $desc\n
Usage: $program \[Options\] command\n
Example: $program -p > command\n
Options \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

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

# 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;;
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"
    command="vmrun -T server -h http://$host:8222/sdk"

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

Open in new window

Question by:ibanja
LVL 23

Assisted Solution

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

Accepted Solution

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

should be

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


if  [ -n "$passwd" ] ; then


Author Closing Comment

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


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a python script 5 87
AWS CLI issues with Tags 3 66
Using an inventory file in a script 7 14
Run same command on multiple files in Linux 3 7
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now