Operating System - HP-UX
1753802 Members
7489 Online
108805 Solutions
New Discussion юеВ

Script help - test command

 
SOLVED
Go to solution
Greg Stark_1
Frequent Advisor

Script help - test command

I want to check if cron.allow exists, if so, look to see if root and oracle are in it. If it does not exist, create it and add them.

Here is the script:
echo "Verifying /var/adm/cron/cron.allow..."
if [ $(test -f /var/adm/cron/cron.allow) ]; then
if [ $(grep -q root /var/adm/cron/cron.allow) ]; then
echo "\troot is in cron.allow"
else
echo "root\n" >> /var/adm/cron/cron.allow
fi
if [ $(grep -q oracle /var/adm/cron/cron.allow) ]; then
echo "\toracle is in cron.allow"
else
echo "oracle\n" >> /var/adm/cron/cron.allow
fi
else
echo "root\noracle\n" >> /var/adm/cron/cron.allow
fi
chmod 644 /var/adm/cron/cron.allow
chown root:system /var/adm/cron/cron.allow
echo "COMPLETED - Verifying /var/adm/cron/cron.allow.\n"

Even if the cron.allow is there and has root and oracle in it, it is still adding them. I know it isn't "catching" on the first "if" statement so something must be wrong with my "test" command.

Thanks again..
7 REPLIES 7
RAC_1
Honored Contributor
Solution

Re: Script help - test command

I would change following.
if [ $(test -f /var/adm/cron/cron.allow) ]; then

to
if [[ -f /var/adm/cron/cron.allow ]];then

your code.

Also grep statements as follows.

stat=$?
root=$(grep -i root /var/adm/cron/cron.allow > /dev/null 2>&1)
if [[ ${stat} -ne "0" ]];then
echo "root\n" >> /var/adm/cron/cron.allow

Anil
There is no substitute to HARDWORK
Jean-Luc Oudart
Honored Contributor

Re: Script help - test command

Hi

change your test command from :
if [ $(test -f /var/adm/cron/cron.allow) ]; then

to
if [[ -f /var/adm/cron/cron.allow ]]; then

Regards
Jean-Luc
fiat lux
A. Clay Stephenson
Acclaimed Contributor

Re: Script help - test command

You need
to change your syntax:

#!/usr/bin/sh

FNAME="/var/adm/cron/cron.allow"

if [[ -f ${FNAME} ]]
then
grep -q "root" S{FNAME}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "root found"
else
echo "root not found"
fi
fi

Note the use of the [['s and ]]'s in lieu of the more traditional ['s and ]'s. The double brackets utilize the shell's internal tests rather than relying upon the external test command. Under this syntax logical and's are && (rather than -a) and logical or's are || (rather than -o). grep -q returns a zero result when a match is found and non-zero otherwise.

If it ain't broke, I can fix that.
Dani Seely
Valued Contributor

Re: Script help - test command

Hey Greg,
Here's a VERY simple way to check for cron.allow and add root and oracle:

#!/usr/bin/ksh
FILE="/var/adm/cron/cron.allow"
MODF="$FILE.$$.mod"
if [[ -f "$FILE" ]]; then
egrep -v "^root|^oracle" $FILE >$MODF
echo "root" >> $MODF
echo "oracle" >> $MODF
cp -p $MODF $FILE
else
echo root > $FILE
echo oracle >> $FILE
fi
Together We Stand!
Dani Seely
Valued Contributor

Re: Script help - test command

Hey Greg,
The script I provided is short and simple and I'm sure it will work for you. Also, I hit submit before I remembered the rest of your message. You are setting permissions of your cron.allow file a little loose. If you are security consciencious, you should set permissions to 700.

Also, the owner and group should be root, bin, or sys (you show root:system ... I think you want root:sys unless you renamed the sys group to system or created a system group.)
Together We Stand!
Dani Seely
Valued Contributor

Re: Script help - test command

Sorry, one more thing ...

Do you have a cron.deny file? If there is a cron.deny file and the cron.deny file has the default accounts in it, the cron.allow file does not have to exist.
Together We Stand!
Bill Hassell
Honored Contributor

Re: Script help - test command

Even easier:

[ -f /var/cron/cron.allow ] && echo "\troot is in cron.allow" || echo "root\n" >> /var/adm/cron/cron.allow

The [[ ... ]] construct is for more specialized conditions including testing undefined variables. The && and || are shortcuts for if-then-else-fi. In your original construct, the $(test...) code is redundant because test and [...] are the same thing. See: man test


Bill Hassell, sysadmin