LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   detecting a file through bash or other means (https://www.linuxquestions.org/questions/linux-general-1/detecting-a-file-through-bash-or-other-means-261944/)

wayloud 12-02-2004 07:49 PM

detecting a file through bash or other means
 
Basically I am looking for this bash script functionality:

if [ -f /tmp/test ]; then
echo "hi"
fi

via command line. It's usage is fairly specific so let me explain what I am doing. I need to use this command via ssh to determine remotely if a file exists. So I will do this:

ssh machine_name some_command

The trick is, I need the some_command to return true if the file exists or no-true if the file doesn't. SSH of course will then exit with the return code of some_command.

The problem that I am running into is that the "echo hi" has it's own return code that is getting in the way.

Any thoughts?

jailbait 12-02-2004 09:38 PM

"The trick is, I need the some_command to return true if the file exists or no-true if the file doesn't. SSH of course will then exit with the return code of some_command."

You can use the exit command to set the return code. I would use 0 for file exists and 1 for file does not exist.

if [ -f /tmp/test ]; then
echo "hi"
exit 0
fi
exit 1

---------------------------
Steve Stites

theYinYeti 12-03-2004 02:27 AM

Simply:
ssh machine_name '[ -f /tmp/test ]'

Or did I understand something wrong?

Yves.

theYinYeti 12-03-2004 02:28 AM

Note that -f is for testing that "it" exists AND is a regula file. For only testing existence, you have to use -e.

Yves.

wayloud 12-03-2004 11:46 AM

Quote:

Originally posted by theYinYeti
Simply:
ssh machine_name '[ -f /tmp/test ]'

Or did I understand something wrong?

Yves.

ugh the brackets... I totally missed that one. Yes thank you that works fine.


All times are GMT -5. The time now is 08:54 AM.