Sunday, 15 June 2014

shell - add two letters from a variable in bash script -


To make the input (word) of my code and to output the sum of all the letters in the input, the number of letters Is equal to - A = 1b = 2c = 3 etc., here is my incomplete code here $ $ * is where the input should be taken and the output should be calculated (w + o + r + d = ??)

Here is a solution that uses an associative array So that their gradual values ​​map (A English) Note that associative arrays require bash 4.0 or higher .

  #! Announce variables declare / usr / bin / env bash # declare -i i declare integer variables- one letter value denotes a # 1 array, for which bash 4+ declaration Let's make a mapping between letter # a regular (string variable) # letter and their values; # sequence brace expression {a..z} 'abc ... z' increases for # $ ((++ i) I = 0 for the letter in pay scale $ i increments and new value returns {A..z}; Make a letter [$ letters] = $ ((++ i)) For a word # Enter the read -p 'word:' word # loop on all characters. Add word and inline letter values ​​to sum = 0 ((i = 0; i  

No comments:

Post a Comment