1833006 Members
3458 Online
110048 Solutions
New Discussion

shell script

 
SOLVED
Go to solution
Tom Satinet
Frequent Advisor

shell script

Hello, I've written a script as outlined below.

It allowes sudoers to add users to a system. however, it checks that they put the correct information in. ie. username must be six characters, branch must be 4. and no Null values for the user's real name. These things are constrained by the application the user account are for (a progress database).

While this works okay, the script exits at the first failure. e.g if they put a 5 letter log on name and a 3 letter branch name, they are only told about the incorrect log on name. Can anyone suggest a way to enable it to echo all the errors:

The output i want is a list of what they did wrong and then the part about 'account created aborted'

#! /bin/ksh
clear

echo "Enter the login name of the User you want to add: \c"
read USER

echo "Enter the user's FIRST name: \c"
read FIRST

echo "Enter the user's SURNAME: \c"
read SURNAME

echo "Which Branch is the user based at? (four letters): \c"
read BRANCH
BRANCH=`echo $BRANCH | tr '[a-z]' '[A-Z]'`

FOUR=5
SIX=7
USERSIX=`echo $USER|wc -m`
BRANCHFOUR=`echo $BRANCH|wc -m`

if test $USERSIX != $SIX
then
echo "The user name must be 6 characters long"

elif test $BRANCHFOUR != $FOUR
then
echo "The Branch name is not 4 characters long"

elif [ $2 -z "$FIRST" -o -z "$SURNAME" ]
then
echo "You have entered a NULL for the users name"

elif [ $4 "$USERSIX" != "$SIX" -o "$BRANCHFOUR" != "$FOUR" -o -z "$FIRST" -o -z "$SURNAME" ]
then
echo "Account Creation ABORTED"
else


clear
echo "The following account is about to be created:"
echo "Username: $USER"
echo "Name: $FIRST $SURNAME"
echo "Branch: $BRANCH"
echo "\nIs this Correct? Please choose y or n"

read Answer
while [ $Answer != 'n' ] && [ $Answer != 'y' ]
do
echo "Please press y or n and press enter"
read Answer
done

if [ $Answer = 'n' ]
then
echo "User NOT added"
else

/usr/sbin/useradd -d /home/$USER -m -c "$FIRST $SURNAME - ($BRANCH)" -s /usr/bin
/ksh -g coins -k /etc/skel/coins $USER

passwd $USER
passwd -f $USER
fi
fi
13 REPLIES 13
Patrick Wallek
Honored Contributor

Re: shell script

It is doing this becuase you are using if/then/elif. If the first condition is true, nothing else will be checked. That's the way the elif works.

If you want everything checked you could just do multiple if/then/fi statements.

Tom Satinet
Frequent Advisor

Re: shell script

yes, i guess so. It would be loads of code though.

There's no simple way to do this?
Tom Satinet
Frequent Advisor

Re: shell script

i was really looking for advice on how to do this. i know how elif works..... :-(
Jannik
Honored Contributor

Re: shell script

You should do a && (and) test something like this:

if [ $1 = "test1" ] && [ $2 = "test2" ]
then
echo "do echo all ok"
else
if
elif
else
fi
fi
jaton
Tom Satinet
Frequent Advisor

Re: shell script

yeah, it's very frustating. having to nest loads of ifs within each other, just to do something so simple.

is there no other way? like if a then b, but carry on with the rest of the script :-(
MarkSyder
Honored Contributor

Re: shell script

You could set up an error log file at the beginning of the script. Null the error log before the checking starts (> error.log) to avoid errors from previous sessions showing up.

Your checks would then write error messages to the error log instead of the screen (make sure you use append - >> - so as not to overwrite earlier errors). At the end, you would then have a test:

if -f error.log
then
your procedure for a failed process.

if -f means if error.log exists and is a standard file - this test will fail if error.log is empty.

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Jannik
Honored Contributor

Re: shell script

you could do:

if [ $1 = "test1" ] && [ $2 = "test2" ]
then
arch=OK
else
echo "error in read 1=$1, 2=$2"
exit
fi

if [ $arch = "OK" ]
then
...
elif
else
fi
jaton
Ivan Ferreira
Honored Contributor

Re: shell script

I think that when you do the check, you shold not use elif, just if, because elif wont be tested when there is a match.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Orhan Biyiklioglu
Respected Contributor
Solution

Re: shell script

try this

#! /bin/ksh
clear

echo "Enter the login name of the User you want to add: \c"
read USER

echo "Enter the user's FIRST name: \c"
read FIRST

echo "Enter the user's SURNAME: \c"
read SURNAME

echo "Which Branch is the user based at? (four letters): \c"
read BRANCH
BRANCH=`echo $BRANCH | tr '[a-z]' '[A-Z]'`

FOUR=5
SIX=7
USERSIX=`echo $USER|wc -m`
BRANCHFOUR=`echo $BRANCH|wc -m`

VALIDUSER=true

if test $USERSIX != $SIX
then
echo "The user name must be 6 characters long"
VALIDUSER=false
fi

if test $BRANCHFOUR != $FOUR
then
echo "The Branch name is not 4 characters long"
VALIDUSER=false
fi

if [ $2 -z "$FIRST" -o -z "$SURNAME" ]
then
echo "You have entered a NULL for the users name"
VALIDUSER=false
fi

if [ $4 "$USERSIX" != "$SIX" -o "$BRANCHFOUR" != "$FOUR" -o -z "$FIRST" -o -z "$SURNAME" ]
then
echo "Account Creation ABORTED"
VALIDUSER=false
fi

if [$VALIDUSER]
then
clear
echo "The following account is about to be created:"
echo "Username: $USER"
echo "Name: $FIRST $SURNAME"
echo "Branch: $BRANCH"
echo "\nIs this Correct? Please choose y or n"

read Answer
while [ $Answer != 'n' ] && [ $Answer != 'y' ]
do
echo "Please press y or n and press enter"
read Answer
done

if [ $Answer = 'n' ]
then
echo "User NOT added"
else

/usr/sbin/useradd -d /home/$USER -m -c "$FIRST $SURNAME - ($BRANCH)" -s /usr/bin
/ksh -g coins -k /etc/skel/coins $USER

passwd $USER
passwd -f $USER
fi
fi

PS: just keeping a variable to indicate a valid user and negating it if any input is invalid...

hth
Orhan Biyiklioglu
Respected Contributor

Re: shell script

line 48 should be

if $VALIDUSER

sorry for the typo.
Tom Satinet
Frequent Advisor

Re: shell script

hey that's great.

i presume if validates true or false automatically like that.

The other bit is great also.

:-)

exactly what i wanted.

11/10!
Tom Satinet
Frequent Advisor

Re: shell script

i see it was my misplacement of 'fi' and elses that were causing my problem.

have i written this in a stupid way? i am relative beginer.

It may seem over the top, but this will be run by people of with very limited techincal (read intellecutal) abilities, on a service desk using sudo trough a client calling the shell script for a programme called 'coins'.

Tom Satinet
Frequent Advisor

Re: shell script

thanks :-)