Re: A test command parameter is not valid. (548 Views)
Reply
Super Advisor
Patrick Ware_1
Posts: 216
Registered: ‎12-14-2004
Message 1 of 8 (548 Views)
Accepted Solution

A test command parameter is not valid.

In the script output below I choose a menu option (5) that adds Zebra printers to servers. I have a if-then-else statement setup to compare the server name entered against valid server names. If a server that is not valid is entered, then the script tells the user that the server selected is not a valid server, and exits, as shown below:

[BEGIN SCRIPT OUTPUT]
/home/user/scripts
Sat Apr 11 22:37:28 CDT 2009
This script adds to, and deletes printers from HP-UX servers using Jetdirect.
There are also options to delete a queue, or queues only.


Printer Script Menu

1. Add A Single Printer To One Or More Servers.
(Deletes the queue first if it already exists.)

2. Delete A Single Printer From One Or More Servers.

3. Add Multiple Printers To One Or More Servers.
(Deletes the queue first if it already exists.)

4. Delete Multiple Printers From One Or More Servers.

5. Add A Zebra Printer To One Or More Valid Servers.

6.Exit

V 1.7 4/11/2009



Enter your selection:5

The printer must have an entry in /etc/hosts of the server(s) you plan
to add the zebra printer to if the printer is to work.

Does the printer have an entry in /etc/hosts of the server(s) you plan
to add the zebra printer to?
Enter 'y' or 'n':y


Enter printer queue name:test

Enter server host names separated by a blank space. ex.(server1 server2 server3):
server15

One or more hostnames you have selected is not a valid server!!!

[END SCRIPT OUTPUT]

The problem comes when I put in one server that is valid, and one that is not. In the output below, server15 is invalid, while server1 is valid. I get a test parameter error:

[BEGIN SCRIPT OUTPUT]
/home/user/scripts
Sat Apr 11 22:38:47 CDT 2009
This script adds to, and deletes printers from HP-UX servers using Jetdirect.
There are also options to delete a queue, or queues only.


Printer Script Menu

1. Add A Single Printer To One Or More Servers.
(Deletes the queue first if it already exists.)

2. Delete A Single Printer From One Or More Servers.

3. Add Multiple Printers To One Or More Servers.
(Deletes the queue first if it already exists.)

4. Delete Multiple Printers From One Or More Servers.

5. Add A Zebra Printer To One Or More Valid Servers.

6.Exit

V 1.7 4/11/2009



Enter your selection:5

The printer must have an entry in /etc/hosts of the server(s) you plan
to add the zebra printer to if the printer is to work.

Does the printer have an entry in /etc/hosts of the server(s) you plan
to add the zebra printer to?
Enter 'y' or 'n':y


Enter printer queue name:test

Enter server host names separated by a blank space. ex.(server1 server2 server3):
server15 server1
NEWtlg_printer.sh[34]: server1: A test command parameter is not valid.
One or more hostnames you have selected is not a valid server!!!
[END SCRIPT OUTPUT]

Here is the code that comprises option 5 in the menu. Please help me correct this, or find a better way to implement this:

[CODE]
function zebra_printer_func
{
echo
echo "The printer must have an entry in /etc/hosts of the server(s) you plan"
echo "to add the zebra printer to if the printer is to work."
echo
echo "Does the printer have an entry in /etc/hosts of the server(s) you plan"
echo "to add the zebra printer to?"
print -n "Enter 'y' or 'n':"
read CHOICE
case "$CHOICE" in
y|yes|Yes|YES) echo
continue ;;
n|no|No|NO) echo
echo "Please correct before continuing!!"
sleep 04
main_menu_func ;;
*) clear
zebra_printer_func ;;
esac
echo
print -n "Enter printer queue name:"
read QNAME
echo
echo "Enter server host names separated by a blank space. ex.(server1 server2 server3):"
read HNAME
echo
if [ $HNAME = server1 -o $HNAME = server2 -o $HNAME = server3 -o $HNAME = server4 -o $HNAME = server5 ]
then
continue
else
echo 'One or more hostnames you have selected is not a valid server!!!'
sleep 04
main_menu_func
fi
for i in $HNAME ; do
(
echo "ssh $HNAME -n /usr/sbin/lpadmin -p$QNAME -v/dev/null -mrmodel -ob3 -ocmrcmodel -osmrsmodel -orc -orm$QNAME -orp$QNAME"
ssh $HNAME "/usr/sbin/lpshut ; /usr/sbin/lpadmin -p$QNAME -v/dev/null -mrmodel -ob3 -ocmrcmodel -osmrsmodel -orc -orm$QNAME -orp$QNAME ; \
/usr/sbin/accept $QNAME ; /usr/bin/enable $QNAME ; /usr/sbin/lpsched"
)
done
}
[/CODE]
Please use plain text.
Super Advisor
Patrick Ware_1
Posts: 216
Registered: ‎12-14-2004
Message 2 of 8 (548 Views)

Re: A test command parameter is not valid.

This link has a better visual representation of what is going on:

http://www.unix.com/shell-programming-scripting/107003-test-command-parameter-not-valid.html
Please use plain text.
Honored Contributor
Steven Schweda
Posts: 9,075
Registered: ‎02-23-2005
Message 3 of 8 (548 Views)

Re: A test command parameter is not valid.

Look at what's in HNAME. Multiple names,
space-separated, right?

What does this look like:?

if [ $HNAME = server1 [...]

Perhaps something like:

if [ server1 server2 server3 = server1 [...]

Too many tokens for the "[" ("test") syntax.

You seem to be expecting HNAME to be one name
("if [ $HNAME = server1 ") and all the names
("for i in $HNAME"). It seems unlikely to
work both ways.

1. Use "-x" or "-v" to get the shell to tell
you what it's doing.

2. Trust no one, especially users. When a
variable contains a user-supplied value, you
should probably use (quoted) "$variable"
instead of (plain) $variable, so that you
will be treating the whole thing as an entity
rather than as some mess of unknown content
supplied by the user, unless you actually
wish to break it into pieces, as in
for x in $variable ; do
.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 4 of 8 (548 Views)

Re: A test command parameter is not valid.

As Steven said multiple names will mess you up. If you want to solve your error with test(1) you should do some of the following:
if [ "$HNAME" = server1 -o ... ];

The pedantic level to allow for leading "-":
if [ X"$HNAME" = Xserver1 -o ... ];

If you have lots of tests you may want to use case:
case $HNAME in
server1|server2|server3|...)
: ;;
*)
echo 'One or more hostnames you have selected is not a valid server!!!' ;;

And if you want to do several names, you should use a for statement.

You might look into a select statement, that will list all of the valid selections, so there is no need to check.

select HNAME in server1 server2; do
... # check for validity
done

This only does one at a time.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 8 (548 Views)

Re: A test command parameter is not valid.

Hi Patrick:

As noted, develop a defensive posture when coding 'test's in the shell by double quoting your variables:

# X= ;[ ${X} = something ] && echo ${X} || echo "tell me what"
sh: test: Specify a parameter with this command.
tell me what

# X= ;[ "${X}" = something ] && echo ${X} || echo "tell me what"
tell me what

Secondly, use 'typeset' to enhance your code. Typeseting variables to integers (with '-i') improves arithmetic performanace. Typeseting with '-l' lowercases the variable contents and makes for less tests in code like:

...
typeset -l CHOICE
read CHOICE
case ${CHOICE} in
y|yes ) echo OK
;;
* ) echo try again
;;
esac
...

Notice that "yes" or "Yes" or "YES" or "yEs", etc. are now equally valid without having to state every permutation.

Regards!

...JRF...
Please use plain text.
Super Advisor
Patrick Ware_1
Posts: 216
Registered: ‎12-14-2004
Message 6 of 8 (548 Views)

Re: A test command parameter is not valid.

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 7 of 8 (548 Views)

Re: A test command parameter is not valid.

>Having more fun with the script.

You need to either combined the for loops or have them do the same:
for HNAME in ${HNAMES}; do
# checking here
...
main_menu_func
done

# this will have the last one
echo ${HNAME} # Checking the HNAME variable

# Change names here:
for HNAME in ${HNAMES}; do

The call to main_menu_func in the first for loop may cause a problem if you don't return or exit that function. It is probably better to combine the loops so you still do the valid servers.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 8 (548 Views)

Re: A test command parameter is not valid.

HI:

In your external link you wrote:

> It only sees my last list item, and only sets it as the variable

You have:

for HNAME in ${HNAMES}
...
for i in $HNAME
...

If you want to walk the list of ${HNAMES} twice, then at least use the same list :-) The second statement should/could be:

for HNAME in ${HNAMES}

...not the last element ${HNAME} from the first iteration.

Regards!

...JRF...
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation