detect file size then quit or continue


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers detect file size then quit or continue
# 1  
Old 01-10-2009
detect file size then quit or continue

Hi Guys, I'm running a cron that mails me a file every night. The file is based on form input from the web and each evening after I mail it I delete and start a new file by the same name. It's a csv file so I preload a line of headers into the file. So far, so good. On days when no one inputs anything I get a file mailed to me with just my headers in place. I would like to stop these files from being sent (no user input). I know the size of the file
if it just contains my headers -183

I can ask for the size like this:

ls -1 myfile.csv | awk '{print $5}'

What I need is the
'if print $5 = 183 die else....and then I can go through the regular stuff I do, mailing the file, deleteing and starting a new file.

steven
# 2  
Old 01-10-2009
not ls -one ls -ell the argument is lowercase l.

Code:
#!/bin/ksh
filesize()
{
     ls -l "$1" | awk '+{print $5}' | read fsize
     if [[ $fsize -eq  183 ]] ; then
           fsize=0
     fi
     echo "$fsize"
}

sz=$( filesize myfile.csv )
if [[ $sz -eq 0 ]]
   echo "No user input"
   exit
fi
# your code goes here

# 3  
Old 01-13-2009
I tried the above code, replace myfile.csv with the proper file name and didn't add my code to the bottom yet. Got the following errors:

%sh checkfilesize1
: not found
ls: fts_open: No such file or directory
awk: syntax error at source line 1
context is
>>> +{ <<<
awk: bailing out at source line 1
: bad variable name
checkfilesize1: 17: Syntax error: end of file unexpected (expecting "then")
%
# 4  
Old 01-14-2009
The + sign in the awk appears to be a typo.
The code can be simplified as follows.

Code:
#!/bin/ksh
fsize=$(ls -lad "myfile.csv"  | awk '{print $5}')
if [ ${fsize} -eq 183 ]
then
        echo "do nothing"
        exit
fi
echo "do something"
# your code goes here

# 5  
Old 01-22-2009
getting closer

Hi, thanks for the reply, I've just had time to plug it in and try it. I'm getting a syntax error as follows:

syntax error: `if' unmatched

I looked this up in google and ended up back here on another thread, smirk. I read through that and I seem to have filled the requirements for an if statement, 2 square brackets, white space on either end...command below...this is what I'm running now:

#!usr/local/bin/ksh
fsize=$(ls -lad "mydata.csv" | awk '{print $5}')
if [ ${fsize} -eq 183 ]
then
echo "do nothing"
exit
fi
echo "do something"

I dumbed it down to see if the first line worked, and it does:

#!usr/local/bin/ksh
fsize=$(ls -lad "mydata_godstone.csv" | awk '{print $5}')
print $fsize
fi

I'm not sure what's wrong now, I have an if and a then. thanks in advance. Steven
# 6  
Old 01-22-2009
Code:
Typo in the first line?
#!usr/local/bin/ksh

#!/usr/local/bin/ksh

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If file pattern exists in directory then continue

he below looks in $dir for any pattern of fileone. As is, it executes but only returns File found if the exact format in the script exsists. Why isn't a pattern of fileone being looked for and if it is in $dir, File found. I think that is what should happen. Thank you :). dir=/path/to if... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Wait and continue if directory stays same size

hi all, i want to make a bash script so it can monitor directory sizes ie if it stays the same size (for a certain time) i want it to run the script but if there still copying and the directory increases in size do not run the script i have heard "inotifywait" can do this, but theres... (7 Replies)
Discussion started by: robertkwild
7 Replies

3. Shell Programming and Scripting

Opening a file in vi and automatically save and quit this file using shell script

Hi friends, In my shell script, I want to open a file using vi editor. After opening the file in vi, I want to save and quit this file automatically.... all through shell script. the code segment is: ------------------------------------------------------------ cd ~/netfpga/projects/scone/sw/... (2 Replies)
Discussion started by: sachinteotia
2 Replies

4. Shell Programming and Scripting

Read from a file in unix(Continue from previous thread)

Hi This is continuation of previos thread status=running username=abc password=123456 server=linux The script was made which is used to capture the data from file ./scr test status It will give result running I have a case like status = running username=abc password=123456... (14 Replies)
Discussion started by: parthmittal2007
14 Replies

5. Shell Programming and Scripting

Script check for file, alert if not there, and continue checking until file arrives

All, Is there a way to keep checking for a file over and over again in the same script for an interval of time? Ie If { mail -user continue checking until file arrives file arrives tasks exit I don't want the script to run each time and email the user each time a file... (4 Replies)
Discussion started by: markdjones82
4 Replies

6. Shell Programming and Scripting

if no file then quit

I have a script that run several subscripts. I need to find out how to do two things. First I would like to check for a file and if that file is not there I want to quit the entire script without running the rest of the script which contain subscripts. If the file is there, I want it to continue... (1 Reply)
Discussion started by: libertyforall
1 Replies

7. Shell Programming and Scripting

perl how to exit a while loop and quit reading the input file

I am reading a file using While loop while <FILE> { $_ = <FILE>; process data... } I would like to quit reading the file once I encounter a String pattern. How do i do it. is it if (/SUMMARY/) { last; } I am having problems with uninitialized value in pattern... (1 Reply)
Discussion started by: subhap
1 Replies

8. Solaris

command to find out total size of a specific file size (spread over the server)

hi all, in my server there are some specific application files which are spread through out the server... these are spread in folders..sub-folders..chid folders... please help me, how can i find the total size of these specific files in the server... (3 Replies)
Discussion started by: abhinov
3 Replies

9. Shell Programming and Scripting

Continue Script when File Found

Hello All, I am trying to write a script that will only continue executing my script if a file exits. I know the directory of the file, so its just a matter of seeing if the file exists yet. If the file has not yet been created, I want the script to wait 10 minutes (600 seconds) and try again.... (7 Replies)
Discussion started by: Jose Miguel
7 Replies
Login or Register to Ask a Question