Counting characters, words, length of the words and total length in a sentence

后端 未结 7 1949
别跟我提以往
别跟我提以往 2021-02-20 15:08

I have to write a script that takes a sentence and prints the word count, character count (excluding the spaces), length of each word and the length. I know that there exist

7条回答
  •  一生所求
    2021-02-20 15:39

    You are very close. In bash you can use # to get the length of your variable.

    Also, if you want to use bash interpreter use bash instead of sh and the first line goes like this -

    #!/bin/bash

    Use this script -

    #!/bin/bash
    
    mystring="one two three test five"
    for token in $mystring
    do
        if [ $token = "one" ]
        then
            echo ${#token}
        elif [ $token = "two" ]
        then
            echo ${#token}
        elif [ $token = "three" ]
        then
            echo ${#token}
        elif [ $token = "test" ]
        then
            echo ${#token}
        elif [ $token = "five" ]
        then
            echo ${#token}
        fi
    done
    

提交回复
热议问题