1834454 Members
2945 Online
110067 Solutions
New Discussion

script

 
SOLVED
Go to solution
Ragni Singh
Super Advisor

script

Hey guys, what am I doing wrong with the script. please help. I'm getting syntax error on line 12, 16, 20.

#!/bin/ksh
# Greets $LOGNAME or a specified user

#date=`data | cut -d\ -f4 |cut -d: -f1`
date=`date|awk '{print $4}'`
name=$1
if [ $# -eq 0 ]
then
name=`finger -s -m $LOGNAME | head -2 |tail -1 |awk '{print $2}'`
fi

if [ $date -le 1 $date -a 11 -ge $date ]
then
echo Good Morning, $name
fi
if [ $date -le 12 $date -a 17 -ge $date ]
then
echo Good Afternoon, $name
fi
if [ $date -le 18 $date -a 23 -ge $date ]
then
echo Good Evening, $name

fi
11 REPLIES 11
Kenneth_19
Trusted Contributor

Re: script

it should be:

if [ $date -ge 1 -a $date -le 11 ]

if [ $date -ge 12 -a $date -le 17 ]

if [ $date -ge 18 -a $date -le 23 ]

it reads like:

if $date greater than or equal to 1 and $date less than or equal to 11, then ....

Regards,
Kenneth
Always take care of your dearest before it is too late
Uday_S_Ankolekar
Honored Contributor

Re: script

Hello,

Try to put echo statement in quotes

echo "Good Morning"

-USA..
Good Luck..
Christopher McCray_1
Honored Contributor

Re: script

Hello,

Flip around the test after the "-a"

What happens??

Chris
It wasn't me!!!!
Ragni Singh
Super Advisor

Re: script

Still getting the same error about syntax ob line 12, 16, 20.


#!/bin/ksh
# Greets $LOGNAME or a specified user

#date=`data | cut -d\ -f4 |cut -d: -f1`
date=`date|awk '{print $4}'`
name=$1
if [ $# -eq 0 ]
then
name=`finger -s -m $LOGNAME | head -2 |tail -1 |awk '{print $2}'`
fi

if [ $date -ge 1 -a $date -le 11 ]
then
echo "Good Morning", $name!
fi
if [ $date -ge 12 -a $date -le 17 ]
then
echo "Good Afternoon", $name!
fi
if [ $date -ge 18 -a $date -le 23 ]
then
echo "Good Evening", $name!

fi
Bill McNAMARA_1
Honored Contributor
Solution

Re: script

try running it a non root user!

pereal - / # sh -x ./file
+ + awk {print $4}
+ date
date=18:26:02
+ name=
+ [ 0 -eq 0 ]
+ + finger -s -m root
+ head -2
+ awk {print $2}
+ tail -1
name=???
+ [ 18:26:02 -ge 1 -a 18:26:02 -le 11 ]
./file[12]: 18:26:02: Syntax error
+ [ 18:26:02 -ge 12 -a 18:26:02 -le 17 ]
./file[16]: 18:26:02: Syntax error
+ [ 18:26:02 -ge 18 -a 18:26:02 -le 23 ]
./file[20]: 18:26:02: Syntax error
pereal - / # finger -s -m root
Login Name TTY Idle When Bldg. Phone
root ??? *p1 Tue 18:25

or...
you could filter the name variable for all special characters....

Later,
Bill
It works for me (tm)
Kenneth_19
Trusted Contributor

Re: script

BTW, in line 5:
date=`date|awk '{print $4}'`
then date will be something like hh:mm:ss, therefore it will have error when comparing with integer in line 12, 16, and 20.

Try change line 5 to:
date=`date|awk '{print $4}'|cut -d : -f 1`
Always take care of your dearest before it is too late
MANOJ SRIVASTAVA
Honored Contributor

Re: script

Sanman


The issue is in setting the date parameter , this sets it to 13:14:50 which also includes the second too , I have modified it to the follwoing :


#!/bin/ksh -x
# Greets $LOGNAME or a specified user

#date=`data | cut -d\ -f4 |cut -d: -f1`
date=`date|awk '{print $4}'| cut -c 1-2`
name=$1
if [ $# -eq 0 ]
then
name=`finger -s -m $LOGNAME | head -2 |tail -1 |awk '{print $2}'`
fi

if [ $date -ge 1 -a $date -le 11 ]

then
echo Good Morning, $name
fi
if [ $date -le 12 $date -a 17 -ge $date ]
then
echo Good Afternoon, $name
fi
if [ $date -le 18 $date -a 23 -ge $date ]
then
echo Good Evening, $name

fi


this will work , also please use -x that is the debuuger mode so that you can exactly know waht the command is doing .



Manoj Srivastava
Bill McNAMARA_1
Honored Contributor

Re: script

oops!
I feel like I have to fix this before I go home now!

#!/bin/ksh
# Greets $LOGNAME or a specified user

#!/bin/ksh
# Greets $LOGNAME or a specified user

hour=$(date +'%H')

name=bob


if [ $hour -ge 18 ] ; then
echo "Good Evening", ${name}
exit 3
fi

if [ $hour -ge 12 ] ; then
echo "Good Afternoon", ${name}
exit 2
fi

if [ $hour -ge 1 ] ; then
echo "Good Morning", ${name}
exit 1
fi

---
the problem is with the -a option...

I'll work on it more,, sheesh my scripting has gone to the dogs..

Later,
Bill
It works for me (tm)
Bill McNAMARA_1
Honored Contributor

Re: script

Try this:


#!/bin/ksh
# Greets $LOGNAME or a specified user

hour=$(date +'%H')
name=bob
if [ $hour -ge 18 -a $hour -lt 24 ] ; then
echo "Good Evening", ${name}
fi

if [ $hour -ge 12 -a $hour -lt 18 ] ; then
echo "Good Afternoon", ${name}
fi

if [ $hour -ge 1 -a $hour -lt 12 ] ; then
echo "Good Morning", ${name}
fi
It works for me (tm)
Bill McNAMARA_1
Honored Contributor

Re: script

I would still test for a valid name tho..


When run as root:

Good Morning, bin dev etc lib log net opt tmp usr var

pereal - / # echo ???
bin dev etc lib log net opt tmp usr var


pereal - / # finger -s -m root
Login Name TTY Idle When Bldg. Phone
root ??? *p1 Tue 18:25

Later,
Bill
It works for me (tm)
Hai Nguyen_1
Honored Contributor

Re: script

Your script should have read like this:

#!/bin/ksh
# Greets $LOGNAME or a specified user

#date=`data | cut -d\ -f4 |cut -d: -f1`
date=`date|awk '{print $4}'|cut -d: -f1`
name=$1
if [ $# -eq 0 ]
then
name=`finger -s -m $LOGNAME | head -2 |tail -1 |awk '{print $2}'`
fi

if [ $date -ge 1 -a $date -le 11 ]
then
echo "Good Morning", $name!
fi
if [ $date -ge 12 -a $date -le 17 ]
then
echo "Good Afternoon", $name!
fi
if [ $date -ge 18 -a $date -le 23 ]
then
echo "Good Evening", $name!

fi