Click to See Complete Forum and Search --> : Bash help


binaryDigit
03-25-2001, 02:35 PM
#!/bin/sh

# using month and day from date. month is $2 and day is $3
set $(date)

# file variables
temp="/home/phil/fw_hits/temp_diff/temp"
hits="/home/phil/fw_hits/fw_hits_$2_$3"
difx="/home/phil/fw_hits/temp_diff/difx"
owner="phil:users"

# run the filter on /var/log/messages grep Packet log: will give us any firewall hits
# grep "$2 $3" will exclude the firewall hits we get by the current day and month
# outputting to a temp file
cat /var/log/messages | grep 'Packet log:' | grep "$2 $3" > $temp

# if the temp file has a non-zero size we know we had a firewall hit
# otherwise we won't continue
if [ -s $temp ]; then
# if a hits file for the current day already exists then
# check to see if temp has more firewall hits than what
# is already in hits file
if [ -e $hits ]; then
diff $temp $hits > $difx
if [ -s $difx ]; then
# copy the temp file to the hits file if temp sees firewall hits
cp $temp $hits
# if this is run in ip-down then the file will be owned by root
# so change ownership so i don't have to su
chown $owner $hits
# there are firewall hits for the current day so let me know
echo -e \\a
fi
else
touch $hits
# copy the temp file to the hits file if temp sees firewall hits
cp $temp $hits
# if this is run in ip-down then the file will be owned by root
# so change ownership so i don't have to su
chown phil:users $hits
# there are firewall hits for the current day so let me know
echo -e \\a
fi
fi
# not going to remove temp file in any situation because it's just going to be used again.
# -binaryDigit


ok my question is how can i check for the size of a file. what i am trying to do is make sure that if i've already
got firewall hits for a certain day/month that i only copy the temp file to it if there are more firewall hits.
i made it work by outputting a diff operation to a file and checking for a non-zero size on the file. i was just wondering if there was a better way.

sfam
03-25-2001, 07:35 PM
Originally posted by binaryDigit:
ok my question is how can i check for the size of a file. what i am trying to do is make sure that if i've already
got firewall hits for a certain day/month that i only copy the temp file to it if there are more firewall hits.
i made it work by outputting a diff operation to a file and checking for a non-zero size on the file. i was just wondering if there was a better way.

To check the size of a file you could do:
ls -l <filename> | awk '{ print $5 }'

That would return the file size. You said you just wanted to know if you had more firewall hits. Another way to do that would be to check the modified time. To do that you could use the same command I just showed you, but change the 5 to an 8.

binaryDigit
03-26-2001, 12:27 AM
ok. thanks, but how do i get that into a variable?



file_size=ls -l <somefile> | awk '{ print $5 }'



???

sfam
03-26-2001, 03:11 AM
file_size=ls -l <somefile> | awk '{ print $5 }'



Yes, except that you need to surround that in backquotes.



file_size=`ls -l <somefile> | awk '{ print $5 }'`



That tells the script to assign the results of that command to the file_size variable.

[ Edit: Can't figure out those damn UBB codes :rolleyes: ]

[ 26 March 2001: Message edited by: sfam ]

ShadowGoblin
03-26-2001, 03:27 AM
I get the same thing when my hammer hits my keyboard repeatedly.Because I get pissed that the linux distro`s still have not figured out a way to double click on an item an have it just run or install!


#!/bin/sh# using month and day from date. month is $2 and day is $3set $(date)# file variablestemp="/home/phil/fw_hits/temp_diff/temp"hits="/home/phil/fw_hits/fw_hits_$2_$3"difx="/home/phil/fw_hits/temp_diff/difx"owner="phil:users"# run the filter on /var/log/messages grep Packet log: will give us any firewall hits# grep "$2 $3" will exclude the firewall hits we get by the current day and month# outputting to a temp filecat /var/log/messages | grep 'Packet log:' | grep "$2 $3" > $temp# if the temp file has a non-zero size we know we had a firewall hit# otherwise we won't continueif [ -s $temp ]; then # if a hits file for the current day already exists then # check to see if temp has more firewall hits than what # is already in hits file if [ -e $hits ]; then diff $temp $hits > $difx if [ -s $difx ]; then # copy the temp file to the hits file if temp sees firewall hits cp $temp $hits # if this is run in ip-down then the file will be owned by root # so change ownership so i don't have to su chown $owner $hits # there are firewall hits for the current day so let me know echo -e \\a fi else touch $hits # copy the temp file to the hits file if temp sees firewall hits cp $temp $hits # if this is run in ip-down then the file will be owned by root # so change ownership so i don't have to su chown phil:users $hits # there are firewall hits for the current day so let me know echo -e \\a fifi# not going to remove temp file in any situation because it's just going to be used again.# -binaryDigit

binaryDigit
03-26-2001, 08:18 PM
sfam: thanks i'll try that. :)