Sponsored Content
Top Forums Shell Programming and Scripting Problems with an if/then loop within a script Post 302136134 by lodey on Saturday 15th of September 2007 10:00:28 AM
Old 09-15-2007
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
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop problems

Here it wont terminate unless i take the not of my statement #!/bin/bash grabXML parseXML TAIL=5 #"$(cat dailyCasLog | tail -n 1)" COUNT=200 LINE=5 #"$(cat dailyCasLog | head -n $COUNT | tail -n 1)" echo $TAIL echo $LINE while ($LINE!=) do #$LINE">>currentLine folderMaker ... (0 Replies)
Discussion started by: rcunn87
0 Replies

2. Shell Programming and Scripting

while loop problems

I have a problem validating my script. The start of my script begins like this: then after this i have all of my script and functions. at the end i close the loop with this code: What i want to know is, how do i make the loop so that only Yes or no can be an answer? and if... (7 Replies)
Discussion started by: amatuer_lee_3
7 Replies

3. Shell Programming and Scripting

Problems with syntax in a loop (AWK)

Hi guys, I'm trying to loop through a number of files that is set by whatever is in a field. eg. The idea is to split FILELIST down into fields, it could contain 1 - 999 fields and it's bar delimited. I thought simple, count the number of fields in the field and then loop... (1 Reply)
Discussion started by: Peejay
1 Replies

4. Shell Programming and Scripting

problems calling out variables in a loop

good afternoon forums. i have a problem that ive been trying to work out all morning and cant seem to get my head around it. what i have in my script is individual letters saved in different variables. so if the word unix was saved then 'u' would be stored in the variable 'wvar1' 'n' in 'wvar2'... (7 Replies)
Discussion started by: strasner
7 Replies

5. UNIX for Dummies Questions & Answers

I'm having problems with a simple for loop on a newline

for i in `seq 1 10 ` ; do printf $i '\n'; done gives me this: 1234567891064mbarch ~ $ (output followed by bash prompt) :( I've tried so many ways to create a newline at the end. Does anyone have any ideas.. Thanks in advance. Sorry (7 Replies)
Discussion started by: 64mb
7 Replies

6. Shell Programming and Scripting

shell script to call perl script problems

Ok, don't ask me why, but all calls to perl must be called by a shell script. Its really not ideal, but its what I have to work with. Calling it isnt the issue, its passing in the arguments. I have about 1000 perl scripts to call by a shell script. Right now, I'm executing the shell script... (3 Replies)
Discussion started by: regexnub
3 Replies

7. Shell Programming and Scripting

How to loop use while loop in csh script?

Hi all, i got 2 text file. file.txt value.txt i want use C shell script to write out while both of the file got different limit....how i going to write it in 1 while loop? (4 Replies)
Discussion started by: proghack
4 Replies

8. Shell Programming and Scripting

loop problems...

#!/bin/bashi function userrecord() { read -p "please enter a number: " a clear output=$(grep -w "$a" appraisalrecord) b=$(echo $a | tr -dc ) if ]; then echo -e "accepted\n" else echo -e "The input must be a numerical number\n" userrecord fi if ]; then echo -e "the ID has... (2 Replies)
Discussion started by: bassmasta1
2 Replies

9. AIX

for loop problems

I have a script that loops through a list of users to list files owned by these users for u is `cat users.list` do echo $u >> result.out find /home -user $u >> result.out find /var -user $u >> result.out find /opt -user $u >> result.out find /usr -user $u >> result.out done an so... (3 Replies)
Discussion started by: Dardeer
3 Replies

10. Shell Programming and Scripting

Korn Shell Loop Problems

Very new to the Korn Shell, but I've been looking up loops online and it seems this should work. I'm just trying to convert an ip range in variables $A and $B and iterate the individual ip's out to new lines. Unfortunately I get {152..155} instead of 152, 153, 154, and 155. # for i in {$A..$B};... (8 Replies)
Discussion started by: Azrael
8 Replies
BF_TAR(1)						    Bogofilter Reference Manual 						 BF_TAR(1)

NAME
bf_tar - shell script to write a tar file of a bogofilter directory to stdout SYNOPSIS
bf_tar [-r] [-R] bogofilter_directory DESCRIPTION
bf_tar bundles a bogofilter working directory in tar format and copies it to standard output (your console, or where you redirect it, see EXAMPLES below). OPTIONS
The -r option causes bf_tar to remove inactive log files after the archive has been written successfully. The default is to leave log files. The -R option causes bf_tar to remove inactive log files before the archive is written. This may reduce chances that the resulting archive is recoverable should it become damaged. The archive may be smaller though. The default is to leave log files. EXIT STATUS
The script exits with status code 0 if everything went well, and nonzero if it encountered trouble. EXAMPLES
o bf_tar ~/.bogofilter > outfile.tar Writes a standard .tar file containing the essential files from ~/.bogofilter to outfile.tar. o bf_tar ~/.bogofilter | gzip -9 -c > outfile.tar.gz Writes a gzipped .tar.gz file containing the essential files from ~/.bogofilter to outfile.tar.gz. o bf_tar `pwd`/mydirectory > outfile.tar Prepend $(pwd)/ or `pwd`/ if you want to specify an absolute path instead of a relative path. NOTES
This script is meant for use with Berkeley DB based bogofilter versions. This script requires a SUSv2 compliant pax utility. This script expects a SUSv2 compliant shell. Solaris systems should have the SUNWxcu4 package installed (when bogofilter is configured) so that /usr/xpg4/bin/sh can be used. AUTHORS
Matthias Andree Bogofilter 03/15/2010 BF_TAR(1)
All times are GMT -4. The time now is 01:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy