#!/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 else break
The syntax works, but you are missing things because you don't indent your code. The break is done when
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)