gpt4 book ai didi

linux - bash 脚本中的 "For"循环只运行一次

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:20:45 25 4
gpt4 key购买 nike

脚本目标很简单。

我有很多目录,其中包含一些捕获的流量文件。我想为每个目录运行一个命令。所以我想出了一个脚本。但我不知道为什么脚本只在第一次匹配时运行。

#!/bin/bash
# Collect throughput from a group of directory containing capture files
# Group of directory can be specify by pattern
# Usage: ./collectThroughputList [regex]
# [regex] is the name pattern of the group of directory

for DIR in $( ls -d $1 ); do
if test -d "$DIR"; then
echo Collecting throughputs from directory: "$DIR"
( sh collectThroughput.sh $DIR > $DIR.txt )
fi
done
echo Done\!

我试试:

for DIR in $1; do

for DIR in `ls -d $1`; do

for DIR in $( ls -d "$1" ); do

for DIR in $( ls -d $1 ); do

但结果是一样的。 for 循环只运行一次。

最后我找到了这个并做了一些技巧让它工作。但是,我想知道为什么我的第一个脚本不起作用。

find *Delay50ms* -type d -exec bash -c "cd '{}' && echo enter '{}' && ../collectThroughput.sh ../'{}' > ../'{}'.txt" \;

"*Delay*"是我想用来运行命令的目录模式名称。感谢您指出问题。

最佳答案

既然要查找$1下的所有子目录,就这样使用:

for DIR in $(find $1 -type d)

关于linux - bash 脚本中的 "For"循环只运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15829580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com