Bash编程与文件系统功能 [英] Bash programming with filesystem functions
问题描述
不用说,到目前为止我遇到了一些问题,但其中一个已经完全停止了我的开发,目录检查。
没有我尝试了几种方法,没有一个似乎工作真的。抓到是我必须通过ssh检查远程服务器上的文件夹。这里我的例子:
ExSshRsa =〜/ .ssh / id_rsa
ExSshPort = 22
ExSshHost = localhost
ExRoot = / var / www /
echo -n验证根访问$ ExRoot ...
SSHRoot ='ssh -i $ ExSshRsa -p $ ExSshPort $ ExSshHost [-d $ ExRoot] ||退出1'
echo $ SSHRoot
如果[$ SSHRoot-eq 0]
然后
echoOK
else
echoFAIL
fi
我得到错误: [::integer expression expected
[或测试不会重新设置0是数值。
将字符串作为参数传递给远程主机不是微不足道的;您需要使用阵列。一个测试示例:
declare -a cmd =(触摸带空格的文件名)
declare -a escaped_cmd
escaped_cmd + =($(printf%q$ arg)
done
ssh localhost
$$ { $ {escaped_cmd [@]}
ssh localhost ls#在单独的行上返回带空格的文件名
所以你的情况应该是:
ExSshRsa =〜/ .ssh / id_rsa
ExSshPort = 22
ExSshHost = localhost
ExRoot = / var / www /
echo -n验证根访问$ ExRoot ...
declare -a cmd =('['-d'$ ExRoot']')#需要引用[,因为它是一个Bash特定的符号
declare -a escaped_cmd
for arg$ {cmd [@ ]}
do
escaped_cmd + =($(printf%q$ arg))
done
ssh -i $ ExSshRsa -p $ ExSshPort $ ExSshHost $ {escaped_cmd [@]}
如果[$? -eq 0]
然后
echoOK
else
echoFAIL
fi
I have been busy this week trying to wrap my head around a little Bash program to migrate a CMS from one server to another. The reasopn for this is because I have more tha 40 of these to do, and need to get it done in a timely manner, thus the Bash idea.
Needless to say, I have run into a couple of problems so far, but one of them has halted my development completetly, directory checking.
No I have tried a couple of methods and none of them seem to work really. The catch is that I have to check the folder on a remote server via ssh. Here my example:
ExSshRsa=~/.ssh/id_rsa
ExSshPort=22
ExSshHost=localhost
ExRoot=/var/www/
echo -n "Verifying Root access $ExRoot..."
SSHRoot='ssh -i $ExSshRsa -p $ExSshPort $ExSshHost [ -d $ExRoot ] || exit 1 '
echo $SSHRoot
if [ "$SSHRoot" -eq 0 ]
then
echo "OK"
else
echo "FAIL"
fi
I get the Error: [: : integer expression expected
Does the [ or test not resturn a 0 which is numerical. ?
Passing strings as arguments to a remote host is not trivial; you need to use arrays. A test example:
declare -a cmd=(touch "file name with spaces")
declare -a escaped_cmd
for arg in "${cmd[@]}"
do
escaped_cmd+=($(printf %q "$arg"))
done
ssh localhost "${escaped_cmd[@]}"
ssh localhost ls # Should return "file name with spaces" on a separate line
So your case should be:
ExSshRsa=~/.ssh/id_rsa
ExSshPort=22
ExSshHost=localhost
ExRoot=/var/www/
echo -n "Verifying Root access $ExRoot..."
declare -a cmd=( '[' -d "$ExRoot" ']' ) # Need to quote "[" since it's a Bash-specific symbol
declare -a escaped_cmd
for arg in "${cmd[@]}"
do
escaped_cmd+=($(printf %q "$arg"))
done
ssh -i $ExSshRsa -p $ExSshPort $ExSshHost "${escaped_cmd[@]}"
if [ $? -eq 0 ]
then
echo "OK"
else
echo "FAIL"
fi
这篇关于Bash编程与文件系统功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!