稿件逻辑错误

问题描述 投票:-1回答:2
#!/bin/bash
whilenum=1
while [ $whilenum -lt 5 ]
do 

if  [ $whilenum == 1 ]
then
function funct1 {
echo The value of whilenum is $whilenum
}
funct1 
else break
fi

if (( $whilenum == 2 | $whilenum == 3 ))
then
for animal in dog bird turtle
do
echo $animal
done
fi  

if [ $whilenum == 4 ]
then
echo Enter your name
read username
fi

if [ ${#username} >  8 ]
then
echo User name is long
else
echo User name is short

fi

whilenum=$(( $whilenum+1 ))
done 

当我调整数字来测试数字时,什么都没有发生,我做了while循环递增,所以它可以终止,但不知道它是否工作。$whilenum == ...

the problem is that you are breaking if whilenum is not equal to 1. Therefore, your script will not continue. I think you should remove the
linux bash for-loop while-loop do-while
2个回答
1
投票

else breakThe syntax works, but you are missing things because you don't indent your code. The break is done when

and the

0
投票

looks for a string whilenum -eq 1, use ${#username} or if [ $whilenum == 1 ]. "1"You did try to write code and learn, so I hope you will study the code beneath.Your code can be improved with a -eq and a if (( $whilenum == 1 )) statement:

for-loop case#!binbash whilenum=1 while [ $whilenum -lt 5 ] do if [ $whilenum == 1 ] then function funct1 { echo The value of whilenum is $whilenum } funct1 else break fi if ( $whilenum == 2)

© www.soinside.com 2019 - 2024. All rights reserved.