【发布时间】:2023-03-23 01:40:01
【问题描述】:
bash 脚本的新手,所以只是想知道我是否完全正确地编写了这段代码。我试图搜索 /etc/passwd 然后 grep 并打印用户。
usage ()
{
echo "usage: ./file.sk user"
}
# test if we have two arguments on the command line
if [ $# != 1 ]
then
usage
exit
fi
if [[ $# < 0 ]];then
usage
exit
fi
# Search for user
fullname=`grep $1 /etc/passwd | cut -f 5 -d :`
firstname=`grep $1 /etc/passwd | cut -f 5 -d : | cut -f 1 -d " "`
#check if there. if name is founf: print msg and line entry
not sure as how to this or if im doing this right...
我这样做对吗?
【问题讨论】:
-
你的第二个
$#测试是多余的,如果代码到达它$#必须是1。 -
看看
finger命令。 -
未引用的
<是重定向,而不是数字比较。正如其他人指出的那样,无论如何条件都是多余的,但是这个错误可能很难调试,所以我单独指出。
标签: bash shell command-line grep