2

I'm trying to create a script that will get a word(min 3 chars, max 10 chars) as its input then it will reverse the position of the characters.

The question is how to combine operations for the if statements to get this script work.

#!/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
  • Your code (max of 5) and your question (max of 10) are at odds. Which do you mean?
    – DopeGhoti
    Aug 7, 2017 at 16:42

1 Answer 1

6

Rather than using an if..elif..elif ladder, just do one test for maximum and minimum length:

#!/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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .