script to check for a condition inside a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to check for a condition inside a file
# 1  
Old 03-21-2007
script to check for a condition inside a file

Hi
I am writing a script file which sends the log files along with their size in a folder named log to a file called temp.log using the following cmd:
ls -st 190_GSTV_HUX_003QISCGSK026** >> /home/user/temp.log

the temp.log looks like this:
16 190_GSTV_HUX_003QISCGSK026_message070321.log
21 190_GSTV_HUX_003QISCGSK026_activity070321.log
10 190_GSTV_HUX_003QISCGSK026_error070321.log

now i need to write a script which checks for the error log file size inside the temp.log
ie., i need to check if the error log file(190_GSTV_HUX_003QISCGSK026_error070321.log) size is zero or not from the file temp.log

is there any cmd to do the same?

any help is greatly appreciated
thanks in advance!!

Last edited by kiran1112; 03-21-2007 at 07:59 AM..
# 2  
Old 03-21-2007
Hi,

not sure whether I understood your problem properly...You can do something like this if u want to take out size and filename seperately ..

ls -st GSTV_error** >> /home/user/temp.log
cat temp.log | awk -F" " 'NR >1 {print "File "$2 " Size "$1 ;}'

or you can go for


while read line
do
{
file=$(echo $line| cut -f1)
size=$(echo $line| cut -f2)
echo "$file $size"
if [ "$file" = "something" -a $size -eq val ]
then

...do something..

fi
}
done <temp
# 3  
Old 03-21-2007
Ok.Try something like below then...

ls -st >temp
while read line
do
{
file=$(echo $line| cut -f2 -d" ")
size=$(echo $line| cut -f1 -d" ")
if [ "$file" = "190_GSTV_HUX_003QISCGSK026_error070321.log" -a $size -gt 0 ]
then
echo "$file and $size" #or do whatever you want to do with that..
fi
}
done <temp

Last edited by dennis.jacob; 03-21-2007 at 08:14 AM..
# 4  
Old 03-21-2007
ls -lst 190_GSTV_HUX_003QISCGSK026** >> /home/user/temp.log

the temp.log looks like...

16 blah blah blah blah size 190_GSTV_HUX_003QISCGSK026_message070321.log
21 blah blah blah blah size 190_GSTV_HUX_003QISCGSK026_activity070321.log
10 blah blah blah blah size 190_GSTV_HUX_003QISCGSK026_error070321.log


sixth column shows size of the file

Code:
check=`cat /home/user/temp.log | awk '{print $6}'`

it will show you size of the file....

if [ $check -eq 0 ]
then
echo "do this"
else
echo "do this"
fi
# 5  
Old 03-21-2007
Hi
many thanks for ur quick reply...
i am trying to do this:
ls -st 190_GSTV_HUX** >> /home/gaaadmin/GAAAdapter/gskgaa/test.log
cd ..
check=`awk 'NR == 6 {print $1}' test.log`

if [ $check -eq 0 ]
then
echo "noerror"
else
echo "error present"
fi

but the thing is this condition checks only for the first line in test.log which may or may not be the error log file ...
so my quwstion is how do we get it to check only for the filename which contains error...

hope i m not confusing you
thanks
# 6  
Old 03-21-2007
If you just want to display those files with zero size,
go for this...

awk '{if ($1>0) {print "no errors in "$2; }else{ print "Error in "$2;}} temp.log

Or if you can use the below approch to tackle this..
while read line
do
{
file=$(echo $line| cut -f2 -d" ")
size=$(echo $line| cut -f1 -d" ")
if [ "$file" = "190_GSTV_HUX_003QISCGSK026_error070321.log" -a $size -gt 0 ]
then
echo "$file and $size" #or do whatever you want to do with that..
fi
}
done <temp
# 7  
Old 03-21-2007
Guys, please wrap your code within the code tags for the benefit of everyone.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Multiple checks inside if condition in a bash shell script

Hi, I need to perform the untar and rm operation if the file found is a .tar and does not have test.tar or hello.tar as the file names. Below is the loop to check the same. for tf in *.tar do if ] then found=1 ... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

Substring check in IF condition in shell script

I want to check if the string has the substring in IF condition then process... i tried below but not working if ]; then ............. field can be "reserved1" ....reservedn / fillspaces1 ... fillspacesn (4 Replies)
Discussion started by: greenworld123
4 Replies

3. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. Shell Programming and Scripting

Linux Shell Script to check for string inside a file?

This is what I have so far grep -lir "some text" * I need to check all files on the system for this text, if the text is found I need to change the file permissions so only ROOT has read and write access. How can this be done? (3 Replies)
Discussion started by: mapleleafs89
3 Replies

5. Shell Programming and Scripting

Replace text inside XML file based on condition

Hi All, I want to change the name as SEQ_13 ie., <Property Name="Name">SEQ_13</Property> when the Stage Type is PxSequentialFile ie., <Property Name="StageType">PxSequentialFile</Property> :wall: Input.XML <Main> <Record Identifier="V0S13" Type="CustomStage" Readonly="0">... (3 Replies)
Discussion started by: kmsekhar
3 Replies

6. Shell Programming and Scripting

Check condition inside the loop

Hi, I am in trouble. I can get inside my condition test inside a loop : I am in ksh (solaris) while read file do <commande to retrieve file> >> ${LOG_RETRIEVE_FILE.log} msg_err=$(cat ${LOG_RETRIEVE_FILE.log} | grep "error retrieve") if ; then <sendmail> exit 1 fi done I tried... (6 Replies)
Discussion started by: Aswex
6 Replies

7. Shell Programming and Scripting

Using ssh to transfer file not working inside if-condition

Hi all, ssh uname@remote_server 'cat /tmp/remote_file_name > home_dir/a512386/new/local_file_name' The above given command is working fine. but if i try to move the file only if exists in the remote server i.e) giving the same within if condition it executes but the file is not stored in my... (1 Reply)
Discussion started by: Shri123
1 Replies

8. Shell Programming and Scripting

Error while using sqlplus command inside 'if' condition in an unix shell script

Hi all, I am using the below given sqlplus command in my unix script to invoke a stored procedure which returns a value .It works fine. RET_CODE=$(/opt/oracle/product/10.2.0.4.CL/bin/sqlplus -S $USER/$PASSWD@$DB_NAME <<EOF EXEC MY_PKG.MY_SP (:COUNT); PRINT COUNT; commit; ... (6 Replies)
Discussion started by: Shri123
6 Replies

9. Shell Programming and Scripting

Help with shell script to check the condition.

:) Hi, I want to script for this scenerio, OSR Settings Scenario : We are looking to find all the *.a files from the following locations in the filesystem of a server. OSR Directories /etc /bin /usr/bin /usr/sbin /var/adm These *.a files should have the permissions on... (12 Replies)
Discussion started by: sakthilinux
12 Replies

10. Shell Programming and Scripting

using flag inside a for loop to check condition

I have a logic like this It initializes the flag variable as "T" at the beginning of the loop everytime Inside each loop it checks for two conditions and updates the flag variable as "A" or "B" In the end of the loop it checks for the value of the variable flag for "A" or "B" and execute... (4 Replies)
Discussion started by: codeman007
4 Replies
Login or Register to Ask a Question