我正在尝试创建一个脚本,它将获取一个单词(最少 3 个字符,最多 10 个字符)作为其输入,然后它将反转字符的位置。
问题是如何组合 if 语句的操作以使该脚本正常工作。
#!/bin/bash
clear
ok=3
echo *****REVERSE*****
while [ $ok = 3 ]
do
echo "Enter a character: "
read id
if [ ${#id} -eq 3 ]
then
echo $id | rev
elif [ ${#id} -eq 4 ]
then
echo $id | rev
elif [ ${#id} -eq 5 ]
then
echo $id | rev
else
echo Minimum character is 3 and Maximum is 5!Logging out..
ok=4
fi
done
答案1
无需使用if..elif..elif
梯子,只需测试最大和最小长度即可:
#!/bin/bash
read -p 'Enter a word :' word
if [[ ${#word} -le 2 ]] || [[ ${#word} -ge 11 ]]; then
echo "Word is too short or long."
else
rev < <(echo ${word})
fi