正确使用$ @ [英] Using $@ properly

查看:39
本文介绍了正确使用$ @的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个微型脚本,该脚本可以接受任意数量的命令行参数,这些参数可以打印出文件(而非目录)的 rwx 权限

I am trying to write a tiny script that accepts any number of command line arguments that prints out the rwx permissions for a file (not directory)

我拥有的是

file=$@    
if [ -f $file ] ; then    
ls -l $file    
fi

但是,它仅接受一个命令行参数.感谢您的帮助.

This accepts only one command line argument however. Thanks for any help.

推荐答案

此处演示了 $ * $ @ 之间的一些区别,并带有和没有引号:

Here is a demonstration of the some of the differences between $* and $@, with and without quotes:

#/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..

这篇关于正确使用$ @的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆