调用另一个脚本以在目录中的每个文件上执行的脚本

问题描述 投票:0回答:2

有两个目录包含这些文件:

首先是/ usr / local / nagios / etc / hosts

[root@localhost hosts]$  ll
total 12
-rw-rw-r-- 1 apache nagios 1236 Feb  7 10:10 10.80.12.53.cfg
-rw-rw-r-- 1 apache nagios 1064 Feb 27 22:47 10.80.12.62.cfg
-rw-rw-r-- 1 apache nagios 1063 Feb 22 12:02 localhost.cfg

第二个是/ usr / local / nagios / etc / services

[root@localhost services]$  ll
total 20
-rw-rw-r-- 1 apache nagios 2183 Feb 27 22:48 10.80.12.62.cfg
-rw-rw-r-- 1 apache nagios 1339 Feb 13 10:47 Check usage _etc.cfg
-rw-rw-r-- 1 apache nagios 7874 Feb 22 11:59 localhost.cfg

我有一个脚本通过Hosts目录中的文件,并将该文件中的一些行粘贴到Services目录中的文件中。

脚本运行如下:

./Nagios-contacts.是 /US人/local/Nagios/etc/hosts/10.80.12.62.cfg /US人/local/Nagios/etc/services/10.80.12.62.cfg

如何实现另一个脚本调用我的脚本并遍历Hosts目录中的每个文件,并为Service目录中具有相同名称的文件执行其工作?

在我的脚本中,我从Hosts目录中的10.80.12.62.cfg中取出联系人,并将它们附加到Service目录中具有相同名称的文件中。

linux bash scripting
2个回答
1
投票

不要使用ls输出作为for loop的输入,而是使用内置的通配符。看why这不是一个好主意。

for f in /usr/local/nagios/etc/hosts/*.cfg
do
  basef=$(basename "$f")
  ./nagios-contacts.sh "$f" "/usr/local/nagios/etc/services/${basef}"
done

0
投票

听起来你只需要做一些迭代。

echo $(pwd)
for file in $(ls); do ./nagious-contacts.sh $file; done;

因此它将遍历当前目录中的所有文件。

您也可以通过做一些更绝对的事情来修改它。

abspath=$1
for file in $(ls $abspath); do ./nagious-contacts.sh $abspath/$file; done

它将循环遍历set目录中的所有文件,然后将abspath / filename传递给您的脚本。

© www.soinside.com 2019 - 2024. All rights reserved.