Script Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script Help
# 1  
Old 09-08-2011
Script Help

Hi ,

I am using the below script to check if a file is present if yes I will my next step if no this should return status as 1

Code:
if [[ -s $1 ]] 
then 
echo "File $1 FOUND!" 
exit 0 
fi 
echo "File $1 NOT FOUND" 
exit 1

But when I am executing this with the file present in the unix directory I am getting status as file FOUND and not found

Code:
#sh File_Size.sh test.xml
File_Size.sh[59]: File test.xml FOUND:  not found

And when I am executing with some dummy name it is working correctly.

Code:
#sh File_Size.sh sdjkjkdf
File_Size.sh[62]: File sdjkjkdf NOT FOUND:  not found

My problem is based on the status code I will continue my shell script but due to the incorrect code I am not able to proceed further in my code.

Please tell me where I am doing it wrong.

Regards,Shruthi
# 2  
Old 09-08-2011
Guess there's bug in some other part of the code. Can you post other lines of your code..?
# 3  
Old 09-08-2011
try this ....
you are missing an "Else"
Code:
if [[ -s $1 ]] 
then 
   echo "File $1 FOUND!" 
   exit 0 
else
   echo "File $1 NOT FOUND" 
   exit 1

fi

This User Gave Thanks to jville For This Post:
# 4  
Old 09-08-2011
I don't see any issue with your code.
Execute the code with -x option like this and see. It will help to identify the issue.
Code:
sh -x File_Size.sh sdjkjkdf

And are you sure the "echo" is in place? Cause it is treating as if the code is missing "echo" and treating "File test.xml FOUND" as a command which it cannot(obviously) find.

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 09-12-2011
Thanks for the replies, the script is some how not working as expected in our environment. is there any other way I can check if file exists if yes the it's size should be greater than 0 bytes. Based on this I should get a return code this return code I will use in the remaining shell script.
# 6  
Old 09-12-2011
Code:
#!/bin/bash
#Script name : run
#Usage : ./run <file_to_be_checked>
file_to_check=$1
test -s $file_to_check && size=$( stat -c %s $file_to_check ) || size=0
test $size -gt 0 && exit 0 || exit 1


root@bt:/tmp# ls -lrt *empty*
-rw-r--r-- 1 root root 0 2011-09-13 06:57 emptyfile
-rw-r--r-- 1 root root 9 2011-09-13 06:57 nonempty
root@bt:/tmp# ./run nonexistant  ;echo $?
1
root@bt:/tmp# ./run emptyfile  ;echo $?
1
root@bt:/tmp# ./run nonempty  ;echo $?
0

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

Hello all, I am facing a weird issue while executing a code below - #!/bin/bash cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset sh UKBA_publish.sh UKBA 28082015 3 if then echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
Discussion started by: ektubbe
2 Replies

3. UNIX for Dummies Questions & Answers

Calling a script from master script to get value from called script

I am trying to call a script(callingscript.sh) from a master script(masterscript.sh) to get string type value from calling script to master script. I have used scripts mentioned below. #masterscript.sh ./callingscript.sh echo $fileExist #callingscript.sh echo "The script is called"... (2 Replies)
Discussion started by: Raj Roy
2 Replies

4. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies
Login or Register to Ask a Question