Results 1 to 10 of 11
Hello all
I am a begginer in shell scripting and i request from you to help me.
the problem is how to count in a string for example "ABcd98" to ...
- 05-01-2008 #1Just Joined!
- Join Date
- May 2008
- Posts
- 6
Help with strings
Hello all
I am a begginer in shell scripting and i request from you to help me.
the problem is how to count in a string for example "ABcd98" to count small letters, big letters and numbers i have try in this way
can any one help me and excuse me for the bad englishCode:#!/bin/bash s="ABcd98" i=1 sm=0 big=0 nr=0 while [ "$s[$i]" != "\0" ] do if [ "$s[$i]">= "a" && "$s[$i]"<= "z" ] then sm=$(( $sm + 1 )) elif [ "$s[$i]">= "A" && "$s[$i]"<= "Z" ] then big=$(( $big + 1 )) elif [ "$s[$i]">= "0" && "$s[$i]"<= "9" ] then nr=$(( $nr + 1 )) fi i=$(( $i + 1 )) done echo "$sm" "$big" "$nr"
- 05-01-2008 #2
Try using:
- -ne instead of !=
- -ge instead of >=
- -le instead of <=
More info here.
- 05-01-2008 #3Just Joined!
- Join Date
- May 2008
- Posts
- 6
i have try and that but nothing
can any one to creta that script for me pleas.
- 05-01-2008 #4
No. No one here will do your homework for you.
I think it would also help to clean up the format of your if statements. They don't look right to me but I don't have a bash shell to test them on at the moment. Look here.
- 05-01-2008 #5Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
You should really start from the basics, and learn how bash works. This looks like some kind of C stuff converted on some quick hacking.
I am not going to do the script for you, so you can learn how. But I am going to give some pointers on what is wrong, and on how you could possibly achieve this.
In bash, you have mainly two data types: simple ones, and complex ones. The simple ones are all strings ultimately. Bash really makes no distinction between a char, a string or a number. The complex ones are arrays. And, a string, is not an array. That's an assumption you imported from C as well, probably. And that's why this line, if conceptually flawed, and will never do what you intend it to:
Oh, and forget about that other C-ism: '\0'. Bash is not C. Don't just blindly throw your code there and hope that it will work. Dude, do your homework.Code:while [ "$s[$i]" != "\0" ]
As someone pointed above, read the bash man page for valid operators, none of this is valid in bash:
The only valid one would be "==", which is equivalent (well, not exactly, but accurate enough for this purpose) to "-eq" (equal).Code:do if [ "$s[$i]">= "a" && "$s[$i]"<= "z" ] then sm=$(( $sm + 1 )) elif [ "$s[$i]">= "A" && "$s[$i]"<= "Z" ] then big=$(( $big + 1 )) elif [ "$s[$i]">= "0" && "$s[$i]"<= "9" ] then nr=$(( $nr + 1 )) fi
Now, let's go back to reality:
In bash, you can get the length of a given string this way:
You can get a portion of the string by doing:Code:s_length=${#s}
Where pos is an integer starting from 0 (first character of the string) and with a maximum value of $(( ${#s} - 1)) and length is another integer, which should range from 1 to ${#s}. But you probably want it to be 1, so you can extract the characters one by oneCode:s_length=${s:pos:length}
For the rest, you just need to learn to use correct logical operators instead of that C-ism you are using above, and how to properly iterate through the string using while, for or any other loop construct.
Your friend:
Luck.Code:man bash
EDIT: A thing that just crossed my mind: maybe this script was intended for csh, and not bash. I can't confirm it though.
- 05-01-2008 #6Just Joined!
- Join Date
- May 2008
- Posts
- 6
is not for homework and it is not for hacking
this year i have learn C in school and i have try to convert it in csh and i bash just to train shell scripting
- 05-01-2008 #7Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
Maybe, but I preffer not to give you the whole script fixed, just in case. If you really want to learn, read the tips I gave you. You should be able to fix the script yourself with a bit of work.
You misunderstood me. In this context, "to hack into" means to make a quick & dirty change or adaptation of something (in this case, a C program) to adequate it to your needs (a bash script).and it is not for hacking
So, do you want the script to be in bash or csh? They are different shells, and they are not compatible with each other. Bash is the most commonly used shell nowadays, csh has a C-like syntax, and is highly incompatible with almost any other shell out there.this year i have learn C in school and i have try to convert it in csh and i bash just to train shell scripting
- 05-01-2008 #8Just Joined!
- Join Date
- May 2008
- Posts
- 6
ok i have try to creat it in bash and csh and i faild i have some questions
1. how to write "\0" terminal character
2. how to red the string letter by letter and all small characters to sum it.
i now is better to lear for my self but if i dont have examples of basic shell script i never can not be able to do nothing.
luck this script
tell me what you think.Code:#! /bin/bash echo Welcome sp=0 sn=0 i=1 echo "enter a value for n: " read n while [ $i -le $n ] do if [ $(( $i - ($i / 2) * 2 )) -eq 0 ] then sp=$(( $sp + 1 )) else sn=$(( $sn + 1 )) fi i=$(( $i + 1 )) done echo In number "$n" have "$sp" even numbers and "$sn" uneven numbers.
- 05-01-2008 #9Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
I can't help unless you tell me if you are going to use bash or csh. Since you are not being clear about it, I will assume you mean bash because that is what the header on your script tells me.
There's not such a character. As I said in my other post:1. how to write "\0" terminal character
You can use that number to know how many times you need to iterate. There's no need for an end-of-string marker.
Originally Posted by myself
I also told you how to do this. Remember this:2. how to red the string letter by letter and all small characters to sum it.
You can use it to extract a given char. What you need to do is to use "${#s}" to know how long the string it. Then, calculate the index, which will be that value minus 1. Now you have the index.Code:s_char=${s:pos:length}
Then, you can use a for loop to iterate from 0 to i=$(({#s} - 1)), and on each iteration, you will get the next char by doing
Then, it's up to you to parse that char to see if it's a valid character. If it is, you need to increase a counter, if not, you just do nothing, and the next char will be processed.Code:s_char=${s:$i:1}
If you really want to learn, use this:i now is better to lear for my self but if i dont have examples of basic shell script i never can not be able to do nothing.
http://www.tldp.org/LDP/abs/abs-guide.pdf
Regardless of the title, it's suitable for all levels, from beginners to bash gurus. And it's full of examples.
Well, the script seems correct at a first glance. However, remove the space on the first line. It's "#!/bin/bash", and it's intended so other programs can recognize the file type. Most times it will work regardless, but still. For the rest, it's correct, though I don't know it's purpose.Code:#! /bin/bash echo Welcome sp=0 sn=0 i=1 echo "enter a value for n: " read n while [ $i -le $n ] do if [ $(( $i - ($i / 2) * 2 )) -eq 0 ] then sp=$(( $sp + 1 )) else sn=$(( $sn + 1 )) fi i=$(( $i + 1 )) done echo In number "$n" have "$sp" even numbers and "$sn" uneven numbers.
- 05-01-2008 #10Just Joined!
- Join Date
- May 2008
- Posts
- 6
thanks bro for help


Reply With Quote
