Linux Shell Learning Notes third Day _ other

Source: Internet
Author: User
Tags case statement current time file copy

Day Three: Conditional selection

Outline

application Example Analysis

Condition test

if...else...fi

Case...in...esac

Implement function Menu :

After executing the script

Press 1 to display the current time

Press 2 to show CPU load

Press 3 to display the remaining memory

Press 0 to exit the script

Press another character to exit after the selection is exceeded

Analysis steps.

#date +%t

Uptime awk Intercept

Free–m

Conditional Test Format

#test –option obj

#[-option obj]

return results

    1. Expression content test results are true
    2. Expression content test results are false

Object Classification for testing

Execution results (success or failure of execution)

File (whether the file exists, etc.)

Text (Consistent)

Number (numerical comparison)

Options for conditional tests

Options Role
-D Directory
-E Is there
-F Whether it is a normal file
-S is the file size equal to 0
-R is readable
-W Whether to write
-X Whether to perform

Logical Operation Symbols

Options

Role

-A

and operation

-O

or operation

!

Not operational

Instance:

#test the middle of the –e/etc/passwd–a–e/etc/shadow is a and operation, then 0 is 0.

#test –e/etc/passwd–o–e/etc/groups The middle is O or operation, then one true is true 0

String operators

= = Two strings equal

!= Two strings Not equal

-Z Empty string

-N Non-empty string

Instance:

#test –z $LOGNAME

#echo $LOGNAME

#echo $?

Numeric comparison operator

Symbol

Description

-eq

Equals

-ne

Not equal to

-gt

Greater than

-lt

Less than

-ge

Greater than or equal to

-le

Less than or equal to

if...else...fi condition Selection

The basic format of the If control structure:

If condition #判断开始 can be either a command or a test statement

Then #如果条件为真 inverse value true 0 is performed

Command 1 #执行命令1

else #如果条件为假 the reverse value false 1 executes

Command 2 #执行命令2

Fi #判断结束

Example (if...else...fi) 1

inputtest.sh

#!/bin/bash

#input Test

Echo–n "Enter Your Name:"

Read name

#did the user just hit return

if ["${name}" = ""]

Then

echo "You did don't enter any information"

Else

echo "Your name: ${name}"

Fi

Example (if...else...fi) 2

filecopy.sh

#!/bin/bash

#file Copy

If cp/etc/passwd passwd.bak 2>/dev/null 2>/dev/null get rid of error tips

Then

echo "Good copy!"

Else

echo "' BaseName $ ': Error,could not copy"

Fi

Nesting of if...else...fi (two-layer nesting)

If Condition 1;then

If condition 2;then

Command 1

Else

Command 2

Else

If condition 3;then

Command 3

Else

Command 4

Fi

Case...in...esac condition selection (more flexible way)

Case statement for more branch judgment

Case format: (multiple modes, only matching and variable equal mode)

Case variable in

Mode 1) command 1..;;

Mode 2) command 2..;;

Esac

Match mode

* Match any character

? Match any single word character

[] Match character Range

Case...in.esac Instance 1

#!/bin/bash

#case Select

Echo–n "Enter a number from 1 to 5:"

Read NUM

Case $NUM in

1) echo "you select 1″;;

2) echo "you select 2″;;

3) echo "you select 3″;;

4) echo "you select 4″;;

5) echo "you select 5″;;

*) echo "basename $This is not between 1 and 5″

Esac

Case...in.esac Instance 2

The topic is: The student's examination result is 0-100 points, in 85 above will prompt you to are the best!, displays you in 70-84 to get a good mark! , in 60-74 of the display come on!,60 below show you must study hard!

#!/bin/bash

Echo–n "Please input your mark:"

Read Mark

Case $mark in

100|9[0-9]|8[5-9]) echo "You are the best!";; 100, 90-99, 85-89

8[0-4]|7[0-9]) echo "You get a good mark!";; 80-84, 70-79

7[0-4]|6[0-9]) echo "Come on!"; 70-74, 60-69

[0-5]            [0-9]) echo "You must study hard!";; 00-59

Esac

Solve today's problems

The input variables are judged using the If...else...fi method.

Executes the corresponding statement on each branch of the judgment.

menu.sh

#!/bin/bash

Clear

echo "—————— –menu ————— –"

echo "1" Show Time "

echo "2) CPU Load"

echo "3" Memory free "

echo "0 Exit"

echo "—————————————— –"

Echo-n "Enter you chose [0-3]:"

Read NUM

If [${num}-lt 0-o ${num}-GT 3]

Then

echo "This is not between 0-3."

Else

if ["${num}" = = "1"]

Then

echo "' Date +%t '"

Else

if ["${num}" = = "2"]

Then

echo "' Uptime | Awk-f ' [,:] ' ' {print $} '

Else

if ["${num}" = = "3"]

Then

echo "' Free-m | awk ' $1== ' Mem: ' {print $} ' '

Else

Exit

Fi

Fi

Fi

Fi

This lesson reviews:

Type of condition test

File test

Text test

Numerical test

Logical test

if...else...fi Condition Selection Structure

Case...in...esac

After class test

1, the modification menu.sh adopts the Non menu type, the parameter passes the way to carry on the choice. For example #./menu.sh 1 output time

2, use case method to implement the menu selection mode

Sudu Answer: (inevitably there will be mistakes, but can achieve success)

1, modify the menu.sh after the conclusion

#!/bin/bash

If [$1-lt 0-o $1-GT 3]

Then

echo "This is not between 0-3."

Else

If ["$" = "1"]

Then

echo "' Date +%t '"

Else

If ["$" = "2"]

Then

echo "' Uptime | Awk-f ' [,:] ' ' {print $} '

Else

If ["$" = "3"]

Then

echo "' Free-m | awk ' $1== ' Mem: ' {print $} ' '

Else

Exit

Fi

Fi

Fi

Fi

2, #!/bin/bash

Clear

echo "—————— –menu ————— –"

echo "1" Show Time "

echo "2) CPU Load"

echo "3" Memory free "

echo "0 Exit"

echo "—————————————— –"

Echo-n "Enter you chose [0-3]:"

Read NUM

Case $NUM in

1) date +%t;;

2) Uptime | Awk-f ' [,:] ' {print $} ';;

3) free-m | awk ' $1== ' Mem: "{print $}";;

0) exit;;

*) echo "This isn't between 0-3.";;

Esac

Today the harvest is still more. Half an hour of the tutorial looked for nearly 3 hours.

Although said if...else...fi easier to understand, but with case feeling simple many, hehe, see a person likes it.

It seems to be enough to read a tutorial every day. You'll faint when you see a lot of heads. Oh. Go on with your study. ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.