gpt4 book ai didi

linux - shell 脚本中的条件不起作用

转载 作者:太空狗 更新时间:2023-10-29 12:16:30 27 4
gpt4 key购买 nike

我的 shell 程序是:

  testname=methun
echo "Please enter your name:"
read username
if [ "$username" == "$testname"]; then
age=20
echo " you are $age years old."
else
echo "How old are you?"
read Age
if [ "$Age" -le 20]; then
echo "you are too young."
else
if["$Age" -ge 100]; then
echo " You are old."
else
echo "you are young."
fi fi fi

现在,当我运行我的程序时,它能够接受用户输入并显示错误。错误如下:

./filename line linenumber:sysntax error near unexpected token 'then'
./filename line linenumber: 'if["$username" -eq "$testname"]; then'

最佳答案

您缺少一些空格:

#!/bin/bash
testname=methun
echo "Please enter your name:"
read username

if [ "$username" == "$testname" ]; then
age=20
echo " you are $age years old."
else
echo "How old are you?"
read Age
if [ "$Age" -le 20 ]; then
echo "you are too young."
else
if [ "$Age" -ge 100 ]; then
echo " You are old."
else
echo "you are young."
fi
fi
fi

关于linux - shell 脚本中的条件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22892878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com