简体   繁体   English

linux计算字符,然后输出用户输入的字符串中的字符数,并且还需要一个while循环

[英]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! 我一直在尝试让linux对字符串中的字符进行计数,然后把它们放出来,我希望用户能够输入字符串以及要输出的字符串中的字符数量,但是我对linux的了解有限,因此我真的需要您的帮助,谢谢!

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 但这仅对一种类型的字符起作用,并且不是在while循环中,如果您愿意的话,它不会允许用户退出,如果您可以帮助的话

an example input would be "i like pie" i would want the program to output "the string you have entered has 10 characters 输入示例为“ i like pie”,我希望程序输出“您输入的字符串有10个字符

You can use read to get input from user and then use ${#var} to get the length: 您可以使用read来获取用户的输入,然后使用${#var}来获取长度:

#!/bin/bash

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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