LoginSignup
1
1

More than 5 years have passed since last update.

リモート先ディレクトリの存在を判定する

Last updated at Posted at 2018-01-11

やり方

USER=<Your SSH user>
IP=<Your IP>
PATH=<Your target dir path> # ~は使えない

if [ $(ssh $UESR@$IP "[ -d $PATH ];echo \$?") -eq 0 ]; then
    echo "exist"
else
    echo "nowhere"
fi

elseの処理がいらないのであれば、ワンラインで以下の方に記述もできます。

[ $(ssh $UESR@$IP "[ -d $PATH ];echo \$?") -eq 0 ] && echo "exist"

注意点

コメントでも記載したように、ディレクトリのパスに~が使えません。というもの、$()でコマンドを包括する際に、~をリモートではなくローカル側のパスに変換してしまうようです。

このあたりをスマートにできればいいんですが、気力が尽きました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1