0

I usually run server scripts like this, with a here-document.

But when I test if a directory exists, the script continues even though the test fails -- why?

ssh $user@$ip /bin/bash <<SCRIPT

if [[ ! -d ~/.debfiles ]];then
  echo "Error: debfiles doesnt exist" 2> ~/error.txt
  exit 1
fi

SCRIPT

I run this from my local computer and the server in question is a VM.

2
  • 1
    You can verify that the test succeeds (there is no directory called .debfiles in the user's home directory) because you can see the error message? Note that nothing will be written to the error.txt file because echo writes to standard output by default and you're redirecting its standard error stream (the file will still be truncated (emptied)). Commented Jan 9, 2024 at 17:25
  • 2
    Please clarify what the intended behaviour is and what it is in this code that you say is being executed even though it shouldn't be executed. Commented Jan 9, 2024 at 17:39

2 Answers 2

2

As Kusalananda noted in his edit, the << syntax you're referring to is called a here document.

As other comments have noted, the intent of your script is somewhat vague, but consider structuring your script something like this:

user=me
ip=myhost
dir=Test

if ssh "$user"@$ip /bin/bash << SCRIPT
[[ ! -d ~/"$dir" ]]
SCRIPT
then
  echo "Error: $dir doesnt exist"
  exit 1
fi

echo the script continues

That way:

$ ssh me@myhost rm -rf Test
$ ./test.sh
Error: Test doesnt exist
$ ssh me@myhost mkdir Test
$ ./test.sh 
the script continues

For a here document as simple as this example, a "here string" would also suffice, provided that $dir doesn't contain any uncooperative characters:

if ssh "$user"@$ip /bin/bash <<< "[[ ! -d ~/$dir ]]"
then
...
2
  • You don't need a heredoc at all. You can simply write if ssh "$user@$ip" [ ! -d ~/$dir ]. There are quoting issues with ~/$dir in either case though: you'd need double quotes to protect it from the local shell but allow the $dir variable to be expanded, and single quotes on the remote to keep whatever it has become. So if ssh "$user@$ip" "[ ! -d ~/'$dir' ]". And this still presupposes your directory name value doesn't contain a single quote Commented Jan 10, 2024 at 8:32
  • @ChrisDavies No, it's not needed, I simply used it because it was the topic of the OP. But thank you. I do take your point. Commented Jan 10, 2024 at 18:37
-1

You're running the exit on the remote server. The section running there exits, and so ssh ends and the remainder of whatever's local then continues.

You've not shown any part that "continues even though the test fails", either on the remote host or the local system, so we cannot test with confidence we're trying what you're trying.

2
  • I tested the user's code with an added statement after the if statement, and I could not get both the body of the if statement, with the exit, and my added statement to execute. What would execute the remainder of the script according to you? Commented Jan 9, 2024 at 17:21
  • There's nothing else local given in the question. I'm assuming ssh <<SCRIPT ... SCRIPT; echo something else, where the OP wouldn't want something else to be output if the exit 1 was triggered, @Kusalananda. (I know the syntax is off; I'm hoping you'll see the intent.) Commented Jan 9, 2024 at 17:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.