Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the judgment of if

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will take you to understand how to analyze if judgment. The knowledge points in the article are introduced in great detail. Friends who feel helpful can browse the content of the article with the editor, hoping to help more friends who want to solve this problem to find the answer to the problem. Follow the editor to learn more about "how to analyze if judgment".

Single branch (single branch is rarely used)

"check_mem.sh" 12L 318C 12J 1 all #! / bin/bashmt=$ (free | awk'/ ^ Mem:/ {print $2}') mu=$ (free | awk'/ ^ Mem:/ {print $3}') mu100=$ (expr $mu\ * 100 / $mt) [$mu100 -gt 1] & & echo "memory usage space has been exceeded Reached $mu100% "disk=$ (df | awk-F' [%] +'/\ / $/ {print $5}') if [$disk-gt 1] then echo" disk usage has exceeded, reached $disk% "fi

Less code without a single branch

#! / bin/bashmt=$ (free | awk'/ ^ Mem:/ {print $2}') mu=$ (free | awk'/ ^ Mem:/ {print $3}') mu100=$ (expr $mu\ * 100 / $mt) [$mu100-gt 1] & & echo "memory usage has exceeded, reaching $mu100%" disk=$ (df | awk-F' [%] +'/\ / $/ {print $5}') [$disk-gt 1] & & echo "disk usage has exceeded, reached $disk%"

Double branch

[root@bogon ~] # vim sfz.shangxinxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Else echo "Please keep trying!" Fi [root@bogon ~] # sh sfz.sh Please enter your score (range 1-100): 65 Please keep trying! [root@bogon ~] # sh sfz.sh Please enter your score (range 1-100): 90 your grades are excellent, keep it!

#! / bin/bash#-c 3 three ping packages #-I 0.2 interval 0.2s# $1 location variable #-W 1 corresponding timeout ping-c 3-I 0.2-w 1 $1 & > / dev/nullif [$?-eq 0] then echo "Host $1 is up." else echo "Host $1 is down." fi [root@bogon ~] # sh check_host.sh 192.168.200.111Host 192.168. 200.111 is up. [root@bogon] # sh check_host.sh 192.168.200.112Host 192.168.200.112 is down. [root@bogon] # sh check_host.sh 192.168.200.113Host 192.168.200.113 is down.

Multi-branch if statement structure: there is no limit to the number of elif. When the number is large, it is recommended to use case.

Read-p "Please enter a number:" numif [$(expr $num% 2)-eq 0] then echo "even number" else echo "odd number" [root @ bogon] # vim grade.shemaker Bash read-p "Please enter your test score (0-100):" numif [$num-ge 0] & [$num-le 100] then if [$num-le 100] & & [$num-ge 80] Then echo "excellent" elif [$num-le 79] & & [$num-ge 60] then echo "qualified" else echo "unqualified" fielse echo entered incorrectly "exit 2fi# exit 2 defines the return value as 2 [root@bogon ~] # sh grade .sh Please enter your test score (0100): 120incorrectly [root@bogon] # sh grade.sh Please enter your test score (0100): 100excellent [root@bogon] # sh grade.sh Please enter your test score (0100): 78 pass [root@bogon ~] # sh grade.sh please enter your test score (0100): 30 failed

two。 Patrol inspection alarm

Thank you for your reading. The above is the whole content of "how to analyze if judgment". Friends who learn to learn to do it quickly. I believe that the editor will certainly bring you better quality articles. Thank you for your support to the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report