我正在尝试编写一个小脚本,它接受任意数量的命令行参数,打印出文件(而非目录)的 rwx
权限
我有的是
file=$@
if [ -f $file ] ; then
ls -l $file
fi
然而,这只接受一个命令行参数。感谢您的帮助。
最佳答案
这里演示了 $*
和 $@
之间的一些区别,带引号和不带引号:
#/bin/bash
for i in $*; do
echo "\$*: ..${i}.."
done; echo
for i in "$*"; do
echo "\"\$*\": ..${i}.."
done; echo
for i in $@; do
echo "\$@: ..${i}.."
done; echo
for i in "$@"; do
echo "\"\$@\": ..${i}.."
done; echo
运行它:
user@host$ ./paramtest abc "space here"
$*: ..abc..
$*: ..space..
$*: ..here..
"$*": ..abc space here..
$@: ..abc..
$@: ..space..
$@: ..here..
"$@": ..abc..
"$@": ..space here..
关于bash - 正确使用 $@,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1575183/