Jason Yu
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.
"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
}
Quick change:
if [ `grep ^$username /etc/password | wc -l` -gt 0 ]; then
if [ `grep ^$username /etc/password | wc -l` -gt 0 ]; then
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.
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.
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
ASKER
ASKER
#!/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
}
>>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
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
ASKER
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
***** 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The permission is showed as below:
[MIRAMAR:root:/home/userad m]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/userad m]
[MIRAMAR:root:/home/userad
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/userad
The issue is the file name typo.
But since you posted this:
>>[MIRAMAR:root:/home/user adm]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.
But since you posted this:
>>[MIRAMAR:root:/home/user
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.
ASKER
Thank you very much, your solution is wonderful.
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