Bash编程与文件系统功能

编程入门 行业动态 更新时间:2024-10-13 04:21:08
本文介绍了Bash编程与文件系统功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 本周我一直很忙,试图围绕一个小的Bash程序,将CMS从一个服务器迁移到另一个服务器。这样做的重要原因是因为我有40多个这样做,需要及时完成这个工作,因此Bash的想法。

不用说,到目前为止我遇到了一些问题,但其中一个已经完全停止了我的开发,目录检查。

没有我尝试了几种方法,没有一个似乎工作真的。抓到是我必须通过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编程与文件系统功能

本文发布于:2023-05-28 06:59:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/315177.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件系统   功能   Bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!