- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- how can I improve this shell script - remove warni...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2003 08:38 AM
05-28-2003 08:38 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2003 08:45 AM
05-28-2003 08:45 AM
Re: how can I improve this shell script - remove warning messages
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2003 08:49 AM
05-28-2003 08:49 AM
Re: how can I improve this shell script - remove warning messages
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2003 08:59 AM
05-28-2003 08:59 AM
Re: how can I improve this shell script - remove warning messages
), 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2003 09:12 AM
05-28-2003 09:12 AM
Re: how can I improve this shell script - remove warning messages
...
if [ "$user" = "root" ]
...
if [ ! -n "$user" ]
...
Note the space between the negation and the '-n'.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2003 09:23 AM
05-28-2003 09:23 AM
Re: how can I improve this shell script - remove warning messages
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: