1753936 Members
9777 Online
108811 Solutions
New Discussion юеВ

Re: Script input

 
SOLVED
Go to solution
James R. Ferguson
Acclaimed Contributor

Re: Script input

Hi (again):

You can/should reward points to all respondents of your thread to the degree that you feel they helped you:

http://forums11.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...
Charles Keyser
Frequent Advisor

Re: Script input

JRH

My apologies to you. I did add points to yours also and failed to acknowledge that

Chris

Maybe I am missing something here or my skills are just lacking I made the changes as you sent
here is what I receive now

select option for password reset

Username to modify cjk1402
cjk1402 is invalid!
Press [ENTER] to continue

Since I am a user why would I receive the the invalid user?

I did a cat on /etc/passwd and my user id is there.

-Charlie
Chris Vail
Honored Contributor

Re: Script input

Instead of 'cat-ing' /etc/passwd, try grep'ing it with the following:

grep $USER /etc/passwd|awk -F: '{ print $1 }'

The grep command matches the input string $USER with any entry in /etc/passwd. The awk portion of the command sets the colon (:) as the field delimiter and then prints the first field in the returned line.

So the grep command might return something like:

user1:WSzIkVWhiLvCM:1154:20::/users/user1:/bin/ksh

The first field is the username. Then comes the encrpyted password (unless you are using a trusted system) then the user number (UID), then the group ID (GID), then the home directory for user1, and the last field is the shell that user1 will use on login. You are interested ONLY in the first field.

So try this experiment......
First use grep to find your user ID. It must be an exact match or it won't work.
Then try the grep command in combination with the awk command I printed above. If the grep command worked, then the awk command should also.

It is possible to do the entire line with a single awk command, but I use grep as a matter of habit.

Chris
James R. Ferguson
Acclaimed Contributor

Re: Script input

Hi (again) Charlie:

Chris's code mixes variable names -- TESTUSER and TESTNAME -- and they are not the same!

Try this:

#!/usr/bin/sh
set -u
echo "Username to modify \c"; read USER
TESTUSER=`awk -v USER=${USER} -F: '$1~USER { print $1 }' /etc/passwd`
if test "$USER" != "$TESTUSER"
then
echo "$USER is invalid!"
echo "Press [ENTER] to continue. \c"
read NOTHING
else
echo $USER
fi

...

Note that I added 'set -u' to the script to catch undefined (empty) variables. This makes debugging so much easier!

Note too that I removed a 'grep'/'awk' pipeline. Awk pattern matches and the 'grep' process is wasteful.

Regards!

...JRF...
Charles Keyser
Frequent Advisor

Re: Script input

JRF

here is the complete script with changes. I ran it and it still says, maybe I have typed something incorrectly? -Charlie

Select an Option # from above: 5
Username to modify cjk1402
cjk1402 is invalid!
Press [ENTER] to continue.

Here is what I have done from the command line to verify if I am in the passwd file


grep cjk1402 /etc/passwd|awk -F: '{ print $1 }'

here is the return

cjk1402


++++++++++++++++++++++++++++++++++++++++++++

trap '' INT
function cont
{
print -n "Do You Wish to Continue Y/N : "
read answ
if [[ $answ = [Yy] ]]
then
return 0
else
return 1
fi
}
while true
do
clear
print -n "
* HELP DESK MENU *
$(uname -n)
HELLO: $(whoami)
===================================================
1. Run Restricted SAM to reset passwords.

2. Display printer status / print jobs.
a. Enter lpstat -p to view all printers
b. Enter lpstat -o to view all print request

3. Cancel a print job.

4. Cancel ALL print jobs for a printer.

5. Reset User Passwords

6. EXIT this program and RETURN TO COMMAND LINE.


===================================================

Select an Option # from above: "

read answer
case "$answer" in
6*|Qq|bye|Ee ) print "Quitting! See You Later, $(whoami)" ; exit ;;
1) if cont
then
. /usr/sbin/sam
fi;;
2) if (( $? == 0 ))
then
print -n "Enter Printer Name: "
read prtr
lpstat $prtr
print -n "OK to clear screen [Hit Any Key]"
read h
fi;;
3) if (( $? == 0 ))
then
print -n "Enter Printer Name-job#: "
read prtr
cancel $prtr
fi;;
5) echo "Username to modify \c"; read USER
TESTNAME=`awk -v USER=${USER} -F: '$1~USER { print $1 }' /etc/passwd`
if test "$USER" != "$TESTUSER"
then
echo "$USER is invalid!"
echo "Press [ENTER] to continue. \c"
read NOTHING
else
echo $USER
fi;;
4) if (( $? == 0 ))
then
print -n "..Enter printer name :"
read prtr_name
print -n "You enter printer ${prtr_name}"
print -n " Is this correct Y/N "
read answ
if [[ "$answ" = [Yy] ]]
then
lpstat $prtr_name | grep $prtr_name | sort -k1 | cut -d " " -f1 > $prtr_name.out
for rem_prt in `cat $prtr_name.out`
do
cancel $rem_prt
done
else
echo "No Such Printer"
fi



fi;;
esac
done
James R. Ferguson
Acclaimed Contributor

Re: Script input

Hi (again) Charles:

Ah, regarding you last post. Put the following two lines at the top of your script and run it again :-)

#!/usr/bin/sh
set -u
...

The first line is good practice -- to declare the interpreter you want to use. It avoids ambiguity.

The second line exposes your error.

Select an Option # from above: 5
Username to modify xxx
/tmp/0807[65]: TESTUSER: Parameter not set.

Once again, you have TESTUSER and TESTNAME:

# grep TEST /tmp/0807
TESTNAME=`awk -v USER=${USER} -F: '$1~USER { print $1 }' /etc/passwd`
if test "$USER" != "$TESTUSER"

...consistency counts :-)

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: Script input

Shalom,

I find set -x is useful as well in spotting scripting errors.

Your last mistake is merely from experience and requires you merely to become familiar with the tools in this thread and use them in future scripting work.

Very interesting thread BTW.

Good luck with your debug.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Charles Keyser
Frequent Advisor

Re: Script input

Steven Thanks for your input

JHR

Well I guess I am lost at this point. Trying to be constant

Do I need to replace the TESTNAME with TEST USER?
Do I need to create this id. I guess I need to go back to the books and read. Good thing I have 3 months to have this written and ready.

Thanks

-Charlie



James R. Ferguson
Acclaimed Contributor

Re: Script input

Hi Charlies:

> Do I need to replace the TESTNAME with TEST USER?

Chose either name for your variable --- TESTNAME or TESTUSER. If it was up to me, I'd use "TESTUSER" since you are comparing the value of ${TESTUSER} to ${USER}:

#!/usr/bin/sh
set -u
echo "Username to modify \c"; read USER
TESTUSER=`awk -v USER=${USER} -F: '$1~USER { print $1 }' /etc/passwd`
if test "${USER}" != "${TESTUSER}"
then
echo "${USER} is invalid!"
echo "Press [ENTER] to continue. \c"
read NOTHING
else
echo ${USER}
fi

...note that for good standards, I enclosed the variables in curly braces when I wanted to evaluate their value. This is a good habit, in my opinion, to form, although it isn't required unless you do:

# USER=charlie
# PASS=hisword
# echo $USER_$PASS
sh: USER_: Parameter not set.
# echo ${USER}_${PASS}
charlie_hisword

Regards!

...JRF...
Charles Keyser
Frequent Advisor

Re: Script input

JRF

Added everything ran it, was able to scroll up and see the results. no errors

Now once this is done, and ran, I want to have it prompt me to change password or reset account would after the last line (see below)


set -u
echo "Username to modify \c"; read USER
TESTUSER=`awk -v USER=${USER} -F: '$1~USER { print $1 }' /etc/passwd`
if test "${USER}" != "${TESTUSER}"
then
echo "${USER} is invalid!"
echo "Press [ENTER] to continue. \c"
read NOTHING
else
echo ${USER}
passwd