bash脚本来运行每个文件夹

时间:2014-06-17 11:37:01

标签: linux bash shell awk sed

我需要在每个文件夹中运行一个bash脚本,但我怎么能做到最好?

这个需要在每个文件夹中运行的脚本 此外,我需要跟踪文件所在的文件夹$ Title

#!/bin/bash

rm -f output2.txt
rm -f output3.txt
ls -td $PWD/test/*.pdf |head -5 >> output2.txt
ls -t test/ | sed -e 's/\.[^.]*$//' | head -5 >> output3.txt
output2=output2.txt
output3=output3.txt


download_one=$(awk 'FNR==1' $output2)
download_two=$(awk 'FNR==2' $output2)
download_three=$(awk 'FNR==3' $output2)
download_four=$(awk 'FNR==4' $output2)
download_five=$(awk 'FNR==5' $output2)

download_one_title=$(awk 'FNR==1' $output3)
download_two_title=$(awk 'FNR==2' $output3)
download_three_title=$(awk 'FNR==3' $output3)
download_four_title=$(awk 'FNR==4' $output3)
download_five_title=$(awk 'FNR==5' $output3)

rm -f output2.txt
rm -f output3.txt

Title=
Title2=$Title

echo $Title,$Title2,$download_one_title,$download_one,$download_two_title,$download_two,$download_three_title,$download_three,$download_four_title,$download_four,$download_five_title,$download_five >> test.csv

第二 错误

    /home/intranet/script: line 5: /home/intranet/test/test1/script: No such file or directory
/home/intranet/script: line 5: /home/intranet/test/test1/script: No such file or directory
/home/intranet/script: line 5: /home/intranet/test/test1/script: No such file or directory
ls: cannot access /home/intranet/test/test1/test/*/*.pdf: No such file or directory
ls: cannot access test/: No such file or directory
/home/intranet/script: line 5: /home/intranet/test/test2/script: No such file or directory
/home/intranet/script: line 5: /home/intranet/test/test2/script: No such file or directory
/home/intranet/script: line 5: /home/intranet/test/test2/script: No such file or directory
ls: cannot access /home/intranet/test/test2/test/*/*.pdf: No such file or directory
ls: cannot access test/: No such file or directory
/home/intranet/script: line 5: /home/intranet/test/test3/script: No such file or directory
/home/intranet/script: line 5: /home/intranet/test/test3/script: No such file or directory
/home/intranet/script: line 5: /home/intranet/test/test3/script: No such file or directory
ls: cannot access /home/intranet/test/test3/test/*/*.pdf: No such file or directory
ls: cannot access test/: No such file or directory

第二个代码

    #!/bin/bash

curdir=$(pwd)
for folder in /home/intranet/test/*; do
    [ -d $folder ] && cd "$folder" && $curdir/script
done
cd $curdir

rm -f output2.txt
rm -f output3.txt
ls -td $PWD/test/*/*.pdf |head -5 >> output2.txt
ls -t test/ | sed -e 's/\.[^.]*$//' | head -5 >> output3.txt
output2=output2.txt
output3=output3.txt


download_one=$(awk 'FNR==1' $output2)
download_two=$(awk 'FNR==2' $output2)
download_three=$(awk 'FNR==3' $output2)
download_four=$(awk 'FNR==4' $output2)
download_five=$(awk 'FNR==5' $output2)

download_one_title=$(awk 'FNR==1' $output3)
download_two_title=$(awk 'FNR==2' $output3)
download_three_title=$(awk 'FNR==3' $output3)
download_four_title=$(awk 'FNR==4' $output3)
download_five_title=$(awk 'FNR==5' $output3)

rm -f output2.txt
rm -f output3.txt

Title=lol
Title2=$Title

echo $Title,$Title2,$download_one_title,$download_one,$download_two_title,$download_two,$download_three_title,$download_three,$download_four_title,$download_four,$download_five_title,$download_five >> test.csv

1 个答案:

答案 0 :(得分:2)

find /path/to/Files -type d -exec myscript.sh {} \;

找到正常的工作,你可以指定更多选项来过滤文件夹,如时间或访问,...