The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
rsh script with inside a for loop trekianov Shell Programming and Scripting 5 12-06-2008 12:39 PM
while loop problems amatuer_lee_3 Shell Programming and Scripting 7 05-13-2008 12:48 AM
Script not executing second loop thumper Shell Programming and Scripting 2 05-07-2008 04:10 PM
While loop problems rcunn87 Shell Programming and Scripting 0 06-20-2006 04:26 PM
not quite sure how to loop this script! moxxx68 Shell Programming and Scripting 3 09-09-2004 07:00 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-15-2007
lodey lodey is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 29
Problems with an if/then loop within a script

Hi there,

I have written a script to clear out log files from the var/tmp dir. It works up to a point. What I needed to do was to exit the script if there was no files to be deleted. I can get this working on a test script but when I implement it into my program it errors out with a `then` not matched error. I have traced it to the line in the script: if [TEMP ="0"]; then

Could somebody have a look at it and teel me where I have went wrong or point me in the right direction

Rgds
Colin

Here is the extract from the script that I created:

#!/bin/sh
#
# Set local variables
USER=`/usr/bin/whoami`
ACCOUNT=specs
HOSTNAME=`/usr/bin/hostname`
DATE=`/usr/bin/date "+%Y%m%d"`
TEMP=`find /var/tmp/ -name "gem*" -atime +3 | wc -l`
TEMPALL=`find /var/tmp/ -atime +3 | wc -l`
# Purge old Log Files
find /users/agaff/nospace/logs/ -name "*.txt" -atime +1|xargs rm -f
# Main Screen
clear
echo "<1> Delete GEM log files on host: " $HOSTNAME
echo "<2> Delete ALL Files in the /var/tmp ( maintenance only ) "
echo "<3> Exit - no files to be deleted"
echo ""
echo " Enter 1 or 2 or 3"
read ANSWER
if [ $ANSWER = "1" ]; then
clear
echo "Examining Log Files for host: " $HOSTNAME
echo ""
if [ $TEMP = "0" ]; then #(this is where it bombs out)
echo
echo
echo"***************************************************"
echo "There are no log files to be deleted from host:" $HOSTNAME
echo
elif [ $TEMP > "0" ];
then
echo "***********************************************"
echo "There are " $TEMP "log files that can be deleted from host:" $HOSTNAME
echo ""
echo ""
echo "<1> Delete GEM log files on Host" $HOSTNAME "in the /var/tmp directory"
echo "<2> Exit - no files to be deleted"
echo ""
echo " Enter 1 or 2"
echo""
read ANS1
if [ $ANS1 = "1" ]; then
clear
echo ""
echo ""
echo"Deleting GEM Logfiles Log Files"
mkdir -p /users/agaff/nospace/logs/
find /var/tmp/ -name "gem*" -atime +3 > /users/agaff/nospace/logs/$DATE"-"$HOSTNAME"-logs".txt| xargs -f >/dev/null 2>&1
echo ""
echo "************************************************************************"
echo "Tmp Logs are now deleted and logfile updated"
echo ""
echo ""
elif [ $ANS1 = "2" ]; then
echo "Exiting"
else
echo "Invalid selection, exiting..."
exit
fi
  #2 (permalink)  
Old 09-15-2007
kamitsin's Avatar
kamitsin kamitsin is offline
Registered User
  
 

Join Date: Nov 2006
Location: /dev/null
Posts: 177
Buddy , it bombed all over the places.

Major errors.

1) You were doing integer comparisoin at many places using the wrong operator.

2) lot of syntax error in echo

3) I don't like the structure of your script. You should use 'case' structure instead.

4) if statement not closed properly

Modified script:
PS: I have modified a few paths in the script to check on my system.

Code:
#!/bin/sh
#
# Set local variables
USER=`/usr/bin/whoami`
ACCOUNT=Compaq
HOSTNAME=`/usr/bin/hostname`
DATE=`/usr/bin/date "+%Y%m%d"`
TEMP=`find . -name "gem*" | wc -l`
TEMPALL=`find .  | wc -l`
# Purge old Log Files
#find /users/agaff/nospace/logs/ -name "*.txt" -atime +1|xargs rm -f
# Main Screen
clear
echo "<1> Delete GEM log files on host: " $HOSTNAME
echo "<2> Delete ALL Files in the /var/tmp ( maintenance only ) "
echo "<3> Exit - no files to be deleted"
echo ""
echo " Enter 1 or 2 or 3"
read ANSWER
if [ $ANSWER -eq "1" ]; then
clear
echo "Examining Log Files for host: " $HOSTNAME
echo ""
if [ $TEMP -eq "0" ]; then #(this is where it bombs out)
echo
echo
echo "***************************************************"
echo "There are no log files to be deleted from host:" $HOSTNAME
echo
elif [ $TEMP -gt "0" ];
then
echo "***********************************************"
echo "There are " $TEMP "log files that can be deleted from host:" $HOSTNAME
echo ""
echo ""
echo "<1> Delete GEM log files on Host" $HOSTNAME "in the /var/tmp directory"
echo "<2> Exit - no files to be deleted"
echo ""
echo " Enter 1 or 2"
echo""
read ANS1
if [ $ANS1 -eq "1" ]; then
clear
echo ""
echo ""
echo "Deleting GEM Logfiles Log Files"
#mkdir -p /users/agaff/nospace/logs/
#find /var/tmp/ -name "gem*" -atime +3 > /users/agaff/nospace/logs/$DATE"-"$HOST
NAME"-logs".txt| xargs -f >/dev/null 2>&1
echo ""
echo "************************************************************************"
echo "Tmp Logs are now deleted and logfile updated"
echo ""
echo ""
elif [ $ANS1 -eq "2" ]; then
echo "Exiting"
else
echo "Invalid selection, exiting..."
exit
fi
fi
fi
Cheers,
K
  #3 (permalink)  
Old 09-15-2007
lodey lodey is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 29
Thanks a lot for your help

I am new to Unix scripting and am learning as I go so evry bit of help is welcome

Now I have a working script
  #4 (permalink)  
Old 09-18-2007
dewets dewets is offline
Registered User
  
 

Join Date: Mar 2007
Location: Somerset West, South Africa
Posts: 10
Just to add my 0.02c worth:

Make use of indenting in script writing!!! It makes maintenance on your script just SOOO much easier.

Trying to find a nested elif command in amongst all you "echo"'s was quite something:

#!/bin/sh
#
# Set local variables
USER=`/usr/bin/whoami`
ACCOUNT=Compaq
HOSTNAME=`/usr/bin/hostname`
DATE=`/usr/bin/date "+%Y%m%d"`
TEMP=`find . -name "gem*" | wc -l`
TEMPALL=`find . | wc -l`
# Purge old Log Files
#find /users/agaff/nospace/logs/ -name "*.txt" -atime +1|xargs rm -f
# Main Screen
clear
echo "<1> Delete GEM log files on host: " $HOSTNAME
echo "<2> Delete ALL Files in the /var/tmp ( maintenance only ) "
echo "<3> Exit - no files to be deleted"
echo ""
echo " Enter 1 or 2 or 3"
read ANSWER
if [ $ANSWER -eq "1" ]; then
clear
echo "Examining Log Files for host: " $HOSTNAME
echo ""
if [ $TEMP -eq "0" ]; then #(this is where it bombs out)
echo
echo
echo "*******************************************"
echo "There are no log files to be deleted from host:" $HOSTNAME
echo
elif [ $TEMP -gt "0" ];
then
echo "****************************************"
echo "There are " $TEMP "log files that can be deleted from host:"
$HOSTNAME
echo ""
echo ""
echo "<1> Delete GEM log files on Host" $HOSTNAME "in the /var/tmp directory"
echo "<2> Exit - no files to be deleted"
echo ""
echo " Enter 1 or 2"
echo""
read ANS1
if [ $ANS1 -eq "1" ]; then
clear
echo ""
echo ""
echo "Deleting GEM Logfiles Log Files"
#mkdir -p /users/agaff/nospace/logs/
#find /var/tmp/ -name "gem*" -atime +3 > /users/agaff/nospace/logs/$DATE"-"$HOST
NAME"-logs".txt| xargs -f >/dev/null 2>&1
echo ""
echo "*******************************************"
echo "Tmp Logs are now deleted and logfile updated"
echo ""
echo ""
elif [ $ANS1 -eq "2" ]; then
echo "Exiting"
else
echo "Invalid selection, exiting..."
exit
fi
fi
fi



Here endeth our readability lesson for today...
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0