脚本检测变量作为命令

时间:2015-02-27 12:14:21

标签: linux bash shell

我正在尝试编写一个shell脚本,我接受用户的用户名并检查系统上是否存在该特定用户。如果是,则更改密码否则显示错误。在这个阶段,我只是检查输入的用户名是否存在并相应地显示一条消息。

这是我写的代码:

#!/bin/bash

echo "Enter the username whose password needs to be reset: "
read UNAME
echo "You want to reset password for $uname"

awk -F':' '{print $1}' /etc/passwd > userlist.txt

FLAG = 0

for LINE in $(cat userlist.txt)
do
if [ "$UNAME" == "$LINE" ]
then
    FLAG = 1
    break
else
    FLAG = 0
fi
done

if[ "$FLAG" == 0 ]
then
    echo "User $UNAME does not exist"
else
    echo "Password for $UNAME can be reset"
fi

问题在于,出于某种原因,bash将我的FLAG变量视为命令。

这是输出:

Enter the username whose password needs to be reset: 
abc
You want to reset password for 
./chpwd.sh: line 9: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
./chpwd.sh: line 18: FLAG: command not found
Password for abc can be reset

正如大家们所看到的,它坚持FLAG变量。我无法弄清楚为什么!

2 个答案:

答案 0 :(得分:1)

shell中的变量使用语法var=value(或更好,var="value")进行设置。也就是说,=周围不能有空格。这就是你的剧本抱怨的原因:

FLAG = 0

说:使用参数FLAG=运行命令0

所以你要写的是:

FLAG=0

另外:注意阅读文件可以改进。从:

for LINE in $(cat userlist.txt)
do
...
done

while IFS= read LINE
do
...
done < userlist.txt

其他事项:

if[ "$FLAG" == 0 ]

需要[]周围的空格:

if [ "$FLAG" == 0 ]

并且,实际上,如果要进行整数比较,则必须使用-eq来查看值是否相同:

if [ "$FLAG" -eq 0 ]

答案 1 :(得分:0)

你可以使用简单的脚本来查找特定用户名是否存在

    #!/bin/bash
    Read -p "enter ur username" uid
    Grep ^$uid /etc/passwd
    [ $? != 0 ] && eche -e "/033[31m i can see ur user!" || echo -e "/033[33m u can chang pass "

另外,你可以使用while读取/ etc / passwd 像:

Read -p "enter uid for check " uid
IFS=":"
While read UID REST 
do
[ $UID -eq $uid ] && echo " user found you can change pass " break
Done </etc/passwd