我想对目录中的每个文件执行一个命令,并将输出存储在一个新目录中,使其具有与输入相同的文件名。我运行的命令是一个 .pl 脚本,格式如下:
test.pl 输入文件输出文件
例如,我有一个名为 input 的目录,其中包含以下文件:种皮.txt测试文件.txt
我运行一个 for 循环,对这两个文件执行命令:
for file in /Users/test/Desktop/input
do
test.pl $file /Users/test/Desktop/output/$file
done
但是,以这种方式提供输出路径是行不通的。我不断收到错误消息,没有这样的文件或目录。
file
获取值/Users/test/Desktop/input
,所以test.pl
接收到/Users/test/Desktop/output//Users/test/Desktop/input
作为最后一个参数。你会想要使用 glob像 /Users/test/Desktop/input/*
然后使用 basename
剥离目录:
for file in /Users/test/Desktop/input/*
do
test.pl "$file" "/Users/test/Desktop/output/$(basename "$file")"
done
我是一名优秀的程序员,十分优秀!