Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script error

Status
Not open for further replies.

smitty3501

IS-IT--Management
May 20, 2005
8
0
0
US
DAY=`date +%a`
if test $DAY != "Mon" || if test $DAY != "Sat"
then
echo " "
echo " "
echo " "
echo " Today is `date` Time To Go Home"
echo " "
echo " "
echo " "
fi

[ssmith@aixtest ~] date_test.sh
./date_test.sh[4]: syntax error at line 4 : `if' unmatched
[ssmith@aixtest ~]
 
The if after your OR (||) is not needed.



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

 
I've tried it without the 2nd "if" statement. Then it runs and it shouldn't since to today is a Monday

#!/bin/sh
clear
DAY=`date +%a`
if test $DAY != "Mon" || test $DAY != "Sat"
then
echo " "
echo " "
echo " "
echo " Today is `date` Time To Go Home"
echo " "
echo " "
echo " "
fi

~
~
~
~
~
~
~
~
~
"date_test.sh" 14 lines, 197 characters
[ssmith@aixtest ~] date_test.sh



Today is Mon Jun 27 11:05:29 CDT 2005 Time To Go Home



[ssmith@aixtest ~]
 
Nope if give the smae results

#!/bin/sh
clear
DAY=`date +%a`
if [[ "$DAY" != "Mon" || "$DAY" != "Sat" ]]
#if test $DAY != "Mon" || test $DAY != "Sat"
then
echo " "
echo " "
echo " "
echo " Today is `date` Time To Go Home"
echo " "
echo " "
echo " "
fi

[ssmith@aixtest ~] date_test.sh



Today is Mon Jun 27 11:28:48 CDT 2005 Time To Go Home



[ssmith@aixtest ~]


 
I know you are looking for a solution within your script, but another solution is to use the crontabs functionality to run the script everyday except for Monday and Saturday.


Jim Hirschauer
 
Thus is just an example. Im going to want perform different functions on various days of the week. I seems easier to have 1 script, then 2 or 3 scripts to maintain
 
Looking more closely at your script, the problem really is with your logic. You don't want the script to run on Monday or Saturday, but the or statement will not work the way you want it to. Your if statement will always evaluate to true. I think you will need to change over to useing a case statement to accomplish your goal, something like this....

clear
DAY=`date +%a`
case $DAY in
Mon|Sat) ;;
*)
echo " "
echo " "
echo " "
echo " Today is `date` Time To Go Home"
echo " "
echo " "
echo " "
;;
esac


Jim Hirschauer
 
You really ought to reconsider moving the date logic into cron instead of rolling your own. Having a single script serve more than one purpose can be a maintenance nightmare.

With multiple scripts:
[ul][li]Breaking a script only breaks the one function[/li]
[li]Changing the run dates, or defining actual times of day will only require changes to a well documented file (crontab), with which any future sysadmin will be familiar[/li]
[li]Zero chance of executing a function on the wrong day due to an overlooked bug[/li]
[li]scripts can actually have meaningful names that indicate what they do[/li]
[li]no need to reinvent a wheel that has been very thoroughly invented and tested[/li]
[/ul]

Remember, ease of maintenance is about helping the next sysadmin out, even when the next sysadmin is you.

For more benefits, think of scripts as functions in the program called "being sysadmin" and read Steve McConnell's, and I don't use this word lightly, masterpiece Code Complete: A Practical Handbook of Software Construction. I consider it required reading for anyone that does anything that can be considered programming.

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

 
I know I am late, but it always "not Monday" or "not Saturday". Less often is it "not Monday" and "not Saturday", coincidentally on the days you would expect.

I suggest picking up an O'Reilly Korn Shell book. Lots of great info. Also, for date/time calculations not suitable for cron, perl isn't bad.

IBM Certified -- AIX 4.3 Obfuscation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top