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

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.

#!/bin/bash
# Shell script to read a character (upper or lower), digit, special symbol and
# display message according to the character entered
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
char=""
echo -n "Enter a one character : "
read char
# use sed command to determine input character is digit or not
# -e : start script
# s/[0-9]// : Attempt to match regex [0-9] i.e. all digit against the pattern space (echo $char).
# If  successful,  replace  that  portion  matched  with replacement i.e. // do nothing. 
# g : Global replacement
# so if digit 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
if [ -z $(echo $char | sed -e 's/[0-9]//g') ]
then
	echo "$char is Number/digit"
elif [ -z $(echo $char | sed  -e 's/[A-Z]//g') ] # find out if character is upper
then
        echo "$char is UPPER character"
 
elif [ -z $(echo $char | sed -e 's/[a-z]//g') ] # find out if character is lower
then
        echo "$char is lower character"
else
	echo "$char is Special symbol" # else it is special character
fi
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
7 comments… add one
  • Soumya Aug 31, 2017 @ 11:05

    Please can u make only one command to check number and alphabet

  • know-it-all Mar 10, 2012 @ 5:06

    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.

    #!/bin/bash
    
    read -p "Enter a one character : " char
    case $char in
        [[:digit:]]) echo "$char is Number/digit"    ;;
        [[:upper:]]) echo "$char is UPPER character" ;;
        [[:lower:]]) echo "$char is lower character" ;;
                  *) echo "$char is Special symbol"  ;;
    esac
    
  • venkat Jul 29, 2010 @ 10:37

    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

    • phani Aug 26, 2010 @ 14:02

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

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

    • Anonymous Dec 21, 2010 @ 21:34

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

  • hammy Feb 1, 2010 @ 3:57

    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…

  • UNICS Dec 21, 2008 @ 16:41

    Hi,

    how can you detect if the character is a NULL ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.