Tag: bash

ssh打破了bash中的while循环

我使用这个bash代码上传文件到远程服务器,对于正常的文件,这工作正常: for i in `find devel/ -newer $UPLOAD_FILE` do echo "Upload:" $i if [ -d $i ] then echo "Creating directory" $i ssh $USER@$SERVER "cd ${REMOTE_PATH}; mkdir -p $i" continue fi if scp -Cp $i $USER@$SERVER:$REMOTE_PATH/$i then echo "$i OK" else echo "$i NOK" rm ${UPLOAD_FILE}_tmp fi done 唯一的问题是,对于名称中有空格的文件,for循环会失败,所以我将第一行replace为: find devel/ -newer $UPLOAD_FILE | while […]

如何在bash中使用getopts的例子

我想以这种方式调用myscript文件: $ ./myscript -s 45 -p any_string 要么 $ ./myscript -h >>> should display help $ ./myscript >>> should display help 我的要求是: getopt在这里获取input参数 检查-s存在,如果不是则返回错误 检查-s之后的值是45还是90 检查-p存在,之后是否有inputstring 如果用户input./myscript -h或只./myscript则显示帮助 我试过这个代码: #!/bin/bash while getopts "h:s:" arg; do case $arg in h) echo "usage" ;; s) strength=$OPTARG echo $strength ;; esac done 但是用这个代码,我得到错误。 如何用Bash和getopt做到这一点?

这里的文件给出了“意外的文件结束”错误

我需要我的脚本从terminal发送电子邮件。 根据我在这里看到的以及在线其他许多地方,我把它格式化为这样: /var/mail -s "$SUBJECT" "$EMAIL" << EOF Here's a line of my message! And here's another line! Last line of the message here! EOF 但是,当我运行这个我得到这个警告: myfile.sh: line x: warning: here-document at line y delimited by end-of-file (wanted 'EOF') myfile.sh: line x+1: syntax error: unexpected end of file …其中行x是程序中最后一行代码,行y是其中包含/ var / mail的行。 我尝试用其他东西(ENDOFMESSAGE,FINISH等)replaceEOF,但无济于事。 几乎我在网上find的所有东西都是通过这种方式完成的,而且我真的很新奇,所以我很难自己搞清楚。 任何人都可以提供帮助吗? […]

如何在Bash中以点分隔版本格式比较两个string?

有没有什么办法比较这样的string在bash,例如: 2.4.5和2.8和2.4.5.1 ?

awk / sed:如何做一个string的recursion查找/replace?

如何查找和replace以下每个事件: subdomainA.example.com 同 subdomainB.example.com 在/home/www/目录树(recursion查找/replace)下的每个文本文件中。

如何连接Bash中的stringvariables?

在PHP中,string连接在一起,如下所示: $foo = "Hello"; $foo .= " World"; 在这里, $foo变成了“Hello World”。 这在Bash中是如何完成的?

回声“#!”失败 – “未find事件”

以下失败,我不明白为什么: $ echo "#!" 以下也失败,同样的错误信息: $ echo "\#!" 错误消息: -bash: !": event not found 为什么会失败? 应该怎么做echo呢?

为什么你需要./(点斜线)之前的脚本名称运行在bash?

当在bash中运行脚本时,我必须在开头写入./ : $ ./manage.py syncdb 如果我不这样做,我会收到一条错误消息: $ manage.py syncdb -bash: manage.py: command not found 这是什么原因? 我想. 是当前文件夹的别名,因此这两个调用应该是等效的。 我也不明白为什么我不需要./运行应用程序时,比如: user:/home/user$ cd /usr/bin user:/usr/bin$ git (运行没有./ )

将string拆分成Bash中的数组

在一个Bash脚本中,我想将一行分成几块,并把它们放入一个数组中。 该行: Paris, France, Europe 我想让他们像这样的数组: array[0] = Paris array[1] = France array[2] = Europe 我想用简单的代码,命令的速度并不重要。 我该怎么做?

在bash脚本中使用expect来为SSH命令提供密码

对于那些想要回复的人,我应该使用SSH密钥,请放弃 我试图在bash脚本中使用expect来提供SSH密码。 提供密码的工作,但我不会在SSH会话中,因为我应该,回到bash。 我的脚本: #!/bin/bash read -s PWD /usr/bin/expect <<EOD spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com' expect "password" send "$PWD\n" EOD echo "you're out" 我的脚本的输出: spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com usr@$myhost.example.com's password: you're out 我想我的SSH会话,只有当我退出它回到我的bash脚本。 之所以我期望使用bash,是因为我使用了一个菜单,我可以select连接到哪个单元。 谢谢