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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting Help :0( 1

Status
Not open for further replies.

Germo

Technical User
Mar 29, 2004
123
0
0
GB
Hi All Guru's

I am not a strong scripter so any help with my question would be great...

I would like a script that compares two file sizes and if Filename2 is less in size than Filename1 then write to filename3 "Alert"

Now I know some of you out there may think that this is an easy one but I don't...... Sorry

Thank You for your help
 
Code:
# ls -l txt1 txt2
-rw-r--r--    1 root     system            3 Mar 02 12:20 txt1
-rw-r--r--    1 root     system            2 Mar 02 12:20 txt2

# cat txt1
aa

# cat txt2
a

# cat b
#!/usr/bin/ksh

size1=$(ls -l /tmp/txt1 | awk '{print $5}')
size2=$(ls -l /tmp/txt2 | awk '{print $5}')
if [[ $size2 -lt $size1 ]]
then
        echo "Alert" > txt3
fi

# cat txt3
Alert
 
[[ $var = "$var" ]] # string commparison
[[ $var = $var ]] # pattern match

As noted in the first example, enclose the right side in double quotes to make it a string comparison because [[ ]] is for pattern matching.

Also note there is a difference between [ ... ] and [[ ... ]]
The single brackets do pathname generation and word splitting. For example:
Code:
[ $path = /tmp/mydir* ]
Then path could match /tmp/mydir1, /tmp/mydir2, /tmp/mydirWhatever

But
Code:
[[ $path = /tmp/mydir* ]]
turns off pathname expansion and you need to have a /tmp/mydir* or it will return false.

Same for word splitting in [ .. ] which will cat text1 and text2 below.
Code:
files="text1 text2" 
cat $files

if [[ .. ]] is used for the above example, then it will look for a file called "text1 text2" because word splitting is off.

You really could and should do:
Code:
integer size1=$(ls -l /tmp/txt1 | awk '{print $1}')
integer size2=$(ls -l /tmp/txt2 | awk '{print $2}')
if (( size2 < size1 ))
 
Also note there is a difference between:
Code:
(( expr ))
$(( expr ))
These will yield different results:
Code:
$(( $var * $var ))
$(( var * var ))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top