Link to home
Start Free TrialLog in
Avatar of Jason Yu
Jason YuFlag for United States of America

asked on

How to understand this line:"if [ -z "`echo $usernam`" -o "`echo $usernam`" = "root" -o "`echo $usernam`" = "sa" ]"

I have a script created for help desk technician to create new users. I don't understand this line:

"if [ -z "`echo $usernam`" -o "`echo $usernam`" = "root" -o "`echo $usernam`" = "sa" ]"

Also, if I need check if a user exists in /etc/passwd file, how could I check it?

Thanks.



#!/bin/ksh

Newusr()
{
echo 
echo '***** New User *****'
echo
echo 'Enter New Username (3-letter ID): '
read usernam
if [ -z "`echo $usernam`" -o "`echo $usernam`" = "root" -o "`echo $usernam`" = "sa" ]
then
exit 0
fi
echo '\n'
echo 'Enter Description for Username' $usernam ':'
read gecoss
echo 
echo 'New User' $usernam 'being added'
/usr/bin/mkuser pgrp='lac' groups='lac,staff' home='/u1/dbms/LIVE.ENTRY' shell='/usr/bin/ksh' gecos="$gecoss" maxage=8 minlen=8 $usernam
echo 'New user '$usernam' created'
echo 'Press <Enter> to return to Main Menu'
read gecoss
}

Open in new window

Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

>>"if [ -z "`echo $usernam`" -o "`echo $usernam`" = "root" -o "`echo $usernam`" = "sa" ]"

If the username you typed in is empty, root or sa, it exits.

>>if I need check if a user exists in /etc/passwd file,

simple grep piped to wc-l?

maybe something like:
if [ `grep $username /etc/password | wc -l` -gt 0 ]; then
     exit
fi
Quick change:
if [ `grep ^$username /etc/password | wc -l` -gt 0 ]; then
Avatar of Jason Yu

ASKER

I used this script, but after I input the username it, it returned back to the menu screen.



Checkusr()
{
echo
echo '***** Check User Existence *****'
echo
echo 'Enter Username Need Check (3-letter ID): '
read usernam

EXISTS=$( cat /etc/passwd | grep $usernam | sed -e 's/:.*//g' )
echo $EXISTS

if [ -z "$EXISTS" ]
then
echo "\$EXISTS is empty"
else
echo "\$EXISTS is NOT empty"
fi


}

Is there something wrong with my script.
>>Is there something wrong with my script.

Other than I don't think this will check what you want it to check:
 cat /etc/passwd | grep $usernam | sed -e 's/:.*//g'

I just tested the syntax you had in a simple script (not the function) and it echoed what I would expect.

#/bin/ksh

echo 
 echo '***** Check User Existence *****'
 echo
 echo 'Enter Username Need Check (3-letter ID): '
 read usernam

 EXISTS=$( cat /etc/passwd | grep $usernam | sed -e 's/:.*//g' )
 echo $EXISTS

 if [ -z "$EXISTS" ]
 then
 echo "\$EXISTS is empty"
 else
 echo "\$EXISTS is NOT empty"
 fi

Open in new window

Slightwv:

Thank you very much for your update.

I tried your script, it is the same effect. After I input a test username "jyu" , it returns back to the orginal selection menu. Please see the attachment.
1.png
2.png
3.png
#!/bin/ksh

Newusr()
{
echo ^L
echo '***** New User *****'
echo
echo 'Enter New Username (3-letter ID): '
read usernam
if [ -z "`echo $usernam`" -o "`echo $usernam`" = "root" -o "`echo $usernam`" = "sa" ]
then
exit 0
fi
echo '\n'
echo 'Enter Description for Username' $usernam ':'
read gecoss
echo ^L
echo 'New User' $usernam 'being added'
/usr/bin/mkuser pgrp='lac' groups='lac,staff' home='/u1/dbms/LIVE.ENTRY' shell='/usr/bin/ksh' gecos="$gecoss" maxage=8 minlen=8 $usernam
echo 'New user '$usernam' created'
echo 'Press <Enter> to return to Main Menu'
read gecoss
}


Checkusr()
{
echo ^L
echo '***** Check User Existence *****'
echo
echo 'Enter Username Need Check (3-letter ID): '
read usernam
if [ `grep ^$usernam /etc/password | wc -l` -gt 0 ]; then

echo "\$usernam exist"

fi



#EXISTS=$( cat /etc/passwd | grep $usernam | sed -e 's/:.*//g' )
#echo $EXISTS

#if [ -z "$EXISTS" ]
#then
#echo "\$EXISTS is empty"
#else
#echo "\$EXISTS is NOT empty"
#fi


}

Open in new window

>>After I input a test username "jyu" , it returns back to the orginal selection menu.

Likely because it just does an echo and returns to the calling script.  It looks like the calling script clears the screen and displays the menu.  When the screen clears, the echo'd output is erased.

What do you want it to do?

I assume something similar to newuser?

Add this to check user:
echo 'Press <Enter> to return to Main Menu'
read gecoss
got the following error:


***** Check User Existence *****

Enter Username Need Check (3-letter ID):
jyu
grep: 0652-033 Cannot open /etc/password.
Press <Enter> to return to Main Menu
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The permission is showed as below:

[MIRAMAR:root:/home/useradm]ls -al
total 31
drwxrwxrwx   2 ssg      security        512 Sep 10 10:35 .
drwxr-xr-x  43 kxtr     lac            1536 Apr 21 12:09 ..
-rwxrwxrwx   1 ssg      security        267 Nov 27 2007  .profile
-rwxrwxrwx   1 root     lac              62 Jun 13 2013  .sh_history
-rwxrwxrwx   1 root     security       2617 Sep 10 13:13 newmenu
-rwxrwxrwx   1 ssg      security       1991 Apr 26 2012  newmenu-bak
-rwxrwxrwx   1 root     system         1412 Nov 27 2007  newmenu.112707
-rwxrwxrwx   1 ssg      security       1905 Oct 26 2007  newmenu2
-rwxrwxrwx   1 ssg      security       2048 May 18 2012  newmenu_backup
-rwxrwxrwx   1 ssg      security       1547 May 28 2002  oldmenu
[MIRAMAR:root:/home/useradm]
The issue is the file name typo.

But since you posted this:
>>[MIRAMAR:root:/home/useradm]ls -al

That tells me nothing.  That is only the permissions of the files in the folder you are sitting in which is /home/useradm

The file it was looking for was in a different file system /etc.
Thank you very much, your solution is wonderful.