Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让bash脚本检查远程目录是否存在如果它存在运行脚本_Bash - Fatal编程技术网

如何让bash脚本检查远程目录是否存在如果它存在运行脚本

如何让bash脚本检查远程目录是否存在如果它存在运行脚本,bash,Bash,但我不知道如何添加下一个条件,以便仅在文件夹存在且文件夹位于远程服务器上的情况下为每个文件夹运行脚本: #Variable with date Now_date=`date +%Y%m%d` #adding source for an original configuration file source=/home/bin/custome.conf #adding varibale to create a copy of original file with date stamp and cop

但我不知道如何添加下一个条件,以便仅在文件夹存在且文件夹位于远程服务器上的情况下为每个文件夹运行脚本:

#Variable with date
Now_date=`date +%Y%m%d`
#adding source for an original configuration file
source=/home/bin/custome.conf
#adding varibale to create a copy of original file with date stamp and coping it
customize="$source-$Now_date"
cp -af -- "$source" "$customize"
#Adding massive with folders 
folders=( "Accounts" "Salary" "Taxes" "Review" "Data")
#Launching script by folderds
for group in ${folders[@]}; do
#That a variable for a string we replace
    DIR_INCOMING_REMOTE="/incoming/deltas-${Now_date}/${folders}"
#replacing configuration string in the file /home/bin/custome.conf
    sed -i "/DIR_INCOMING_REMOTE/s|.*|export DIR_INCOMING_REMOTE='${DIR_INCOMING_REMOTE}'|g;" "$customize"
#Launching external script which processing $customize.conf file(now it looks like customize.conf-20200206) and checking for ok message
    ./external_script.sh "$customize" && { echo "exiting fine"; continue; } || { echo "failed"; break; }
done

这种构造可能会有所帮助

if ssh 192.168.101.10 '[ -d /home/incoming/deltas-${Now_date}/${folders} ]' 
then
{
   # perform directory check exist for /home/incoming/deltas-20200206/Salary and than run script. the same for other folders, if folder not exists, just skip folder upload. It will run in cron and check for folder creation and than execute script for a next folder.

}
fi

基于@tripleee注释的更新

ssh HOST "[[ -d TESTING_DIR ]] || exit 1" && echo ok || echo fail

您可以将其作为嵌套命令运行;然后按如下方式检查退货:

ssh HOST test -d "TESTING_DIR" && echo ok || echo fail

是否要在远程或本地运行脚本?单引号阻止插入变量。除此之外,我看不出有什么问题。当然大括号是非常不必要的。脚本将在本地运行
| |退出1
是非常不必要的
ssh
将已经从远程shell返回退出代码。但如果目录名包含变量插值,则应引用该目录名。并且可能会切换到
[
,因为这里的
[[
没有特别的好处,而且您不知道远程shell是否是Bash。
$(ssh dc1 [ -d /some/path ] )
if [ $? -eq 0 ]; then 
    # The path exists
    # more stuff ....
fi;