如何扩展我的 bash 脚本?

如何扩展我的 bash 脚本?

我的剧本

#!/bin/bash

for file in *.xml; do
find . -name "*.xml" -exec grep "sample_freq" "{}" \;
find . -name "*.xml" -exec grep "sensor_sernum" "{}" \;
done

非常适合一个文件夹

      <sample_freq>131072</sample_freq>
        <sample_freq>131072</sample_freq>
        <sample_freq>131072</sample_freq>
        <sample_freq>131072</sample_freq>
        <sample_freq>131072</sample_freq>
        <sensor_sernum>0</sensor_sernum>
        <sensor_sernum>0</sensor_sernum>
        <sensor_sernum>255</sensor_sernum>
        <sensor_sernum>237</sensor_sernum>

但问题是我的目录中有七个文件夹,每个文件夹都有一个 xml 文件。是否可以扩展我的脚本,以便它进入所有七个文件夹并从每个文件夹中获取信息?
我的目录的内容 milenko@milenko-HP-Compaq-6830s:~/Documents/magnetotellurics/MT8$ ls EDI´s meas_2015-06-29_19-18-28 meas_2015-06-29_19-26-

58  meas_2015-06-29_19-47-58  mys.sh   n.sh
meas_2015-06-29_19-03-00  meas_2015-06-29_19-22-58  meas_2015-06-29_19-30-58  meas_2015-06-29_20-39-58

我已经尝试了 choroba 建议我做的事情

#!/bin/bash

for file in ./*; do                                                                                      
    grep 'sample_freq'   "$file"
    grep 'sensor_sernum' "$file"
done

grep: ./EDI´s: Is a directory
grep: ./EDI´s: Is a directory
grep: ./meas_2015-06-29_19-03-00: Is a directory
grep: ./meas_2015-06-29_19-03-00: Is a directory
grep: ./meas_2015-06-29_19-18-28: Is a directory
grep: ./meas_2015-06-29_19-18-28: Is a directory
grep: ./meas_2015-06-29_19-22-58: Is a directory
grep: ./meas_2015-06-29_19-22-58: Is a directory
grep: ./meas_2015-06-29_19-26-58: Is a directory
grep: ./meas_2015-06-29_19-26-58: Is a directory
grep: ./meas_2015-06-29_19-30-58: Is a directory
grep: ./meas_2015-06-29_19-30-58: Is a directory
grep: ./meas_2015-06-29_19-47-58: Is a directory
grep: ./meas_2015-06-29_19-47-58: Is a directory
grep: ./meas_2015-06-29_20-39-58: Is a directory
grep: ./meas_2015-06-29_20-39-58: Is a directory
find . -name "*.xml" -exec grep "sample_freq" "{}" \;
    grep 'sample_freq'   "$file"
    grep 'sensor_sernum' "$file"
    grep 'sample_freq'   "$file"
    grep 'sensor_sernum' "$file"

答案1

for你根本不需要循环。

find . -name "*.xml" -exec grep "sample_freq" "{}" \; -exec grep "sensor_sernum" "{}" \;

答案2

这里不需要find,只需使用 $file 变量,但展开包含路径的通配符:

for file in folder*/*.xml ; do                                                                                       # */ fix SE highlighting bug
    grep 'sample_freq'   "$file"
    grep 'sensor_sernum' "$file"
done

相关内容