compariosn troubles...


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers compariosn troubles...
# 1  
Old 11-12-2003
Data compariosn troubles...

Hi Guys,

I am trying to compare using if, but keep getting some strange results.

if [ "${batch_count}" > "1" ]; then
keeps creating the file 1

if [ "${batch_count2}" -gt "1" ]; then
does not work at all

if [ "${batch_count2}" -gt 1 ]; then
does not work

if [ ${batch_count2} -gt 1 ]; then
does not work

if [ ${batch_count2} -gt "1" ]; then
does not work eihter.

I am using a ksh, on Solaris

I have tried the same permutations using the ">" sign as well.

I am stumped.

Thanks in advance.
Smilie
# 2  
Old 11-12-2003
You can't use > with a [ ] style test. You can use -gt with a [ ] style test. If that doesn't work for you. you have garbage in your variable.

You should switch to [[ ]] style tests but stay with -gt. > in legal with [[ ]] but it is not a numeric test.
# 3  
Old 11-12-2003
I have echoed the variable content to the screen to make sure that there is actually a usable variable, which there is.

But still cannot get the script to work.

# Set variable to path
cd $SSM_IN_DIR
fail_path=`pwd`
pwd
# Count the files in the directory
batch_count=`ls -tr | wc -w`
if [[ "${batch_count}" -gt 1 ]]; then

The variable is picking up that there are files in the diretory.
But my IF statement will not compare it the the value 1.

What I am trying to do is, remove files form a directory. After an application failure. This needs to be done before the application can be used again.
# 4  
Old 11-12-2003
I am using HPUX 11.X - the code worked for me. In your code, be sure that you have 1 space after the [[ and 1 space before the closing ]] as these are required. Also, make sure you are using the correct "magic number" for your shell/system. See bold.
Code:
#!/usr/bin/ksh
  
batch_count=`ls -tr | wc -w`
   
if [[ $batch_count -gt 1 ]]
 then
   echo "Yep its greater than 1 [ $batch_count ]"
 else
   echo "Nah, didnt work [ $batch_count ]"
fi

added code tags for readability --oombera

Last edited by oombera; 02-19-2004 at 05:32 PM..
# 5  
Old 11-12-2003
Still no go....

Here is my script.
Code:
#!/usr/bin/ksh

# This script will delete any failed files in the $SSM_IN_DIR

# Set variable to path
cd $SSM_IN_DIR
fail_path=`pwd`

# Count the files in the directory
batch_count=`ls -tr | wc -w`
batch_count2=`expr ${batch_count} - 1`
if [[ ${batch_count2} -gt 1 ]]; then
#	clear
	cd ${fail_path} | ls -lsF
	echo ""
	if [[ ${batch_count} -gt 1 ]]
	 then
		echo "There are" ${batch_count} "files in ${fail_path} directory."
		echo ""
	elif [[ ${batch_count} -lt 2 ]]
	then
		echo "There is" ${batch_count} "file in ${fail_path} directory."
		echo ""
	fi
	echo "Delete files [Y/N]]: \c"
	read cont
	clear
	if [[ ${cont} = "Y" ]] || [[ ${cont} = "y" ]]
	then
		LOGFILE=`ls -tr ${fail_path}`
		for file in ${LOGFILE}
		do
			if [ ! -d $file ]]
			then
				clear
				cd ${fail_path} | rm $file
			fi
		done
		ls -lsF ${fail_path}
	elif [[ ${cont} = "N" ]] || [[ ${cont} = "n" ]]
	then
	elif [[ ${cont} = " " ]]; then
		echo "Sorry, that is an invalid choice!"
		exit
	else 
		echo "Not sure what you wish to do?"
		end
	fi
elif [[ ${batch_count2} -lt 1 ]]
then
	echo "There are no failed files in the ${fail_path} directory. "
	echo ""
	cd ${fail_path} | ls -ls
	echo ""
	read cont
fi

added code tags for readability --oombera

Last edited by oombera; 02-19-2004 at 05:33 PM..
# 6  
Old 11-12-2003
First, the example I gave for the magic number, make sure you use the path that is applicable to your system. The path I gave is how the admin set up the system I am using.

Second, regarding your script, I think your problem is that you are not matching your if and fi correctly. Try and keep it really simple to start. You can do this by converting all of your elif to if statements. That way you can better see what it is your script is doing. Once you get it working, you can edit it to combine your if statements. For now keep it simple.

Third, I would suggest using a case statement after your read command. You can accomplish a lot more, and your code will be easier to read if you do.

Lastly, I dont understand what it is you are trying to do with commands such as this cd ${fail_path} | ls -lsF when you dont do anything with the output.
# 7  
Old 11-12-2003
It is hard to read like that, but I have all of the IF statments indented so that I can see which one is linked to which FI.

Plus the cd ${fail_path} | ls -ls is purely an out put to so that the user can see that the files are gone.

Plus I have exactly the same script working for another use, all that is different is the path variable.

I am not so familiar with CASE and ESAC.

I suppose now is a good time to get a grip on that way of doing things.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. BSD

PF troubles on OpenBSD 5.0

I am setting up a system as an ADSL gateway. ADSL is working fine. PF is not forwarding for some reason. # ifconfig lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33196 priority: 0 groups: lo inet6... (0 Replies)
Discussion started by: John Tate
0 Replies

2. Shell Programming and Scripting

awk and tr troubles

I am trying to make all the fields containing lower case letters upper case and the third field of a file display ** instead. I have this: awk '{printf "%s %s ** %d %d\n", $1, $2, $4, $5}' database.txt | tr '' '' < database.txt And that only changes it to upper case, other... (4 Replies)
Discussion started by: Bungkai
4 Replies

3. Shell Programming and Scripting

for loop troubles

What I have here is a pretty textbook recursive function. Its purpose right now is simply to display all folders that don't contain folders. It works fine in all instances I can think of... except one. If there is a folder with a space in its name, the thing goes Kablooie. AFAIK the problem comes... (5 Replies)
Discussion started by: divisionbyzero
5 Replies

4. UNIX for Dummies Questions & Answers

Cron troubles

I am aware this question has been answered time and again. I feel I have tried everything I have seen on the net and really need help to get this working. Same old story. Shell script, working from command but not from cron. I need my script to take values from a .properties file. Tried... (2 Replies)
Discussion started by: airalpha
2 Replies

5. Shell Programming and Scripting

Encoding troubles

Hello All I have a set of files, each one containing some lines that follows that regex: regex='disabled\,.*\,\".*\"'and here is what file says about each files: file <random file> <random file> ASCII text, with CRLF line terminatorsSo, as an example, here is what a file ("Daffy Duck - The... (3 Replies)
Discussion started by: tukuyomi
3 Replies

6. Shell Programming and Scripting

if-statement troubles

I try to compare the day and month of someones birthday with the day and month of today, so my little bash script can send a mail to the person that has its birthday that day. The first line of the file birthdays looks like this: firstname,lastname,01/01/1990,.... The variable birthday's... (4 Replies)
Discussion started by: doc.arne
4 Replies

7. HP-UX

cron troubles

I have a cronjob that I need to run everyday and it needs to have todays date inputed, here is what I have, but is not working as expected.......... 23 02 * * * cd /path;./RequestSummaryReport.sh $(date +%Y-%m-%d) the output from mail gives me............. Date: Fri, 8 Feb 2008 02:12:07... (4 Replies)
Discussion started by: theninja
4 Replies

8. Shell Programming and Scripting

String Compariosn: Use of If and OR

How to use OR with If for string comparison?? What is wrong in these two codes?? 1. if test $1 != "SO" OR $1 != "NO" then echo "Invalid" else echo "Valid" 2. if(( $1 != "SO" || $1 != "NO" )) then echo "Invalid" else echo "Valid" Please help? (2 Replies)
Discussion started by: sandeep_hi
2 Replies

9. UNIX for Dummies Questions & Answers

ssh2 troubles

I'm trying to set up a secure and trusted connection between 2 boxes running solaris using ssh2. I've run ssh-keygen2 on the local box and on the remote box, created the identification file ( IdKey id_dsa_2048_a ) on the local machine and copied across the public key file from the local to... (5 Replies)
Discussion started by: PaulC
5 Replies

10. Programming

compiling troubles

i keep getting the following error with the code segment below when i try to compile the program. The code is from 'defs.h' parse error before '(' parse error before ')' stray '\' in program this is the code segment and the error is on the second line of the segment #define... (1 Reply)
Discussion started by: token
1 Replies
Login or Register to Ask a Question