Operating System - HP-UX
1834000 Members
2050 Online
110063 Solutions
New Discussion

how can I improve this shell script - remove warning messages

 
Manuel Contreras
Regular Advisor

how can I improve this shell script - remove warning messages


We have impmented sudo, and have been creating some scripts/tools witch datactr personnel could utilize to perform certin tasks.

One of which is creating unix accounts.

The following is an example of one, which is used to reset a users password if they forgot it (which rarely happens).



# cat setwelcome.sh

#!/bin/ksh

user=$1

checkID=`grep "$user\:" /etc/passwd `

if [ -z "$checkID" ]
then
echo " **================================================================**"
echo " I am sorry, but that Users ID is NOT valid - $user "
echo " **================================================================**"
exit
fi

if [ $user = "root" ]
then
echo " **================================================================**"
echo " I am sorry, but you can not change that account "
echo " **================================================================**"
exit
fi

if [ !-n $user ]
then
echo " **================================================================**"
echo " I am sorry, but that ID is NULL "
echo " **================================================================**"
echo ""
exit
fi

cp /etc/passwd /etc/passwd.OLD

echo " **================================================================**"
echo " you will NOW be prompted for a passwd for user: $user "
echo ""
echo " the password value will have to be entered twice"
echo " **================================================================**"
echo ""
echo ""

/usr/bin/passwd $user

echo ""
echo " **================================================================**"
echo " the following Users Unix/TPS password has been reset "
echo " User ID: $user "
echo " **================================================================**"
echo ""

exit 0


If I run it manually without the expected userID, I get the correct exit on a NULL value, but I also get a warning on an expected value (userid):

# ./setwelcome.sh
./setwelcome.sh[19]: test: argument expected
**================================================================**
I am sorry, but that ID is NULL
**================================================================**



If I run the script with a userID, I get an error because the userID is NOT null :

# ./setwelcome.sh test
./setwelcome.sh[27]: test: argument expected
**================================================================**
you will NOW be prompted for a passwd for user: test

the password value will have to be entered twice
**================================================================**




This script works, I would just like to improve it so that I (datactr) no longer get warning messages listed above.
any input is appreciated,

manny


5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: how can I improve this shell script - remove warning messages

Hi Manny:

Change:

if [ !-n $user ]

...to:

if [ !-n "$user" ]

It is good practice to surround the argument to a 'test' with double quotes. Then, if the variable is null, you don't end up with:

if [ !-n ]

Regards!

...JRF...
Massimo Bianchi
Honored Contributor

Re: how can I improve this shell script - remove warning messages

hi,
the test on the null value of user must be the first test.

Anticipate that !


Other way to test:

put a

set -x

as the second line, so you would see where is the problem by yourself .

HTH,
Massimo

Manuel Contreras
Regular Advisor

Re: how can I improve this shell script - remove warning messages

James, If I put the variable in "" (if [ !-n "$user" ]
), It didn't work:

# ./setwelcome.sh
./setwelcome.sh[19]: test: argument expected
./setwelcome.sh[27]: test: argument expected
**================================================================**
you will NOW be prompted for a passwd for user:

the password value will have to be entered twice
**================================================================**


Changing password for root
New password:


Notice it prompts for new password for root account (reason for null checker).



Massimo,
I thought of moving the null checker to the beginning, but I still get warnings on the tested NULL value:

# ./setwelcome.sh tuner
./setwelcome.sh[11]: test: argument expected
**================================================================**
you will NOW be prompted for a passwd for user: tuner

the password value will have to be entered twice
**================================================================**


Changing password for tuner
New password:




Thanks for your input,
manny
James R. Ferguson
Acclaimed Contributor

Re: how can I improve this shell script - remove warning messages

Hi (again) Manny:

...
if [ "$user" = "root" ]
...
if [ ! -n "$user" ]
...

Note the space between the negation and the '-n'.

Regards!

...JRF...
Manuel Contreras
Regular Advisor

Re: how can I improve this shell script - remove warning messages

ding,ding,ding,ding....that was it!

Thanks,
manny





# ./setwelcome.sh
**================================================================**
I am sorry, but that ID is NULL
**================================================================**

#
# ./setwelcome.sh tuner
**================================================================**
you will NOW be prompted for a passwd for user: tuner

the password value will have to be entered twice
**================================================================**


Changing password for tuner
New password: