Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

bash case statement

case "$1" in
        start)
            start ;;
        stop)
            stop ;;
        restart)
            stop; start ;;
        *)
            echo $"Usage: $0 {start|stop|restart}"
            exit 1
esac
Comment

bash case statement

# Basic syntax:
case "STRING" in
	patterns)
		echo "commands to perform if patterns matched STRING"  
        ;;
	to)
        echo "commands to perform if to matched STRING" 
        ;;
    search)
        echo "commands to perform if search matched STRING" 
        ;;
    *)
        echo "anything that wasn't matched by the previous patterns"
        exit 1
        ;;
esac

# Example usage:
# If you want to write a bash script where you ask the user how to proceed, you
#	could do something like this:
read -r -p $'
Do you want to perform this action? (y/n/q): ' RESPONSE
	case $RESPONSE in
    	[Yy]*)
        	echo "The user chose y"
        	;;
        [Nn]*)
        	echo "The user chose n"
            ;;
        [Qq]*)
        	echo "The user chose q, script aborted"
            exit 0
            ;;
        *)
        	echo "The user chose something other than y, n, or q"
            ;;
	esac
# Note, the patterns like [Yy]* will match the string in $RESPONSE if that
#	string starts with Y or y followed by any number of other characters. So
#	it would also match Yes, yes, yertyujiwjwijdwi, etc
# Note, if you want this message to keep repeating until the user types q, put
#	the above in a while loop that never ends (e.g. while true; do ... done)
Comment

bash switch case

case $1 in
start)
    #Start logic
    ;;
stop)
    # Stop logic
    ;;
*)
	# Default logic
    echo $"Usage: $0 {start|stop}"
    exit 1
    ;;
esac
Comment

bash case statement

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

bash case

#!/bin/bash
$web="example.com"
case $web in
 forest.com) echo "It's forest.";;
 animal|human) echo "Maybe animal or maybe human";;
 example.com) echo "This is a site!";;

*) echo $web;;
esac
Comment

PREVIOUS NEXT
Code Example
Shell :: unzip ubuntu 
Shell :: openssh client ubuntu 
Shell :: linux remove folder with content 
Shell :: lower brightness of a ubunut pc 
Shell :: body-parser use 
Shell :: firebase deploy with token 
Shell :: how to connect to heroku app using git remotely 
Shell :: how to enable remote desktop with cmd 
Shell :: linux remove directory and contents 
Shell :: apt lock 
Shell :: angular add modulee 
Shell :: git: delete branch in local and on remote 
Shell :: git view changes in commit 
Shell :: clone branch in git 
Shell :: how to delete branch github 
Shell :: ubuntu remove all folders but keep some 
Shell :: pip install --upgrade 
Shell :: how to go back to the last directory in linux 
Shell :: add line at beginning of file unix 
Shell :: split bash string 
Shell :: how to check directory size in linux 
Shell :: angular add universal 
Shell :: remove letter in string bash 
Shell :: install appx 
Shell :: install brew on mac m1 
Shell :: how to change bash prompt color 
Shell :: how to verify my ssh key password 
Shell :: install windows app powershell 
Shell :: poetry python 
Shell :: git remove submodules 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =