As pLumo explained, your test will always succeed.
What you want to do is to test whether your pattern, $SSHPATH/$ID.*
, expands to something. This is different from testing with -f
whether it's a regular file, as the pattern may expand to multiple names and the -f
test can only be applied to a single pathname.
Locally, you can do this with
set -- "$SSHPATH/$ID".*
if [ -e "$1" ]; then ...; fi
This would set the positional parameters ($1
, $2
, etc.) to the names matching the pattern given to set
. The if
statements then tests whether the first match is an existing name (the pattern would remain unexpanded in most shells if it did not match anything).
To run this through ssh
:
ssh user@host "set -- '$SSHPATH/$ID'.*; [ -e \"\$1\" ]"
The wonky quoting ensures that the $SSHPATH
and $ID
is expanded by the local shell, while "$1"
is expanded remotely.
This would give you an exit status of zero ("success") if the pattern matched an existing name, and non-zero ("failure") if it did not (or if ssh
failed in some manner).
You may use this in an if
statement like so:
if ssh user@host "set -- '$SSHPATH/$ID'.*; [ -e \"\$1\" ]"; then
# do something for success
else
# do something for failure
fi
To test for non-existence, negate the test:
if ssh user@host "set -- '$SSHPATH/$ID'.*; [ ! -e \"\$1\" ]"; then
# do something for failure
else
# do something for success
fi
I have ignored your use of sshpass
, but you may possibly just prefix the ssh
with it as you have done in the question.
$SSHPATH/$ID.*
(something liketest ! -f "$SSHPATH/$ID.*"
). – muru Aug 08 '19 at 07:45[]
and just quoting the variables it didn't execute if the file didn't exist – Freedo Aug 08 '19 at 07:55