unix script to check whether particular file exists and to find its size


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting unix script to check whether particular file exists and to find its size
# 8  
Old 02-05-2008
compile time error when i execute this code

Hi all,
Please tell me to solve this issue,

ROTATE_FILES=" <PATH>"
REAL_FILE="MQ.log"

function rotate_files()
{
for j in $1;do
echo "Loop1"

if [ -d $j ]
then
for i in `ls -a /$j`
do

if [ $i = MQ.log ]
then
echo $i

FILESIZE=`ls -l $i|awk '{print $5}'` #......error......#
LIMIT=0

if [ $FILESIZE -ge 0 ] #......error......#
then
echo "the size is greater"
fi

fi

done
fi
done
}

rotate_files "$ROTATE_FILES"

-----------------------------------------------------------------

FILESIZE=`ls -l $i|awk '{print $5}'`
"ls: MQ.log: No such file or directory" <<<error message

if [ $FILESIZE -ge 0 ]
"[: -ge: unary operator expected" <<<error message

please tell me how to solve this
# 9  
Old 02-05-2008
Try providing full path instead of relative path

Quote:
FILESIZE=`ls -l $i|awk '{print $5}'` #......error......#
In case the script is executed from the different directory,then "$i" will expand to MQ.log and not the full path to it,so it throws an error.Either you can try running the script from the directory (ROTATE_PATH) or give the full path

Thanks
Nagarajan G
# 10  
Old 02-05-2008
Quote:
Originally Posted by Balachandar
Hi all,
Please tell me to solve this issue,

ROTATE_FILES=" <PATH>"
REAL_FILE="MQ.log"

function rotate_files()
{
for j in $1;do
echo "Loop1"

if [ -d $j ]
then
for i in `ls -a /$j`
do

if [ $i = MQ.log ]
then
echo $i

FILESIZE=`ls -l $i|awk '{print $5}'` #......error......#
LIMIT=0

if [ $FILESIZE -ge 0 ] #......error......#
then
echo "the size is greater"
fi

fi

done
fi
done
}

rotate_files "$ROTATE_FILES"

-----------------------------------------------------------------

FILESIZE=`ls -l $i|awk '{print $5}'`
"ls: MQ.log: No such file or directory" <<<error message

if [ $FILESIZE -ge 0 ]
"[: -ge: unary operator expected" <<<error message

please tell me how to solve this
change -ge to -gt

check if [[ ! -f $i ]];then
only check the size.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script which will check the target file and folder exists and copy it

Hi All, I am a beginner in this and trying to write a shell script in linux which will : 1. Ask for a file name and check if its exists. 2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists. 3. If target folder... (3 Replies)
Discussion started by: ashish_neekhra
3 Replies

2. Shell Programming and Scripting

Except script fails to check file exists or not in remote node

Dear members, The following expect script connects to remote node and check for the file "authorized_keys" in directory /root/.ssh in remote node. However the result is always found even if the file exist or doesn't exist. expect { "$fname" { send_user "found\n" } Any idea what is... (4 Replies)
Discussion started by: Sudhakar333
4 Replies

3. Shell Programming and Scripting

Script to check file exists

Hi, I am trying to write a script which checks if any file exists with "*.log" or "*.out" in Directory below is the code #------------------ path=/abd/xyz/ if ; then echo "Good" else echo "Failure" fi #-------------------------- its always going to else part and printing... (8 Replies)
Discussion started by: ch33ry
8 Replies

4. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

5. Shell Programming and Scripting

Script to check if file exists

guys, I am trying to write a script that does the following: it looks for a file in a specific directory and if the file is not there (NOT), it emails me. I have tried the following but its not working. It simply hangs up. Please help. if then mail -s 'blah blah blah' my email... (4 Replies)
Discussion started by: basisvasis
4 Replies

6. Shell Programming and Scripting

Shell script to check if any file exists in 4 folders

Hi All, working on AIX 5.3. Requirement is: Shell script in ksh to check if any file exists in 4 folders as below: 1. /FILE/INB/INT1 2. /FILE/INB/INT2 3. /FILE/INB/INT3 4. /FILE/INB/INT4 Thanks a lot for your time! a1_win. (3 Replies)
Discussion started by: a1_win
3 Replies

7. UNIX for Advanced & Expert Users

how to find the file size in unix

Anybody can help HOW TO FIND THE FILE SIZE IN UNIX (5 Replies)
Discussion started by: lmraochodisetti
5 Replies

8. Shell Programming and Scripting

Command/script to find size of Unix Box ?

Please could anyone provide me the Command/script to find the size and usage of Unix box ASAP ? (6 Replies)
Discussion started by: sakthifire
6 Replies

9. Shell Programming and Scripting

Need to write a script in UNIX to find a file if another file exists

So I have a lot of Java applications on my servers all having their own folder from the applications subdirectory. Now, I need to do the following. Search all the applications subdirectories for message.jar. If the message.jar file exists, I need to search the application directory for... (1 Reply)
Discussion started by: mmdawg
1 Replies

10. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies
Login or Register to Ask a Question