Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if scp command is available?

Tags:

scp

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.

like image 501
sorin Avatar asked Jun 02 '11 14:06

sorin


2 Answers

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.

like image 100
Rahul Avatar answered Sep 28 '22 06:09

Rahul


#!/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.

like image 33
Mel Avatar answered Sep 28 '22 05:09

Mel