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 use Bash script

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use the Bash script, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I. selection and judgment of conditions

1. Conditional selection if

(1) usage format

If condition 1; branch code whose then condition is true elif condition 2; branch code elif condition 3 if then condition is true; branch code else if then condition is true fi

Judge on a condition-by-condition basis, execute its branches when the first encounter is true, and then end the entire if.

(2) Classic cases:

# determine the age #! / bin/bash read-p "Please input your age:" age if [[$age = ~ [^ 0-9]; then echo "please input an int" exit 10 elif [$age-ge 150]; then echo "your age is wrong" exit 20 elif [$age-gt 18]; then echo "good good work,day day up" else echo "good good study,day day up" fi

Analysis: please enter the age, first determine whether the input contains characters other than numbers, if so, report an error; no, continue to determine whether it is less than 150 and whether it is greater than 18.

# judgment score #! / bin/bash read-p "Please input your score:" score if [[$score = ~ [^ 0-9]; then echo "please input an int" exit 10 elif [$score-gt 100]; then echo "Your score is wrong" exit 20 elif [$score-ge 85]; then echo "Your score is very good" elif [$score-ge 60]; then echo "Your score is soso" else echo "You are loser" fi

Analysis: please enter the score, first determine whether the input contains characters other than numbers, if there is, report an error; no, continue to determine whether it is greater than 100, whether it is greater than 85, whether it is greater than 60.

2. Conditional judgment case

(1) usage format

Case $name in; PART1) cmd;; PART2) cmd;; *) cmd;; Note: case supports glob-style wildcards: *: any length, any character?: any single character []: any single character in the specified range a | b: an or b

(2) case:

# judge yes or no #! / bin/bash read-p "Please input yes or no:" anw case $anw in [yY] [eE] [sS] | [yY]) echo yes;; [nN] [oO] | [nN]) echo no;; *) echo false;; esac

Analysis: please enter yes or no, answer Y yes, answer various case combinations for yes;, answer Napo, and No various case combinations for no.

Two or four cycles

1 、 for

(1) usage format

① for name in list; do loop body done ② for ((exp1; exp2; exp3)); do cmd done

Exp1 is executed only once, which is equivalent to embedding while in for.

③ execution mechanism:

Assign the elements in the list to the variable name in turn; execute the loop body after each assignment; until the elements in the list are exhausted and the loop ends

List can be represented by glob wildcards, such as {1... 10}, * .sh, or by variable references, such as seq 1$ name

(2) case

# find the sum of sum=0 read-p "Please input a positive integer:" num if [[$num = ~ [^ 0-9]; then echo "input error" elif [[$num-eq 0]]; then echo "input error" else for i in `seq 1$ num`; do sum=$ [$sum+$i] done echo $sum fi unset zhi

Analysis: the initial value of sum is 0, please enter a number, first determine whether the input contains characters other than numbers, yes, report an error; do not judge whether it is 0, not 0 into the for loop, I range for 1 ~ the number of input, each loop is sum=sum+i, the end of the loop, and finally output the value of sum.

# find the sum of for ((iSum 1) numb * * (0)) do echo "$*" shift done of (1 million 2 minutes.. 100)

3. Return value result

True always returns success result: null command, do nothing, return success result false always return error result

Create an infinite loop

While true; do cycle body done

4. it can be executed in parallel in the loop to make the script run faster

(1) usage

For name in list; do {loop body} & done wait

(2) examples:

# search the ip address of UP read-p "Please input network (eg:192.168.0.0):" net echo $net | egrep-o "\" [$?-eq 0] | | (echo "input error"; exit 10) IP= `echo $net | egrep-o "^ ([0-9] {1} 3}\.) {3}" `for i in {1.. 254}] in the IP address range specified by you (subnet mask 24) Do {ping-c 1-w 1$ IP$i & > / dev/null & &\ echo "$IP$i is up"} & done wait

Analysis: please enter an IP address example 192.168.37.234, if the format is not 0.0.0.0, then report an error exit; if correct, enter the loop, the value of the IP variable is 192.168.37. The range of I is 1-254.The parallel ping 192.168.37.1-154 is pinged and the IP is output as UP. Until the end of the cycle.

4. Signal acquisition trap

1. Usage format

Trap 'trigger instruction' signal. After receiving the specified signal from the system, the custom process will execute the trigger instruction instead of performing the original operation trap 'signal, ignoring the operation trap'-'signal of the signal, restoring the operation trap-p of the original signal, and listing the custom signal operation

The signal can be expressed in three ways: the number 2 of the signal, the full name SIGINT, and the abbreviation INT

2. Common signals

1) SIGHUP: reread the configuration file without shutting down the process 2) SIGINT: abort the running process; equivalent to Ctrl+c 3) SIGQUIT: equivalent to ctrl+\ 9) SIGKILL: force to kill the running process 15) SIGTERM: terminate the running process (default is 15) 18) SIGCONT: continue running 19) SIGSTOP: background hibernation 9 signal, forced kill, cannot be captured

3. Case

# ① printing 0-9 ctrlprinting cannot be terminated #! / bin/bash trap 'echo press ctrl+c' 2 for

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