【发布时间】:2019-04-18 19:30:41
【问题描述】:
我有以下脚本 应该像这样工作:
bash code.sh -u user -g
它应该返回输入用户的组,而是返回正在执行脚本的用户的组。
#!/bin/bash
while getopts u:fgd options; do
case $options in
u)
user = $OPTARG 2>/dev/null;
if grep -q $OPTARG /etc/passwd == 1 2>/dev/null ;
then
echo "user exists";
else
echo "user doesnt exist"
fi
;;
g) if $user;
then
echo $user;
echo "the groups of the user are: ";
groups $user ;
else
echo "user missing"
fi
;;
f) echo "output f" ;;
d) echo "output d" ;;
esac
done
基本上在 g) 中,如果有一个输入的用户,该用户没有被捕获作为参数,它应该可以工作
【问题讨论】:
-
当
2> /dev/null使错误消失时,这是因为它隐藏了错误消息,而不是因为它使代码工作 -
通过ShellCheck 运行您的代码。一开始就有几个语法错误。