Shell script to read a character (upper or lower), digit, special symbol and display message according to the character entered

in Categories Decision Making last updated April 5, 2008

This script uses sed command to find out all digits or upper characters. -z string (conditional expression) will return true if the length of string is zero.

Script logic

Read one character
[1] Use sed command to determine input character is digit or not (sed -e ‘s/[A-Z]//g’))

[2] Attempt to match regex [0-9] i.e. all digit against the pattern space

[3] If successful, replace that portion matched with replacement // i.e. do nothing.
If digits are in input, it will replace all those digit, and if input is only digit, nothing is left i.e. string is zero and that is what tasted with -z conditional expression.


Share this on:

7 comment

  1. Here’s a way to do the same thing in pure bash (no sed) and in half the lines. More info here: https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html and here: https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html.

  2. HI!
    Here i want replace /leftarrow with leftarrow in my html file. Below one is my code. But its not working properly. Please help !!!

    #!/bin/bash
    # ALL HTML FILES
    FILES=”*.html”
    # for loop read each file
    for f in $FILES
    do
    INF=”$f”
    OUTF=”$f.out.tmp”
    sed ‘s/[/leftarrow]/leftarrow/g’ $INF > $OUTF
    /bin/cp $OUTF $INF
    /bin/rm -f $OUTF
    done
    Thanks in advance

    1. sed ’s/[/leftarrow]/leftarrow/g’ $INF > $OUTF —- in this line use as below

      sed ’s/ \ /leftarrow/leftarrow/g’ $INF > $OUTF— “\” is the escape sequence

    2. FYI, you can use “sed -i” so that you won’t have to copy the input to output and remove the file…

  3. hi
    how to detect the occurance of any character and further save it on onother file using shell script.
    say i have smb.conf file and i have to detect the occurance of “[” and “]” and save all words enclosed in this brackets..
    I hope u understand my problem thanks…

    Have a question? Post it on our forum!