0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

sshでリモート先に目的のファイルが存在するかの確認

Last updated at Posted at 2021-02-19
  • sshで、リモート先に目的のファイル、あるいはディレクトリが存在するのか確認したい。
  • sshでログインはしたくない。
  • ローカル側のスクリプト内で実行したい。

ファイルを確認したい
ファイルだったら 戻り値0
ファイル以外だったら 戻り値1

$ ssh user@host.local 'test -f  ~/data/testfile'
echo $?
0

ディレクトリを確認したい場合

$ ssh user@host.local 'test -d  ~/data/testdir'
echo $?
0

間違ってはいないけど無駄だった方法

$ ssh user@host.local 'test -f  ~/data/testfile;echo $?'
0

ディレクトリを確認したい場合

$ ssh user@host.local 'test -d  ~/data/testdir;echo $?'
0

スクリプト内で戻り値を取る

RC=$(ssh user@host.local 'test -d  ~/data/testdir;echo $?')

$RC を if文等で評価すれば良し

0
1
2

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?