Operating System - HP-UX
1846152 Members
5211 Online
110254 Solutions
New Discussion

Selection option within a script

 
SOLVED
Go to solution
fg_1
Trusted Contributor

Selection option within a script

Hello all

I have this script here which selects all printers within the /etc/lp/interface directory and performs an hpnpadmin command against each one, then puts the output to screen.

What I am trying to figure out here is how to modify this script so that I can possibly run the script and select an individual printer for querying, in other words allow an input on the screen asking for the printer queue name to lookup.

ex: printstatus mv2itd08

As the current script is written it will select all printers due to the path listed as /etc/lp/interface/*.

Any idea's.

TYIA.
14 REPLIES 14
Todd Lehr
Frequent Advisor
Solution

Re: Selection option within a script

If you use the above syntax then in your script the env variable $1 will contain mv2itd08 which is the first command line option.

You could use something like

if [ "$1" = "" ]
then
run stuff just for one printer
else
do something else to check every printer
fi

If you want to have the script ask for the printer name you can use

echo "Enter Printer Name:\c"
read printer_name

which will allow the user to enter the printer name and this will place the entered name into the variable $printer_name.

Then use something similar to the above if then else like this

if [ "$printer_name" = "all" ]
then
do something for all printers
else
do something for specific printer
fi

beyond this, I would need to see the script to give more exact suggestions
steven Burgess_2
Honored Contributor

Re: Selection option within a script

Frank

echo "Please enter the name of the printer wish to query\c"
read printer
printstatus $printer

?

HTH

Steve
take your time and think things through
Jordan Bean
Honored Contributor

Re: Selection option within a script


Perhaps this may help:

#!/usr/bin/sh
cd /etc/lp/interface || exit 1
if [ $# -eq 0 ]
then
set -- *
fi
for p in $@
do
if [ -x $p ]
then
hpnpadmin $p ...
fi
done
James R. Ferguson
Acclaimed Contributor

Re: Selection option within a script

Hi:

I'd probably do something like this:

#!/usr/bin/sh
if [ $# -eq 1 ]
then
echo "do this printer ---> $1"
else
echo "do_all_printers"
fi
exit 0

Regards!

...JRF...
fg_1
Trusted Contributor

Re: Selection option within a script

My appologies all I should have posted the script.

Here it is:

mvitux01@/opt/CHS/root/bin# more printstatus
#! /usr/bin/ksh

# This script is utilized to gather printer information on all print queue's installed
# on a system using the hpnpadmin command.

# Environmental section

set -u

BASEDIR=/opt/CHS/root/bin
DATE=`date +%m/%d/%Y`
YDATE=`TZ=CST+24 date +%m/%d/%Y`
SCRIPT_EXECUTION_TIME=$(date +'%H:%M:%S')
SCRIPT_REVISION="HPUX-b.11.00.01"
SCRIPT_HPUX_VERSION=$(uname -r)
SCRIPT_SYSTEM=$(uname - n)
SCRIPT_USER=$(whoami)
SCRIPT_EXECUTION_TIME=$(date +'%Y/%m/%d/%H:%M:%S')

echo $SCRIPT_SYSTEM \(Version $SCRIPT_REVISION\) $SCRIPT_EXECUTION_TIME
echo _____________________________________________________________________
echo
if [ ${SCRIPT_USER} != "root" ] ; then
echo
echo "This program must be executed utilizing the root logon id only"
echo
return 501
fi
echo _______________________________________________________________________


# Gather information about printer queue's on system.
##################################################################################

cd /etc/lp/interface
for i in $(grep "PERIPH=" /etc/lp/interface/* |grep -v "MODEL")
do
qname=`echo $i | cut -d: -f 1 | cut -d/ -f 5`
ip=`echo $i | cut -d= -f 2`
echo Queue name: $qname
echo Printer name / IP address: $ip
hpnpadmin -iqS $ip
echo ""
echo ""
done
mvitux01@/opt/CHS/root/bin#
Jordan Bean
Honored Contributor

Re: Selection option within a script


Looking at:

cd /etc/lp/interface
for i in $(grep "PERIPH=" /etc/lp/interface/* |grep -v "MODEL")
do
qname=`echo $i | cut -d: -f 1 | cut -d/ -f 5`
ip=`echo $i | cut -d= -f 2`
echo Queue name: $qname
echo Printer name / IP address: $ip
hpnpadmin -iqS $ip
echo ""
echo ""
done


I suggest doing it this way:

cd /etc/lp/interface || exit 1
if [ $# -eq 0 ]
then
set -- *
fi
grep -l '^PERIPH=' $@ | while IFS=':=' read qname junk1 ip junk2
do
cat <<-EOF

Queue name: $qname
Printer name / IP address: $ip
$(hpnpadmin -iqS $ip)

EOF
done
Jordan Bean
Honored Contributor

Re: Selection option within a script


Correction, drop the -l from the grep... I was working with two ideas and chose the second. Sorry about that.
curt larson_1
Honored Contributor

Re: Selection option within a script

if you want to use the select command try something like this

printernames=$(grep "PERIPH=" /etc/lp/interface/* |grep -v "MODEL")

print "Select the number of the printer(s) blah ...\n"
PS3="Enter the number of your selection here ->\c"
invalid='true'
while $invalid = 'true'
do
select i in $printernames "All Printers"
do case $i in
'a printer name')
invalid='false'
hpnpfunction $i
break;;
'All Printers') for j in $printernames
do
invalid='false'
hpnpfunction $j
done
*) print -u2 'You've entered an invalid number.'
esac
done
done

Thomas M. Williams_1
Frequent Advisor

Re: Selection option within a script

In regards to my last reply:
I incorrectly typed the wrong function name as "select_printer" instead of "single_printer".
I Think the Clock is Slow ...
Tom Danzig
Honored Contributor

Re: Selection option within a script

I have a similar type script shown below which may offer some assistance:

#!/usr/bin/sh
let arg=0

cd /etc/lp/interface

if [[ "$1" = "" ]] ; then
list="`ls`"
else
list="$*"
let arg=1
fi

for i in $list
do
if [[ -d $i ]] ; then
continue
fi
/opt/hpnpl/bin/ishpnp $i
if [[ $? -eq 0 ]] ; then
printf "%-10s is a JetDirect printer - " $i
/opt/hpnpl/bin/hpnpadmin -E $i
else
if [[ $arg -eq 1 ]] ; then
printf "ERROR: %10s is NOT a JetDirect printer\n" $i
fi
:
fi
done

exit
Thomas M. Williams_1
Frequent Advisor

Re: Selection option within a script

Please excuse this message if you have already received it. I wasn't sure if you got it.

#!/bin/ksh

# Defaults

PS3="Please select desired printer: "
fulllist="`cd /etc/lp/interface;ls`"

all_printers ()
{
return
# This is where your current syntax goes.
# Ex: hpnpadmin ${fulllist} If you can send all devices into the command ?
}

select_printer ()
{
select prtname in ALL Quit ${fulllist}
do
[ "${prtname}" = "Quit" ] && exit 0

if test -n "${prtname}"
then
if test "${prtname}" = "ALL"
then
prtlist="${fulllist}"
else
prtlist="${prtname}"
fi

break
fi
done

# Put the syntax for running the hpnpadmin
# Ex: hpnpadmin ${prtlist}
}

# Main

if test ${#} -ne 0
then
while getopts :s option
do
case ${option} in
s) select_on="Y" ;;
?) echo "Usage: ${0} [<-s>] (-s turns select option on)."
exit 4 ;;
esac
done
fi

[ "${select_on}" = 'N' ] && all_printers || single_printer

exit 0
I Think the Clock is Slow ...
Thomas M. Williams_1
Frequent Advisor

Re: Selection option within a script

Just a note:
remove the "return" in the "all_printers" function. It was only there for me to run the script to check for syntax errors.
I Think the Clock is Slow ...
fg_1
Trusted Contributor

Re: Selection option within a script

Thomas

Where you say the statement:

#This is where your current syntax goes.
# Ex: hpnpadmin ${fulllist} If you can send
# all devices into the command?

What do you mean by that statement, and what part of my original script do I put in there.
Sorry but im not clear on that part of it.
fg_1
Trusted Contributor

Re: Selection option within a script

Thomas

It's also giving me an error that says:

select_on parameter not set.

I thought that part was set by the script already.