Questions tagged «bash»

与其他Bourne / POSIX shell相对,GNU的Bourne Again SHell特有的问题。对于一般关于Unix shell的问题,请改用/ shell标记。

2
如何从父外壳中的子外壳中获取变量
我编写了一个简单的脚本来为Web服务中的一些报告计时: BASE_URL='http://example.com/json/webservice/' FIRST=1 FINAL=10000 for report_code in $(seq 1 $FINAL); do (time -p response=$(curl --write-out %{http_code} --silent -O ${BASE_URL}/${report_code}) ) 2> ${report_code}.time echo $response # <------- this is out of scope! How do I fix that? if [[ $response = '404' ]]; then echo "Deleting report # ${report_code}!" rm ${report_code} …

4
使用bash脚本安装crontab
我创建了一个脚本,将两个脚本安装到crontab上。 #!/bin/bash sudo crontab -l > mycron #echo new cron into cron file echo "*/05 * * * * bash /mnt/md0/capture/delete_old_pcap.sh" >> mycron #schedule the delete script echo "*/12 * * * * bash /mnt/md0/capture/merge_pcap.sh" >> mycron #schedule the merge script #install new cron file crontab mycron rm mycron 该脚本将运行,并将这两行添加到crontab中。但是,如果我再次运行该脚本,它将再次添加这些行,因此,我将有四行内容相同。我希望运行安装脚本,以使插入crontab的行不会重复。我怎样才能做到这一点
11 linux  bash  cron 



1
非阻塞bash命令
假设我有一个bash脚本,其中包含以下内容: #!/bin/sh gedit rm *.temp 当我使用执行它时sh ./test.sh,会gedit弹出,但是rm直到关闭后该零件才运行gedit。 我希望脚本即使gedit未关闭也能继续运行;就像gedit不阻止bash执行一样。 我提供的示例只是一个示例(rm在实际情况下,第一个示例无效)。
11 bash  job-control 

4
bash函数的隐式返回?
说我有一个bash函数,如下所示: gmx(){ echo "foo"; } 此函数将隐式返回echo命令的退出值,还是使用return是必需的? gmx(){ echo "foo"; return $? } 我假设bash的工作方式是,bash函数的最后一条命令的退出状态是“返回”的状态,但不能100%确定。

3
如何将文件名拆分为变量?
假设我有以下格式的csv文件列表: INT_V1_<Product>_<ID>_<Name>_<ddmmyy>.csv ASG_B1_V1_<Product>_<ID>_<Name>_<ddmmyy>.csv 该INT_V1_&ASG_B1_V1_是固定的,这意味着所有的CSV文件,开始使用它。 如何将文件名拆分为变量? 例如,我想捕获Name并将其分配给变量$Name。
11 bash  ksh 

1
将SIGINT或SIGTERM发送给父脚本本身而不是子进程时执行命令或函数
假设我有这个 script.sh #!/bin/bash exit_script() { echo "Printing something special!" echo "Maybe executing other commands!" kill -- -$$ # Sends SIGTERM to child/sub processes } echo "Some other text" #other commands here sleep infinity 我想script.sh执行的功能exit_script时,它接收SIGINT或SIGTERM 例如: killall script.sh # it will send SIGTERM to my script 我希望我的脚本执行此操作 exit_script() { echo "Printing …

5
移动驻留在单个目录中的大量文件的最有效方法是什么?
CentOS 5.x 如果这是一个重复的问题,我深表歉意。我见过很多类似的问题(关于删除文件),但情况并不完全相同。 我有一个包含成千上万个文件的目录(可能超过一百万个),作为对一个不同问题的短期修复,我需要将这些文件移动到另一个位置。 为了讨论的目的,假设这些文件最初位于其中/home/foo/bulk/,我想将它们移至/home/foo2/bulk2/ 如果尝试mv /home/foo/bulk/* /home/foo2/bulk2/ ,会出现“参数过多”错误。 Google先生告诉我,批量删除文件的另一种方法是运行find。就像是: find . -name "*.pdf" -maxdepth 1 -print0 | xargs -0 rm 如果我要删除内容,那很好,但是在这种情况下,我想移动文件...如果我输入类似find . -name "*" -maxdepth 1 -print0 | xargs -0 mv /home/foo2/bulk2/bash的内容,则抱怨该文件不是目录。 在此处将批量文件从一个目录移动到另一个目录的最佳命令是什么?
11 linux  bash  shell  find  rename 

2
强制Bash使用Perl RegEx Engine
您可能已经知道,Bash RegEx引擎不支持现代RegEx引擎支持的许多功能(向后引用,环视断言等)。以下是我刚刚创建的一个简单的Bash脚本,试图解释我的最终目标是: #!/bin/bash # Make sure exactly two arguments are passed. if [ $# -lt 2 ] then echo "Usage: match [string] [pattern]" return fi variable=${1} pattern=${2} if [[ ${variable} =~ ${pattern} ]] then echo "true" else echo "false" fi 因此,例如,类似以下命令的内容将返回false: . match.sh "catfish" "(?=catfish)fish" 而在Perl或JavaScript正则表达式测试器中使用时,完全相同的表达式将找到匹配项。 反向引用(例如(expr1)(expr2)[] \ 1 \ …


2
将带有引号和双引号的参数传递给bash脚本
目前,我有: #!/bin/bash screen -p 'ScreenName' -x eval 'stuff '"'"$@"'"'\015' echo eval 'stuff '"'"$@"'"'\015' 但是当我将脚本称为: # script.sh asd "asd" 'asd' 我的论点传递为:asd asd asd 然后我得到输出: eval stuff 'asd asd asd'\015 我除外:asd“ asd”'asd' 如何更改脚本以将所有引号与所有引号一起传递?
11 bash  shell  quoting 

1
从zsh shell运行bash脚本
我在上运行bash脚本时遇到问题zsh。该脚本与下面的脚本相似,应该在bash下运行: #!/bin/bash echo "<235234>" | egrep -oe [0-9]+ 但是,我的外壳程序默认是打开的zsh,因此要运行上述脚本,我需要/bin/bash在zsh提示符下键入,然后运行. script.sh。 但是,这将导致错误,并且我得到: zsh: no matches found: [0-9]+ (我应该得到235234)。 在这一点上,我感到困惑,并有以下问题: 如果我在bash,为什么会报错zsh? 为什么正则egrep表达式失败?正则表达式对我来说似乎正确。 注意:万一重要,我不应该修改脚本(脚本由多个用户共享,我们不应该对其进行更改)。 谢谢
11 bash  zsh 



By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.