如何运行find找到的所有脚本

时间:2018-05-01 19:44:56

标签: linux bash find

我试图找到为websphere创建的所有init脚本。 我知道所有的脚本都以-init结尾,所以代码的第一部分是:

find /etc/rc.d/init.d -name "*-init"

此外,我需要在特定路径上运行的所有脚本,因此第二部分将是

| grep -i "/opt/ibm"

最后,我需要最后一部分的帮助。我找到了使用stop参数运行它们所需的脚本。

find /etc/rc.d/init.d -name "*-init" | grep -i "/opt/ibm" | <<run script found with stop argument>>

如何运行find?

找到的命令

2 个答案:

答案 0 :(得分:0)

使用循环,以便我们在执行时更加小心:

#!/bin/bash

shopt -s globstar
for file in /etc/rc.d/init.d/**/*-init; do     # grab all -init scripts
  script=$(readlink -f "$file")                # grab the actual file in case of a symlink
  [[ -f $script ]]          || continue        # skip if not a regular file
  [[ $file = */opt/ibm/* ]] || continue        # not "/opt/ibm/", skip
  printf '%s\n' "Executing script '$script'"
  "$script" stop; exit_code=$?
  printf '%s\n' "Script '$script' finished with exit_code $exit_code"
done

答案 1 :(得分:-1)

如果省略&#39;发现&#39;并直接使用grep你可以这样做:

grep -i "/opt/ibm" /etc/rc.d/init.d/* | sed 's/:.*/ stop/g' | sort -u | bash
  • 它直接使用grep,它将文件名添加到输出:filename:matched line
  • 由于您只需要文件名而不是匹配项,因此请使用sed替换&#39;:&#39;以及&#39;的其余部分。停止&#39; (见停止前的空格)
  • 使用sort -u(确保只执行一次脚本)
  • 将结果传递给shell