Determine the exist status


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Determine the exist status
# 1  
Old 05-14-2013
Determine the exist status

Hi,

i am a novice in scrpting and need your help on the following. i have a script that checks if a file exists:
Code:
if [ -f  $path/file1 ] ; then
echo " Files exists"
else 
echo " file not found"
fi

assuming the files does not exist ,, the script when run dusplays " file not found".. so far true. but when i type : echo $? i get output as 0.
shouldnt it say 1 if a condition fails.Smilie if possible can someone plz explain why it shows 0 instead of 1 when a condition has failed.

thanks

Last edited by joeyg; 05-14-2013 at 02:53 PM.. Reason: Please wrap commands and data inside CodeTags
# 2  
Old 05-14-2013
Did it? What condition should have failed?

Where did you put 'echo $?'

$? changes every time you run a statement, so it's easy to miss your chance at it having the value you're interested in.
# 3  
Old 05-14-2013
Do the following exercise
Code:
[ -f /does/not/exist ]
echo $?
echo $?

The second echo returns the exit status of the first echo.
BTW always quote variables in command arguments!
[ -f "$path/file1" ] or [ -f "$path"/file1 ]
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to see the status of all the ftp put & get files logs and curent ftp transfer status ?

How to see the status of all the ftp put & get files logs and curent ftp transfer status if any active ftp running in the background ? (2 Replies)
Discussion started by: i4ismail
2 Replies

2. Programming

How to track table status delete/update/insert status in DB2 V10 z/os?

Dear Team I am using DB2 v10 z/os database . Need expert guidance to figure out best way to track table activities ( Ex Delete, Insert,Update ) Scenario We have a table which is critical and many developer/testing team access on daily basis . We had instance where some deleted... (1 Reply)
Discussion started by: Perlbaby
1 Replies

3. Shell Programming and Scripting

Propagate exist status from a child shell script to a parent.

Hi All, I have a parent shell script A and a child shell script B. 1). If a command i.e a mysqdump fails in shell script B fails then I trap the error with this code if ] then func_exit "Failed to export the cleaned DB1.${MYDBNAME} database to the ${MYARCHIVEDIR} directory"... (1 Reply)
Discussion started by: daveu7
1 Replies

4. UNIX for Dummies Questions & Answers

Check the exist status of the cd command

Hi, As in scripting , some cd commands getting failed, so we do check the exist status as 0 or 1. But every time we call to function for it. But does any single line exist will do the job with || , && ? i.e ls -l Logs cd Logss | exit echo hias Logss is not exist , Before printing "hi" we... (5 Replies)
Discussion started by: posix
5 Replies

5. UNIX for Dummies Questions & Answers

302 server status code to 301/404 server status code

Hello, Sorry for my english. I have an arcade site. mydomain.com/game.html If database has the game name is good. mydomain.com/fd43f54.html if database has not the game name redirect to mydomain.com by 302 error code. if database has not the game name i want a 301/404 error code and no... (0 Replies)
Discussion started by: hoo
0 Replies

6. Programming

Can C determine which OS it's being compiled on?

Hello all! I've searched the archives, google, documentation and I can't seem to find any answer regarding my question. Our code has to be lint free and due to the following lint warning ---> logical expression always true: op "||" <--- we are forced to #include <note.h > (which appears to be... (3 Replies)
Discussion started by: quattro20v
3 Replies

7. Red Hat

Installing RedHat 8.0 onto Dell PowerEdge SC1425 - hdc: status error: status = 0x58

I have successfully installed RedHat 8.0 onto a Dell PowerEdge SC1425 today. This server has two SATA hard drives, and an IDE DVD-ROM drive. Using the following kernel parameters, i successfully installed across both hard drives from CD: ide0=0x1f0,0x3f6,14 vga=791 resolution=1024x768 expert... (5 Replies)
Discussion started by: fishsponge
5 Replies

8. Shell Programming and Scripting

check the status and send an email with status

Hi, We have a text file which has the following data. ISA~00~ ~00~ ~ZZ~VISTN ~ZZ~U1CAD ~051227~183 7~U~00200~000011258~0~P~< GS~FA~EE05J~U1CAD~051227~1831~000011258~X~002002 ST~997~0001 AK1~SH~247 AK2~856~2470001 AK5~A AK2~856~2470002 AK5~A... (3 Replies)
Discussion started by: isingh786
3 Replies

9. UNIX for Dummies Questions & Answers

How do I determine # of processors ...

I have a console server that runs some form of UNIX/Linux, but I get a bash shell, and I want to determine how many processor (what speed) and them amount of RAM in the system. (2 Replies)
Discussion started by: bbrandeb49
2 Replies

10. UNIX for Dummies Questions & Answers

Couldn't open status file /var/samba/STATUS.LCK

I believe i have most of samba configured right but i get this error each time time try to run it. I was given suggestion that i touch the file, i did, but i still cannot rid myself of this error. Any suggestions (2 Replies)
Discussion started by: macdonto
2 Replies
Login or Register to Ask a Question