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

Jason YuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
>>"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
0
slightwv (䄆 Netminder) Commented:
Quick change:
if [ `grep ^$username /etc/password | wc -l` -gt 0 ]; then
0
Jason YuAuthor Commented:
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.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

slightwv (䄆 Netminder) Commented:
>>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

0
Jason YuAuthor Commented:
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
0
Jason YuAuthor Commented:
#!/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

0
slightwv (䄆 Netminder) Commented:
>>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
0
Jason YuAuthor Commented:
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
0
slightwv (䄆 Netminder) Commented:
I missed the typo when you first posted that.

It is /etc/passwd not /etc/password
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jason YuAuthor Commented:
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]
0
slightwv (䄆 Netminder) Commented:
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.
0
Jason YuAuthor Commented:
Thank you very much, your solution is wonderful.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.