shell script to find the sum of numbers associated with each character in a string
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
shell script to find the sum of numbers associated with each character in a string
i have just started dealing with shell script.... i want to make a script that adds each letter in a name and gives a total...i ve tried with some commands but i feel that i am falling short of commands, can anyone suggest an appropriate command which can help me with this....
thnx in advance...
It uses a for loop to iterate through the alphabet, if the letter is in the word it counts how many times. Then it calculates the sum and prints it.
Code:
#!/bin/bash
letternum=0
ALPHABET=(Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz)
read -p "Enter your name: " name
for i in ${ALPHABET[@]}; do
((++letternum))
if [ "$(echo $name | grep [$i])" ]; then
occurences=$(echo $name | tr -cd [$i] | wc -c)
((sum+=letternum*occurences))
fi
done
echo "$name: sum: $sum"
i tried all those one by one, i think that they need minute corrections as i get following outputs.....btw "namesum" is the name by which i ve saved the file
@crts ontrying the method described by you i get
Code:
Syntax error: "(" unexpected
@Cedrik ontrying the method described by you i get
Code:
Syntax error: EOF in backquote substitution
@kabamaru ontrying the method described by you i get
Code:
namesum: 3: Bb: not found
Enter your name: aagam
namesum: 13: Bad substitution
@ catkin, no i am not able to do that..
i would be glad to knw all these methods....
and can anyone give the syntax to find the character in the string by specifying its postion...i tried to do dat with match and index commands but i didnt get desired results...
i tried all those one by one, i think that they need minute corrections as i get following outputs.....btw "namesum" is the name by which i ve saved the file
@crts ontrying the method described by you i get
Code:
Syntax error: "(" unexpected
Not sure why you get that error. I do not get an error when I run it and I checked that there is no typo in my posted solution. Did you type or copy/paste it when you tried it?
@crts, i copy paste-d it..
@grail i tried dat and it gives
Code:
aagam@infinity:~$ sh namesum
Enter name: aagam
namesum: 5: Syntax error: Bad for loop variable
@kabamaru, it still gives the same error...... is there a possibility of terminal being faulty?
and plz tell me the command used to extract a letter from the string by specifying its position...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.