3

Kindly check and advise my script. I'm trying to get a word (min 2, max 5 chars) as its input.

User will provide the character position and then the script will change it either from lower to uppercase or uppercase to lowercase. Below is incomplete script so far:

input: teSt 3

output: test

#!/bin/bash
clear
while true
do
echo ******TEST*****
read -p 'Enter a word :' word
if [[ ${#word} -le 1 ]] || [[ ${#word} -ge 6 ]]; then
   echo "Invalid input (Min of 2 and max of 5)!."
   exit 1

fi
echo ${word^^*}
done
  • As written, your script will ask for input and capitalize the entire thing. What have you tried to reach the criterion you specify, and how did it not work as intended? – DopeGhoti Aug 7 '17 at 19:56
  • i also need to convert the Uppercase Input into lower then don't really know how to script when user provided the character position then the script will change it either from lower to uppercase or uppercase to lowercase. – trixie101 Aug 7 '17 at 20:08
  • Your description is not clear. If the user types "Nalia" as the input, what should the output be? SImilarly, provide the output for "NALIA", "nalia" and any other examples that might help somebody understand what you are trying to do. And explain what you mean by "character position" - your example script does not contain any explanation for that. – NickD Aug 7 '17 at 20:21
  • example input: naLia 3 3 represent the character needs to be change expected output: nalia L is the third character, form uppercase L into lowercase..vice versa..my script is incomplete sorry – trixie101 Aug 7 '17 at 20:27
  • Another homework problem? Are you trying to say that all lower case in the user input is made upper case, and all upper case is made lower? – Lizardx Aug 7 '17 at 20:43
1
#!/bin/bash

read -p 'Enter a word : ' str id

if [[ ${#str} -le 1 ]] || [[ ${#str} -ge 6 ]]; then
   echo "Invalid input (Min of 2 and max of 5)!." && exit 1

idt=$((id-1))

case ${str:idt:1} in
    [[:lower:]]) 
            r='\U';
        ;;
    [[:upper:]])
            r='\L';
        ;;
esac

echo $str | sed -r "s/./$r&/$id"

$ ./script.sh
Enter a word : test 1
Test
$ ./script.sh
Enter a word : Test 2
TEst
$ ./script.sh
Enter a word : teST 3
tesT
  • thanks..i tried this but it kicks me out w/out result.not sure if there's missing character.#!/bin/bash clear while true do read -p 'Enter a word : ' str id if [[ ${#str} -le 1 ]] || [[ ${#str} -ge 6 ]]; then echo "Invalid input (Min of 2 and max of 5)!." && exit 1 idt=$((id-1)) case ${str:idt:1} in [[:lower:]]) r='\U'; ;; [[:upper:]]) r='\L'; ;; esac echo $str | sed -r "s/./$r&/$id" fi done – trixie101 Aug 7 '17 at 21:17
1

you can use ~ to toggle uppercase and lowercase,

check this:

#!/bin/bash
clear
while true
do
echo ******TEST*****
read -p 'Enter a word :' word pos
if [[ ${#word} -le 1 ]] || [[ ${#word} -ge 6 ]]; then
   printf "\n\tInvalid input (Min of 2 and max of 5)!.\n\n"
fi

for x in  `seq 0 ${#word}`; do
    c=${word:$x:1}
    if [ "$x" -eq "$(($pos-1))" ]; then
        printf "%s" "${c~}"
    else
        printf "%s" "${c}"
    fi
done
printf "\n\n"
done
0

This script only toggles case of the specified letter. It doesn't validate input.

#!/bin/bash

while read -r -p "Enter a word :" word position; do
    sed -r "s/([a-z])|([A-Z])/\U\1\L\2/${position}" <<< "$word"
done    

Testing:

$ ./up_and_low_case.sh 
Enter a word :CAT 2
CaT
Enter a word :rabbit 4
rabBit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.