1751700 Members
5574 Online
108781 Solutions
New Discussion юеВ

Script giving error

 
Sri123
Advisor

Script giving error

Hi All,

I am trying a write a script if an user's account gets disabled, then the script would run and mailx reporting the same.

Below is a script that i am trying to create, which is giving an error along with the output.
Kindly let me know what changes needs to be done.

I am looking for a similar script for non-trusted system also. Hope someone can help me sort out this error.


lbsdr-4CPU@APP>cat test1.sh
#!/usr/bin/sh
for USER in $(cat /tmp/users)
do
PCT=$(/usr/lbin/getprpw -m lockout $USER | tr -cd "[1\012]")
PCS=$(hostname)
if (( ${PCT} != 0 )) ; then
echo "Account '$USER' is disabled in the server ${PCS}. Kindly get in touch with UNIX Admin to enable the account"
fi
done
lbsdr-4CPU@APP>
lbsdr-4CPU@APP>cat /tmp/users
sridhar
plasbo


The output says:

lbsdr-4CPU@APP>./test1.sh
Account 'sridhar' is disabled in the server lbsdr. Kindly get in touch with UNIX Admin to enable the account
./test1.sh[6]: != 0 : Assignment requires an lvalue.


Kindly let me know what has to be done here.

Thanks,
Sridhar
9 REPLIES 9
PM Srividhya
Advisor

Re: Script giving error

Hi,

Try using a square brackets [] instead of normal brackets () for the IF statement:

if [ ${PCT} != 0 ]

Regards,
Vidhya
Sri123
Advisor

Re: Script giving error

Hi Vidhya,

Now it gives me a different error.

lbsdr-4CPU@APP>./test1.sh
Account 'sridhar' is disabled in the server lbsdr. Kindly get in touch with UNIX Admin to enable the account
./test1.sh[6]: test: Specify a parameter with this command.



lbsdr-4CPU@APP>cat test1.sh
#!/usr/bin/sh
for USER in $(cat /tmp/users)
do
PCT=$(/usr/lbin/getprpw -m lockout $USER | tr -cd "[1\012]")
PCS=$(hostname)
if [ ${PCT} != 0 ] ; then
echo "Account '$USER' is disabled in the server ${PCS}. Kindly get in touch with UNIX Admin to enable the account"
fi
done
Steven Schweda
Honored Contributor

Re: Script giving error

> Kindly let me know what has to be done
> here.

Kindly explain what you wish to do here.


What is the output from the simple command?:

/usr/lbin/getprpw -m lockout $USER | tr -cd "[1\012]"

or, even simpler, from:

/usr/lbin/getprpw -m lockout $USER

?

Or in the script:

echo ">${PCT}<"

If you're not sure what's in a variable like
PCT, then it's safer to use some quotation
marks around its value. For example:

if [ "${PCT}" != 0 ] ; then

Are you trying to test the character string
output from this command, or its exit status
value, or what?
Sri123
Advisor

Re: Script giving error

Hi Steven,

What i am trying to do is to run a script to mailx and tell that a particular account is locked among a list of accounts.

I haven't added mailx in the script as of now, but want the script to say that an account has been locked and it needs to be unlocked.

Further, i can add mailx upon the script running successfully.


Thanks,
Sridhar
Jose Mosquera
Honored Contributor

Re: Script giving error

Hi,

You are using text comparision operators with numeric values, I suggest you try this into your if statment:
if [ "${PCT}" != "0" ]
then
echo "Account '$USER' is disabled in the server ${PCS}. Kindly get in touch with UNIX Admin to enable the account"
fi

Rgds.
Dennis Handly
Acclaimed Contributor

Re: Script giving error

>./test1.sh[6]: != 0 : Assignment requires an lvalue.
>if (( ${PCT} != 0 )) ; then

This seems like the right syntax. But you don't need to treat PCT special in arithmetic expressions:
if (( PCT != 0 )); then

If this fails, PCT isn't numeric. As Steven said, you should print out its value to see if your tr(1) is correct.
Steven Schweda
Honored Contributor

Re: Script giving error

> What i am trying to do is [...]

Ok. Did you stop reading at the first
question mark?
James R. Ferguson
Acclaimed Contributor

Re: Script giving error

Hi Sridar:

If you are running on a non-trusted system, 'getprpw' isn't going to be available. It applies only to the root user of trusted systems.

This said, I would suspect that your 'PCT' variable is simply empty giving you the error you see.

As Steven has said, echo the 'PCT' value with visible surrounding characters of your choice to confirm its emptiness or expose its value.

Regards!

...JRF...



Jose Mosquera
Honored Contributor

Re: Script giving error

Hi again,

When double quotes encloses a variable or value convert them into characters values.
So to solve this you only needs set double quotes in your "IF" statement as I have indicated you previously. In this way you can compare any valor that adopt the $PCT variable against the character zero (not value).

Rgds.