🖥️case

➡️This is a command-line reference manual for commands and command combinations that you don’t use often enough to remember it. This cheatsheet explains the case command with important options and switches using examples.

▁ ▂ ▃ ▄ ꧁ 🔴☠ COMMANDLINE-KUNGFU WITH CHEATSHEETS ☠🔴꧂▅ ▃ ▂ ▁

#   ██████╗ █████╗ ███████╗███████╗
#  ██╔════╝██╔══██╗██╔════╝██╔════╝
#  ██║     ███████║███████╗█████╗  
#  ██║     ██╔══██║╚════██║██╔══╝  
#  ╚██████╗██║  ██║███████║███████╗
#   ╚═════╝╚═╝  ╚═╝╚══════╝╚══════╝

syntax of bash case statement.

case expression in
    pattern1 )
        statements ;;
    pattern2 )
        statements ;;
    ...
esac

#!/bin/bash
echo "enter the value"
read a
case $a in
1) echo ” 1 ” ;&
2) echo ” 2 ” ;&
3) echo ” 3 ” ;;&
4) echo ” 4 ” ;;
5) echo ” 5 ” ;&
esac

# Example 01
#------------#

#!/bin/bash
# signal.sh
if [ $# -lt 2 ]
then
        echo "Usage : $0 Signalnumber PID"
        exit
fi

case "$1" in

1)  echo "Sending SIGHUP signal"
    kill -SIGHUP $2
    ;;
2)  echo  "Sending SIGINT signal"
    kill -SIGINT $2
    ;;
3)  echo  "Sending SIGQUIT signal"
    kill -SIGQUIT $2
    ;;
9) echo  "Sending SIGKILL signal"
   kill -SIGKILL $2
   ;;
*) echo "Signal number $1 is not processed"
   ;;
esac

# Example 02 - Pattern Match in a File
#--------------------------------------#

#!/bin/bash
# fileop.sh
# Check 3 arguments are given #
if [ $# -lt 3 ]
then
        echo "Usage : $0 option pattern filename"
        exit
fi

# Check the given file is exist #
if [ ! -f $3 ]
then
        echo "Filename given \"$3\" doesn't exist"
        exit
fi

case "$1" in

# Count number of lines matches
-i) echo "Number of lines matches with the pattern $2 :"
    grep -c -i $2 $3
    ;;
# Count number of words matches
-c) echo "Number of words matches with the pattern $2 :"
    grep -o -i $2 $3 | wc -l
    ;;
# print all the matched lines
-p) echo "Lines matches with the pattern $2 :"
    grep -i $2 $3
    ;;
# Delete all the lines matches with the pattern
-d) echo "After deleting the lines matches with the pattern $2 :"
    sed -n "/$2/!p" $3
    ;;
*) echo "Invalid option"
   ;;
esac

	# Execution of the above script is shown below.
	$ cat text
	Vim is a text editor released by Bram Moolenaar in 1991 for the Amiga computer.
	The name "Vim" is an acronym for "Vi IMproved" because Vim was created as an extended version of the vi editor, with many additional features designed to be helpful in editing program source code.
	Although Vim was originally released for the Amiga, Vim has since been developed to be cross-platform, supporting many other platforms.
	It is the most popular editor amongst Linux Journal readers.
	Bash case regex output. After deleting the lines matches with the pattern Vim:
	
	$ ./fileop.sh  -d Vim text
	It is the most popular editor amongst Linux Journal readers.

# Example 03 - Find File type from the Extension
#------------------------------------------------#

#!/bin/bash
# filetype.sh
for filename in $(ls)
do
	# Take extension available in a filename
        ext=${filename##*\.}
        case "$ext" in
        c) echo "$filename : C source file"
           ;;
        o) echo "$filename : Object file"
           ;;
        sh) echo "$filename : Shell script"
            ;;
        txt) echo "$filename : Text file"
             ;;
        *) echo " $filename : Not processed"
           ;;
esac
done

	$ ./filetype.sh
	a.c : C source file
	b.c : C source file
	c1.txt : Text file
	fileop.sh : Shell script
	obj.o : Object file
	text : Not processed
	t.o : Object file

# Example 04 - Prompt User with Yes or No
#-----------------------------------------#

#!/bin/bash
# yorno.sh
echo -n "Do you agree with this? [yes or no]: "
read yno
case $yno in

        [yY] | [yY][Ee][Ss] )
                echo "Agreed"
                ;;

        [nN] | [n|N][O|o] )
                echo "Not agreed, you can't proceed the installation";
                exit 1
                ;;
        *) echo "Invalid input"
            ;;
esac

	$ ./yorno.sh
	Do you agree with this? [yes or no]: YES
	Agreed

# Example 05 - Startup script
#-----------------------------------------#

#!/bin/bash
# startpcapp.sh
case "$1" in
'start')
echo "Starting application"
/usr/bin/startpc
;;
'stop')
echo "Stopping application"
/usr/bin/stoppc
;;
'restart')
echo "Usage: $0 [start|stop]"
;;
esac

	$ ./startpcapp start
	Starting application
	/usr/bin/startpc started

#!/bin/sh
# talk.sh from https://www.shellscript.sh/case.html
echo "Please talk to me ..."
while :
do
  read INPUT_STRING
  case $INPUT_STRING in
	hello)
		echo "Hello yourself!"
		;;
	bye)
		echo "See you again!"
		break
		;;
	*)
		echo "Sorry, I don't understand"
		;;
  esac
done
echo 
echo "That's all folks!"

	Okay, so it's not the best conversationalist in the world; it's only an example!
	Try running it and check how it works...
	
	$ ./talk.sh
	Please talk to me ...
	hello
	Hello yourself!
	What do you think of politics?
	Sorry, I don't understand
	bye
	See you again!
	
	That's all folks!
	$
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

  █║▌│║█║▌★ KALI ★ PARROT ★ DEBIAN 🔴 PENTESTING ★ HACKING ★ █║▌│║█║▌

              ██╗ ██╗ ██████╗  ██████╗ ██╗  ██╗███████╗██████╗
             ████████╗██╔══██╗██╔═══██╗╚██╗██╔╝██╔════╝██╔══██╗
             ╚██╔═██╔╝██║  ██║██║   ██║ ╚███╔╝ █████╗  ██║  ██║
             ████████╗██║  ██║██║   ██║ ██╔██╗ ██╔══╝  ██║  ██║
             ╚██╔═██╔╝██████╔╝╚██████╔╝██╔╝ ██╗███████╗██████╔╝
              ╚═╝ ╚═╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═════╝

               █║▌│║█║▌ WITH COMMANDLINE-KUNGFU POWER █║▌│║█║▌

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░