The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk/sed Command : Parse parameter file / send the lines to the ksh export command rajan_san Shell Programming and Scripting 4 11-06-2008 01:29 PM
Passing Parameter in SED command dsrookie UNIX for Dummies Questions & Answers 2 06-09-2008 01:25 AM
Help regarding Error message: A test command parameter is not valid skyineyes Shell Programming and Scripting 5 06-29-2007 05:38 AM
Using of Top command as script parameter elthox Shell Programming and Scripting 4 01-31-2007 01:21 PM
pwd: A specified flag is not valid for this command. Paxton UNIX for Dummies Questions & Answers 2 08-29-2003 12:56 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-12-2009
LinuxRacr LinuxRacr is offline
Registered User
  
 

Join Date: Aug 2007
Location: DFW, Texas
Posts: 60
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:

Code:
/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!!!
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:

Code:
/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!!!
Here is the code that comprises option 5 in the menu. The if-then-else statement is in bold. 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
         }
  #2 (permalink)  
Old 04-12-2009
TonyFullerMalv's Avatar
TonyFullerMalv TonyFullerMalv is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Location: Malvern, Worcs. U.K.
Posts: 730
It sounds like the shell is running "test" rather than treating it as a string (if is also test to just to confuse matters), althiough that is the queuename not the servername!? I would always write the line:
Code:
if [ $HNAME = server1 -o $HNAME = server2 -o $HNAME = server3 -o $HNAME = server4 -o $HNAME = server5 ]
with each $HNAME as "${HNAME}" so that if HNAME the if test does not fail but I don't think that is a problem you may has a mismatched pair of " or ' or `, try running:
Code:
# bash -n script
replacing bash with the shell you have declared at the top of the script and ensure that reports no errors?

The code is also are not handling the entering of two server names in your script, you need something like:
Code:
read HNAMES
echo
for HNAME in ${HNAMES}; do
  Your code here
done

Also try a different queuename than test!

Last edited by TonyFullerMalv; 04-12-2009 at 10:55 AM..
  #3 (permalink)  
Old 04-12-2009
LinuxRacr LinuxRacr is offline
Registered User
  
 

Join Date: Aug 2007
Location: DFW, Texas
Posts: 60
Thanks for your input. I've fixed the test error I was getting. Now I have another issue. Now when I enter two or more VALID servers, I get the a reply telling me that one or more of my server names are invalid. Here is the output:

Code:
/home/user/scripts
Sat Apr 12 12:34:14 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):
server1 server2

One or more hostnames you have selected is not a valid server!!!
Here is the updated code:

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 HNAMES
echo
if [ "$HNAMES" = server1 -o "$HNAMES" = server2 -o "$HNAMES" = server3 -o "$HNAMES" = server4 -o "$HNAMES" = server5 ]
  then
   continue
else
  echo 'One or more hostnames you have selected is not a valid server!!!'
  sleep 04
  main_menu_func
fi

for HNAME in $HNAMES ; 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
         }
  #4 (permalink)  
Old 04-12-2009
sauron's Avatar
sauron sauron is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 33
Hi
the error occours because when you write "server1 server2" $HNAME will be "server1 server2" and not "server1" "server2" so when you test you variable with the if statement, you are asking what follow:

if [ "server1 server2 " = server1 ]

and it will be always false.

Tony before told you to use "for" to read your HNAME

for SERVERS in $HNAME; do

here you can put you if statement because you are reading one input for iteration

done

hope it helps

Bye Bye

------------------------------------
Computers are like air-conditioners
they stop working properly
when you open windows


  #5 (permalink)  
Old 04-12-2009
TonyFullerMalv's Avatar
TonyFullerMalv TonyFullerMalv is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Location: Malvern, Worcs. U.K.
Posts: 730
As Sauron has said with the latest mod to the code HNAMES is going to equal "server1 server2"; the line:
Code:
for HNAME in $HNAMES ; do
line need to go before the:
Code:
if [ "$HNAMES" = server1 -o "$HNAMES" = server2 -o "$HNAMES" = server3 -o "$HNAMES" = server4 -o "$HNAMES" = server5 ]
line so that you are testing the user entered server names one at a time and change the above quoted line back to testing "${HNAME}" not "${HNAMES}".

Either that of you are going to have to test for all variations of "server1 server2", "server2 server1", "server1 server2 server3", etc!

Last edited by TonyFullerMalv; 04-12-2009 at 04:14 PM..
  #6 (permalink)  
Old 04-13-2009
LinuxRacr LinuxRacr is offline
Registered User
  
 

Join Date: Aug 2007
Location: DFW, Texas
Posts: 60
Ok, I tried that. The latest issue I am having is that now when I enter more than one valid server, it only keeps the last server in the list. I set up the script so that it would echo what the $HNAME is. See my example below :

Code:
/home/user/scripts
Sun Apr 12 23:30:21 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):
server1 server2 server3

server3 #It only sees my last list item, and only sets it as the variable
Here is the updated code:

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 HNAMES
echo
for HNAME in ${HNAMES}
do
  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
done
echo ${HNAME} #Checking the HNAME variable
exit #Inserted exit so that the script will stop here..
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
         }
  #7 (permalink)  
Old 04-13-2009
sauron's Avatar
sauron sauron is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 33
hi,
if you put "echo "$HNAME" out of the for cicle, it is obvious you will see just the last server the cicle reads.

Try putting an echo "$HANME" inside the cicle.
The second cicle (for i in $HNAME) it is wrong because $HNAME out of the first cicle takes the last value of the cicle, you should use (for i in $HNAMES) because $HNAMES is the user inserted list of servers.

Bye
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0