Get the App
SLTechnology News&Howtos  ›  Servers  › 

Carding the basic knowledge of shell script & lt; V & gt;: process Control case

Shulou Source: shulou.com Published: 2022-06-02 17:32:08 10月03日 Update

Case uses format

Case value in

Mode 1)

Instruction 1

Instruction 2

...

Mode 2)

Instruction 1

Instruction 2

...

Esca

Echo 'enter a number between 1 and 4:'

Echo 'the number you entered is:'

Read aNum

Case $aNum in

1) echo 'you chose 1'

2) echo 'you chose 2'

3) echo 'you chose 3'

4) echo 'you chose 4'

*) echo 'you did not enter a number between 1 and 4'

Esac

Example 1

#! / bin/bash

# case...esac

Echo "What is your preferred scripting language?"

Echo "1) bash"

Echo "2) prel"

Echo "3) python"

Echo "4) ruby"

Echo "5) I do not know!"

Read lang

Case $lang in

1) echo "you selected bash"

2) echo "you selected prel"

3)

Echo "you selected python"

4)

Echo "you selected ruby"

5)

Echo "I do not know!"

Esac

Execution result

[root@localhost shell] # sh case.sh

What is your preferred scripting language?

1) bash

2) prel

3) python

4) ruby

5) I do not know!

four

You selected ruby

[root@localhost shell] # sh case.sh

What is your preferred scripting language?

1) bash

2) prel

3) python

4) ruby

5) I do not know!

five

I do not know!

[root@localhost shell] #

Example 2

#! / bin/bash

Echo-n "Do you agree whith this? [yes or no]:"

Read yn

Case $yn in

[Yy] | [Yy] [Ee] [Ss])

Echo "Agreed."

[Nn] | [Nn] [Oo])

Echo "Not Agreed."

Exit 1

*)

Echo "Invalid input."

Esac

Running result:

[root@localhost shell] # sh case2.sh

Do you agree whith this? [yes or no]: 1

Invalid input.

[root@localhost shell] # sh case2.sh

Do you agree whith this? [yes or no]: n

Not Agreed.

[root@localhost shell] # sh case2.sh

Do you agree whith this? [yes or no]: Yes

Agreed.

[root@localhost shell] #

Example 3

#! / bin/bash

Case $1 in

Sql) echo "Runing mysql backup using mysqldump tool..."

Sync) echo "Runing backup using rsyuc tool..."

Git) echo "Runing backup using gistore tool..."

Tar) echo "Runing tape backup using tar tool..."

*)

Echo "Backup shell script utility"

Echo "Usage: $0 {sql | sync | git | tar}"

Echo "sql: Run mysql backup utility."

Echo "sync: Run web backup utility."

Echo "git: Run gistore backup utility."

Echo "tar: Run tape backup utility."

Esac

Running result:

[root@localhost shell] # sh case3.sh

Backup shell script utility

Usage: case3.sh {sql | sync | git | tar}

Sql: Run mysql backup utility.

Sync: Run web backup utility.

Git: Run gistore backup utility.

Tar: Run tape backup utility.

[root@localhost shell] # sh case3.sh sql

Runing mysql backup using mysqldump tool...

[root@localhost shell] #

Example 4

#! / bin/bash

# get the one with the highest disk utilization

Max_usage=$ (df-Ph | awk'{print $5}'| grep% | grep-v "Use" | sort-n | tail-1 | cut-d "%"-F1)

Max_dir=df-Ph | awk'{print $5 print 6}'| grep-v Use | sort-n | tail-1 | awk'{print $2}'

Case ${max_usage} in

[1-6])

MSG= "All is quiet.\" ${max_dir}\ "is ${max_usage}% used"

[7-8])

MSG= "Start thinking about cleaning out some stuff."

MSG= "$MSG There's a partition ${max_dir} is ${max_usage}% used."

9 [1-8])

MSG= "Better hurry with that new disk..."

MSG= "$MSG One partition ${max_dir} is ${max_usage}% used."

99)

MSG= "I'm drowning herel There's a partiton at ${max_usage} used"

)

MSG= "I seem to be runing with an nonexitent amount of disk space..."

Esac

# echo $MSG | mail-s "disk report date" root

Echo $MSG

Running result:

[root@localhost shell] # sh case4.sh

All is quiet. "/" is 22% used

[root@localhost shell] #

Tags: Instance instruction result selection number input run between pattern maximum usage format disk Basics Basics process knowledge script Control Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB MySQL Docker macOS NVidia