Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    6

    Question 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

    Code:
    #!/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"
    can any one help me and excuse me for the bad english

  2. #2
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    Try using:

    • -ne instead of !=
    • -ge instead of >=
    • -le instead of <=


    More info here.

  3. #3
    Just Joined!
    Join Date
    May 2008
    Posts
    6
    i have try and that but nothing
    can any one to creta that script for me pleas.

  4. #4
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    Quote Originally Posted by CyberHacker View Post
    can any one to creta that script for me pleas.
    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.

  5. #5
    Linux 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:

    Code:
    while [ "$s[$i]" != "\0" ]
    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.

    As someone pointed above, read the bash man page for valid operators, none of this is valid in bash:

    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
    The only valid one would be "==", which is equivalent (well, not exactly, but accurate enough for this purpose) to "-eq" (equal).

    Now, let's go back to reality:

    In bash, you can get the length of a given string this way:

    Code:
    s_length=${#s}
    You can get a portion of the string by doing:

    Code:
    s_length=${s:pos:length}
    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 one

    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:

    Code:
    man bash
    Luck.

    EDIT: A thing that just crossed my mind: maybe this script was intended for csh, and not bash. I can't confirm it though.

  6. #6
    Just 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

  7. #7
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by CyberHacker View Post
    is not for homework
    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.

    and it is not for hacking
    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).

    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
    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.

  8. #8
    Just 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

    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.
    tell me what you think.

  9. #9
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by CyberHacker View Post
    ok i have try to creat it in bash and csh and i faild i have some questions
    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.

    1. how to write "\0" terminal character
    There's not such a character. As I said in my other post:

    Quote Originally Posted by myself
    In bash, you can get the length of a given string this way:

    Code:
    s_length=${#s}
    You can use that number to know how many times you need to iterate. There's no need for an end-of-string marker.

    2. how to red the string letter by letter and all small characters to sum it.
    I also told you how to do this. Remember this:

    Code:
    s_char=${s:pos:length}
    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.

    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

    Code:
    s_char=${s:$i:1}
    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.

    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.
    If you really want to learn, use this:

    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.

    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.
    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.

  10. #10
    Just Joined!
    Join Date
    May 2008
    Posts
    6
    thanks bro for help

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...