Get the App
SLTechnology News&Howtos  ›  Servers  › 

Shell condition judgment

Shulou Source: shulou.com Published: 2022-06-02 08:00:51 09月29日 Update

Before we begin, take a look at the logical judgment symbols: & & and, | or! Non-A & & B must be established at the same time in order to pass the judgment-> judgment process: first execute A to determine whether it is established, and if it is established, continue to execute B to determine whether it is established-- > if it is not established, then directly end, no longer execute BA | | BA, B has an establishment, you can pass the judgment! An if An is not true, then by judging the number and symbol, as follows: comparison value: equal to-- >-- eq does not equal->-ne less than-- >-- lt greater than-- >-- gt less than or equal->-- le greater than or equal to->-ge some English words: help to remember some English words: equal equal to, not equal not equal, less than less than, great than greater than less equal less than or equal to great equal.

Comparison string: = equal, = equal, same =,! = not equal, > greater than, the application source code is as follows: [root@linux4 ~] # if [- d / root]; then / / to determine whether the root directory exists

> echo "ok" / / if there is an input OK

> fi

Ok## practical 2:if dual Branch Application-> pinghost.sh

Ping-c 3-I 0.2-w 3 192.168.1.1 the number of packets sent by Murc-I the interval between each packet sent. The default unit: second (s)-w waiting time. The source code that exceeds the return failure is as follows: #! / bin/bash# ping a given host and print the host status

Ip=192.168.1.1

Ping-c 3-I 0.2-w 3$ ip > > / dev/null #-c count-I?-w wait?

If [$?-eq 0]; then?

Echo "the host $ip is up."?

Else

Echo "the host $ip is down."?

Fi## actual combat 3:if multi-branch application-> judge numerical interval num.sh

The source code is as follows: #! / bin/bashread-p "Please enter a score:" numif [$num-gt 85-a $num-lt 100]; then

Echo 'excellent!'

Elif [$num-gt 70]; then

Echo 'good!'

Elif [$num-gt 60]; then

Echo 'qualified!'

Else

Echo 'not qualified!'

Fi

# # practical 4:if nesting use

The source code is as follows: #! / bin/bash

Read-p "Please enter the student's grade:" a

If [$a-lt 10]; then

Echo "Congratulations to this student on reaching the finals!"

Read-p "Please enter the classmate's gender:" b

If [$b = "male"]; then

Echo "Congratulations to this student on entering the men's final group!"

Else

Echo "Congratulations to this student on entering the women's final group!"

Fi

Fi

Tags: Statement branch classmate actual combat source code input condition character string final application score numerical value data time flow symbol excellent good number Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology NVidia Huawei Linux