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

# 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 :: git how to roll back to a commit 
Shell :: You need to have the Android SDK Build-tools installed on your system 
Shell :: git branch 
Shell :: batch fork bomb 
Shell :: linux bash clean up log files in /var/log 
Shell :: Problem binding to port 80: Could not bind to IPv4 or IPv6. 
Shell :: brave installation ubuntu 
Shell :: cp all files except one 
Shell :: git pull --tags origin main remote: Repository not found. 
Shell :: setting docker as a non root user 
Shell :: generate new ssh key 
Shell :: how to create permanent alias in linux 
Shell :: tensorflow docker hub 
Shell :: push the project to github 
Shell :: install package in docker 
Shell :: sublime text editor download for linux 
Shell :: mac see current path2 
Shell :: boost volume in ubuntu 18.04 
Shell :: change display resolution on terminal 
Shell :: check gcp disk space usage 
Shell :: git submodule push current branch to master 
Shell :: Flatpak in linux 
Shell :: remove local commiits 
Shell :: read from .env file bash 
Shell :: storage setup in termux 
Shell :: Backup Restore and Upgrade Gitlab 
Shell :: flutter pubspec install 
Shell :: bash change case 
Shell :: bash split array into chunks 
Shell :: boot pendrive cmd 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =