5

I am looking for a multiplatform solution that would allow me to check if scp command is available.

The problem is that scp does not have a --version command line and when called without parameters it will return with exit code 1 (error).

Update: in case it wasn't clear, by multiplatform I mean a solution that will work on Windows, OS X and Linux without requiring me to install anything.

sorin
  • 137,198
  • 150
  • 472
  • 707
  • This thread was helpful for me: http://stackoverflow.com/questions/304319/is-there-an-equivalent-of-which-on-the-windows-command-line. As mentioned in the answers here, you'd use `which scp` for non-windows systems. On windows systems you'd use `where.exe scp` – KenHBS Feb 14 '17 at 09:46

2 Answers2

7

Use the command which scp. It lets you know whether the command is available and it's path as well. If scp is not available, nothing is returned.

Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
Rahul
  • 71,392
  • 13
  • 57
  • 105
2
#!/bin/sh

scp_path=`which scp || echo NOT_FOUND`

if test $scp_path != "NOT_FOUND"; then
        if test -x ${scp_path}; then
                echo "$scp_path is usable"
                exit 0
        fi
fi
echo "No usable scp found"

sh does not have a built-in which, thus we rely on a system provided which command. I'm not entirely sure if the -x check is needed - on my system which actually verifies if the found file is executable by the user, but this may not be portable. On the rare case where the system has no which command, one can write a which function here.

Mel
  • 5,748
  • 1
  • 13
  • 12