Shell script loops and signals

Source: Internet
Author: User

Conditional judgment

If condition 1;then

COMMAND

Elif condition 2;then

COMMAND

Else

Command (:): Indicates that pass does not execute any command

Fi

Read user input for data judgment

1   Echo-N"Enter your number:"2 Read Userinput3   Expr$userinput +0&>/dev/NULL4   if[$?-ne0]; Then5     Echo "You must enter a number!"6 Continue7   Else8 COMMAND9   fi
View Code

Case variable reference in

PAT1)

Branch 1

;;

Esac

The difference between a variable name and a variable reference: A variable reference is the need to add a $ in front of the variable name, name representing the variable name $name representing the variable reference

Parallel execution of the shell command wait is the function of the background command to exit the program after execution, the default daemon after the end of the need to manually hit enter to exit the program

1net=172.17.Wuyi2  forIinch{1..254}; Do3{if Ping-c1-w1 $net. $i &>/dev/NULL; Then4           Echo$net. $i is up5     Else6           Echo$net. $i is down7     fi; } &8  Done9 wait
View Code

Loop for Loop

for (EXP1; exp2; exp3); Do COMMANDS; Done

EXP1 is a looping initialization statement that actually does not participate in the loop

EXP3 equivalent to the last statement in the loop body

The exp2 of the loop condition must first determine the execution result of the EXP2 before each execution of the loop body.

1#!/bin/Bash2DECLARE-a array3  for((i=1; i<=Ten; i++))4  Do5array[$i]=$ (random% -))6  Done7 8min=${array[1]}9max=${array[1]}Ten  One  forNuminch${!array[@]}; Do A    Echo$ ((Array[${num})) -    if[[${min}-ge ${array[${num}]}]; Then -min=${array[${num}]} the    fi -    if[[${max}-le ${array[${num}]}]; Then -max=${array[${num}]} -    fi +  -  Done + Echo "The min is $MIN" A Echo "The Max is $MAX"
View Code
1#!/bin/Bash2DECLARE-I min Max3DECLARE-a nums4  for((i=0;i<Ten; i++)); Do5nums[$i]=$RANDOM6[$i-eq0] && min=${nums[$i]} && max=${nums[$i]} &&Continue7[${nums[$i]}-gt $max] && max=${nums[$i]}8[${nums[$i]}-lt $min] && min=${nums[$i]}9  DoneTen Echo "All numbers is ${nums[*]}" One EchoMax is $max A EchoMin is $min
View Code
1 #!/bin/bash2 re= 'WC -L $1cut"" -F1 '  3echo $re
View Code

Continue and break

Continue and break can be added after a number n, indicating that the first n-level of the current round of the end of the cycle so that directly into the next round of judgment, the most inner layer of the loop is the 1th layer

Continue 2 or Break 3

Loop Control Shift Command

Move the parameter list to the left, the default move unit is 1, and the moved parameters are automatically deleted. Used scene: Coordinate with the loop to process the positional parameters provided to the execution script individually

Special usage of the while loop

Loop through the standard output of a file or command

1  DF  while read line;  Do 2          Echo $line 3    Done ; 4 5   while read line;  Do 6         Echo $line 7   Done </root/file
View Code

Select loops and Menus

Select variable in list

Do

Circular Body Command

Done

The Select loop is used primarily to create menus, and menu items in numerical order are displayed on standard errors, and a PS3 prompt is displayed, waiting for the user to enter

The user enters a number in the menu list and executes the corresponding command

User input is saved in the built-in variable REPLY

Signal Capture Trap

The trap ' trigger command ' signals that a custom process receives a specified signal from the system and executes the triggering instruction without performing the original operation

Operation of the trap ' signal ignoring signal

Trap '-' signal recovery operation of the original signal

Use Kill-l to view all the captured signal data

Trap "Echo Press CTRL + C" 2

The kill-9 signal cannot be captured.

[Email protected] ~]#Kill-L1) SIGHUP2) SIGINT3) Sigquit4) Sigill5) SIGTRAP6) SIGABRT7) Sigbus8) SIGFPE9) SIGKILLTen) SIGUSR1 One) SIGSEGV A) SIGUSR2 -) Sigpipe -) SIGALRM the) SIGTERM -) Sigstkflt -) SIGCHLD -) Sigcont +) SIGSTOP -) SIGTSTP +) Sigttin A) Sigttou at) Sigurg -) sigxcpu -) Sigxfsz -) SIGVTALRM -) sigprof -) Sigwinch in) SIGIO -) SIGPWR to) Sigsys the) sigrtmin *) sigrtmin+1     $) sigrtmin+2    Panax Notoginseng) sigrtmin+3 -) sigrtmin+4     the) sigrtmin+5     +) sigrtmin+6     A) sigrtmin+7     the) sigrtmin+8 +) sigrtmin+9     -) sigrtmin+Ten     $) sigrtmin+ One     $) sigrtmin+ A     -) sigrtmin+ - -) sigrtmin+ -     the) sigrtmin+ the     -) sigrtmax- -    Wuyi) sigrtmax- -     the) sigrtmax- A -) sigrtmax- One     Wu) sigrtmax-Ten     -) sigrtmax-9     About) sigrtmax-8     $) sigrtmax-7 -) sigrtmax-6     -) sigrtmax-5     -) sigrtmax-4     A) sigrtmax-3     +) sigrtmax-2 the) sigrtmax-1     -) Sigrtmax
View Code

Shell script loops and signals

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.