简体   繁体   中英

linux counting characters then output number of characters in a string entered by user and it also needs a while loop

I have been trying to get linux to count the characters in a string and then out put them i want the user to be able to enter a string and the amount of characters in a string to be outputed however i have a limited understanding of linux so i really need your help thank you!

so far i have got this:

#!/bin/bash

x="This is a test"

y="${x//[^s]}"

echo "$y"

echo "${#y}"

but that only does it for one type of character and it's not in a while loop that will allow the user to quit if they wan to if you can help it would be appretiated

an example input would be "i like pie" i would want the program to output "the string you have entered has 10 characters

You can use read to get input from user and then use ${#var} to get the length:

#!/bin/bash

read -p "Enter some input text: " input
echo "# of chars in input: ${#input}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM