我正在尝试使用命令行参数列出任意数量目录中的文件。 我试图通过命令行传递目录路径并显示搜索目录中的文件。像这样的东西。 “目录:PATH1” 文件 文件 文件
“目录:PATH2” 文件 文件
等
所以我使用 $* 传递所有命令行参数,但它只显示第一个列出的目录中的文件。
#!/bin/bash
cd $*
for filename in *
do
echo "Directory: $*"
echo $filename
done
最佳答案
以防万一目录名中有空格,您可能想使用“$@”,检查目录是否存在也无妨:
for dir in "$@" ; do
if [ -d "$dir" ] ; then
echo "Directory: $dir"
ls "$dir" # If all you want is to show the contents, this should do
else
echo "Not a directory: $dir"
fi
done
关于linux - 通过传递命令行参数列出目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14711695/