#!/bin/bash count=3 while[ $count -gt 0 ]; do read passwd if[ $passwd -eq 'zust']; then echo "Success" exit else echo "Password error" count=$((count-1)) fi done echo "Login failed"
时间: 2024-02-14 21:16:26 浏览: 22
这段代码中有两个错误:
1. `while[ $count -gt 0 ];` 应该改为 `while [ $count -gt 0 ];`,即 while 和 `[` 之间需要有空格。
2. `if[ $passwd -eq 'zust'];` 应该改为 `if [ $passwd = 'zust' ];`,即 if 和 `[` 之间需要有空格,且字符串比较应该使用 `=` 而不是 `-eq`。
以下是修改后的代码:
```
#!/bin/bash
count=3
while [ $count -gt 0 ]; do
read passwd
if [ $passwd = 'zust' ]; then
echo "Success"
exit
else
echo "Password error"
count=$((count-1))
fi
done
echo "Login failed"
```
相关问题
#!/bin/bash int=1234 [$int-lt1234] echo $?输出结果
这段代码有一些语法错误,应该修改为:
```
#!/bin/bash
int=1234
if [ $int -lt 1234 ]; then
echo $?
fi
```
这里使用 if 语句来判断 $int 是否小于 1234,然后输出结果。如果 $int 等于 1234,则不输出结果。
完成程序,实现计算1-100的和。 #!/bin/bash i=1 sum=0 while [ $i -le 100 ] do (1) (2) done echo The sum is $sum
#!/bin/bash
i=1
sum=0
while [ $i -le 100 ]
do
sum=$(($sum+$i)) # (1) 累加器,将当前数值加到总和中
i=$(($i+1)) # (2) 循环变量,加1
done
echo "The sum is $sum" # 输出结果