When checking for none existent / null values I usually write the if statement with quotes around the variable. This deals with the situation where there is no value for the argument. If the value is null, after argument resolution, the if statement becomes:
if [[ == ""]] ....
which is an invalid statement.
Ex. if [[ "${1}" == "" ]] ........
JimR
Original Message:
Sent: 9/6/2023 8:02:00 AM
From: Marcin Marciszewicz
Subject: RE: Displays password change warning for root user
Try this, I removed the brackets [ ], and added spaces behind when comparing:
#!/bin/ksh
USER="`/usr/bin/whoami | awk '{print $1}'`"
SGROUP="`id -gn ${USER}`"
if [[ ${1} == "" ]];then
if [ $SGROUP == "system" ] && [ $USER == "root" ];then
echo "#####################################################################
# #
# This operation can cause serious problems. #
# Changing the root password may result in a problem with the #
# operation of the client application #
# #
#####################################################################
1.Press Enter to continue changing `whoami` password"
read hold
/usr/bin/passwd
fi
elif [[ ${1} == "root" ]];then
if [ $SGROUP == "system" ] && [ $USER == "root" ];then
echo "#####################################################################
# #
# This operation can cause serious problems. #
# Changing the root password may result in a problem with the #
# operation of the client application #
# #
#####################################################################
2.Press Enter to continue changing `whoami` password"
read hold
/usr/bin/passwd ${1}
fi
fi
------------------------------
Marcin Marciszewicz
------------------------------
Original Message:
Sent: Thu July 20, 2023 11:27 PM
From: Dao Tien Dat
Subject: Displays password change warning for root user
Hi, I'm trying to use your script but has an error about syntax: 0403-057 Syntax error at line 5 : `then' is not expected.
Can you help me about it. Thank you
------------------------------
Dao Tien Dat
Original Message:
Sent: Wed July 12, 2023 07:38 AM
From: Marcin Marciszewicz
Subject: Displays password change warning for root user
Hello,
In general, I created the script thanks to your help :)
I have implemented the command passwd and passwd $USER, then check whether the user belongs to the correct group and whether the logged in user is root.
#!/bin/ksh
USER="`/usr/bin/whoami | awk '{print $1}'`"
SGROUP="`id -gn ${USER}`"
if [[ ${1} == "" ]];then
if [[ $SGROUP=="system" ]] && [[ $USER=="root" ]];then
echo "#####################################################################
# #
# This operation can cause serious problems. #
# Changing the root password may result in a problem with the #
# operation of the client application #
# #
#####################################################################
Press Enter to continue changing `whoami` password"
read hold
/usr/bin/passwd
fi
elif [[ ${1} == "root" ]];then
if [[ $SGROUP=="system" ]] && [[ $USER=="root" ]];then
echo "#####################################################################
# #
# This operation can cause serious problems. #
# Changing the root password may result in a problem with the #
# operation of the client application #
# #
#####################################################################
TEST Press Enter to continue changing `whoami` password"
read hold
/usr/bin/passwd ${1}
fi
fi
I added to the .profile of the root user the path to the script
I assigned run permissions and everything works as I expected.
Is there any way to optimize it better?
Thank you all for the tips.
------------------------------
Marcin Marciszewicz
Original Message:
Sent: Wed June 28, 2023 09:04 AM
From: Henrik Morsing
Subject: Displays password change warning for root user
Hi Marcin,
You could define a function that calls the actual password command.
Regards,
Henrik Morsing
------------------------------
Henrik Morsing
Original Message:
Sent: Thu June 22, 2023 11:14 AM
From: Marcin Marciszewicz
Subject: Displays password change warning for root user
Hi,
I will try to check this solution next week
Thanks a lot
------------------------------
Marcin Marciszewicz
Original Message:
Sent: Thu June 22, 2023 09:03 AM
From: Fazel Vayalilagathu
Subject: Displays password change warning for root user
You can add a wrapper script as below and name it passwd and keep it in a directory that is in first search order in PATH variable.
====================================================================
if [ ${1} == "root" ];then
echo "This operation can cause major issues..."
echo "Press Enter to continue changing ${1} password"
read hold
passwd ${1}
else
passwd ${1}
fi
=============================================================================
This will give you the expected results I believe.
------------------------------
Fazel Vayalilagathu
Original Message:
Sent: Wed June 21, 2023 11:40 AM
From: Marcin Marciszewicz
Subject: Displays password change warning for root user
Hi,
How do I add a warning before changing the password for the root user?
I have been trying to find a solution to this problem for a long time, but nothing comes to mind.
Adding a warning to /etc/motd does not solve the problem.
That is, we type:
passwd root
the message appears
"This operation can cause major issues..."
Help me
------------------------------
Marcin Marciszewicz
------------------------------