这个 shell 脚本不起作用,代码是正确的,但每次都会出错 [重复]

问题描述 投票:0回答:1
echo "Enter Username"
read name
username = 'hagemaru'
if [ $name == $username ]
  then
    echo "Valid Username"
else
    echo "Entered Username is invalid"
    exit 1
fi
exit 0

执行时出错:

l.sh: 3: username: not found
l.sh: 4: [: hagemaru: unexpected operator

输出应该是:

Valid Username
bash shell ubuntu unix syntax
1个回答
0
投票

您需要删除

username
=
之间的空格,否则bash 会将
username
解释为不存在的命令(如错误消息中所示)。这样做,您还需要删除
username=
'hagemaru'
之间的空格,因为 bash 会将
username= 'hagemaru'
解释为运行带有参数
username=
的命令
'hagemaru'
,这是无效的,因为没有
username=
bash 中的命令。

echo "Enter Username"
read name
username='hagemaru'
if [ $name == $username ]
  then
    echo "Valid Username"
else
    echo "Entered Username is invalid"
    exit 1
fi
exit 0
© www.soinside.com 2019 - 2024. All rights reserved.