lunes, 11 de enero de 2016

bash checking vm

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

vmuser=1234
outnum=18097143489
trunk=rapidvox
echo $vmuser
context=internal
vmnum=$(asterisk -x " voicemail show users" | grep -i $vmuser | awk '{print $5}' | awk 'NR==2')

#`asterisk -x "originate SIP/$1 extension $2@$context"`
if [ $vmnum -eq 0 ]
then
echo "no new voicemail, number of voicemail is equal to $vmnum $(date)"

else
echo "yo got $vmnum voicemail $(date)"
#`asterisk -x "originate SIP/$trunk/$outnum extension s@$context"`

fi

Bash: Meaning of “[: too many arguments


I couldn't find any one simple straightforward resource spelling out the meaning of and fix for the following BASH shell error, so I'm posting what I found after researching it.
The error:
-bash: [: too many arguments
Google-friendly version: bash open square bracket too many arguments.
Context: an if condition in single square brackets with a simple comparison operator like equals, greater than etc, for example:
VARIABLE=$(/some/command);
if [ $VARIABLE == 0 ]; then
  # some action
fi 
shareimprove this question
1 
Where is the code that produced this specific error? – Anderson Green Jun 17 '13 at 22:39

2 Answers

up vote90down voteaccepted
If your $VARIABLE is a string containing spaces or other special characters, and single square brackets are used (which is a shortcut for the test command), then the string may be split out into multiple words. Each of these is treated as a separate argument.
So that one variable is split out into many arguments.
The same will be true for any function call that puts down a string containing spaces or other special characters.

Easy fix

Wrap the variable output in double quotes, forcing it to stay as one string (therefore one argument). For example,
VARIABLE=$(/some/command);
if [ "$VARIABLE" == 0 ]; then
  # some action
fi 

An alternate fix is to use double square brackets (which is a shortcut for the new test command).
This exists only in bash (and apparently korn and zsh) however, and so may not be compatible with default shells called by /bin/sh etc. This means on some systems, for example, it might work from the console but not from cron, depending on how everything is configured.
It would look like this:
VARIABLE=$(/some/command);
if [[ $VARIABLE == 0 ]]; then
  # some action
fi 
http://stackoverflow.com/questions/13781216/bash-meaning-of-too-many-arguments-error-from-if-square-brackets



echo "*file 2" | grep -o ^. prints *.
Since you have a command substitution outside double quotes, it undergoes globbing (a.k.a. wildcard matching a.k.a. filename generation) and word splitting. If the current directory is not empty, *expands to the list of files in the current directory. Each file becomes one token in the [ command, which is highly likely to be a syntax error.
The problem is that you didn't use double quotes around the command substitution. Always use double quotes around variable and command substitutions unless you have a good reason to omit them.
if [ "$(echo "*file 2" | grep -o ^.)" = '.' ]
http://unix.stackexchange.com/questions/162616/why-am-i-getting-too-many-arguments