First set a small goal, first proficient in the shell say! --OLD boyUseShelldeveloping a Springboard function script caseexample 17_6: Developing an Enterprise-class Shell springboard machine case. Requiring a user to log on to a springboard machine can only perform an administrator-given option action, and does not allow any form of interrupt script to execute a
Tags: case judge logical judgment file directory attribute in shell script determine if special usageI. Logical judgments in the shell
Format 1:if condition; Then statement; Fia=2if [ $a -ge 2 ];then echo ">=2";fi
Format 2:if condition; Then statement; else statement; Fia=1if [ $a -ge 2 ];then echo ">=2";else echo "
Format 3:if ...; Then ...; Eli
Tags: control statement CTO ROC read case share picture COM ASE pngThe Linux shell has learned several times.But the memory is not good, always forget, chase the main reason may be not common reason!Case beginning Esac EndHere's the result.Input method does not enter the symbol is wrong, the beginning of the total error, and then switched the input method is good
I. Overview of CASE statementsUse case statements to rewrite if multiple branches to make the script structure clearer and more hierarchicalPerform different command sequences for different values of variablesThe case statement is structured as follows:Case Variable value inMode 1)Command sequence 1;;Mode 2)Command Sequence 2;;*)Default command sequenceEsac1 #!/b
number of arbitrary characters meaning! 9Echo"Usage $ {Hello}"Ten ;; OneEsacTo create a shell script with the VI editor:To write a shell script with the VI editor:The results of the implementation are as follows:In the above sh09-2.sh case, if you enter "sh test02.sh test" to execute, then "usage test02.sh {Hello}" will appear on the screen, telling the
if logic judgment in shell scripts
Logical judgment Expression:
-GT (>); Greater than great than
-lt (
If logical judgment format:
Format 1:if condition; Then statement; Fi
format 2:if condition; then statement; Else statement; Fi
format 3:if ...; Elif ...; Then ...; else ...; Fi
--
can use | | Combine multiple conditions
a conditions b:a and b
conditions a| | Condition b:a or b
if [$a-gt 5] [$a-lt]; then
if [$b-gt 5] | | [$b-lt 3]; Then
F
Tags: for statement while statement case statement Shell Yang ShufanThe shell, as a scripting language, also contains other program control structures such as loops, branches, and so on, making it easier to perform more complex and powerful functions.Using the FOR Loop statementAt work, you often encounter a task that needs to be executed multiple times, and each
Case statement in shell: It can match the variable content with multiple templates, and then decide which part of the Code should be executed based on the template that matches successfully. Format of www.2cto.com: case matching mother board in TEMPLATE 1 [| template 2]…) Statement group; TEMPLATE 3 [| TEMPLATE 4]…) Statement group; the matching sequence of esacc
Shell Process Control (if, else, case, while, for, until), shellcase
1. Select Conditions
1.1.if statement
Simple syntax
#!/bin/bashMATH_SCORES="$1"NAME="$2"if [ -z "${MATH_SCORES}" ]then printf "The command requires that options have a scores.\n" printf "What's ths scores of your math? :" read MATH_SCORESfiif [ -z "${NAME}" ]then printf "The command requires that options have a student's name.\n" prin
shell programming often encounters strings, file content-case conversions, and choosing the right commands in different scenarios can improve programming efficiency. Applicable Scenariosfile contents or strings that need to be converted to uppercase and lowercasestring Case Substitutionlowercase Replace UppercaseEcho " Hello World " TR ' [A-z] ' ' [A-z] ' Echo "
Case: Flow control statements• Loop Statement For...done statement• Use (()) to extend the method of using arithmetic operations in the shell• Looping statement nesting• Jump out of the loop: Break and Continueshift parameter Left SHIFT commandHow to use functions in shellCase statement; Select structureCase SWITCH inVALUE1)Statement.....;;Value2)Statement.....;;*)Statement.....;;Esac#! /bin/bashecho "*****
to break in other languages, it means jumping to the end of the entire case statement.The value will detect each pattern that matches. Once the pattern matches, the other mode is no longer resumed after the corresponding command is executed. If there is no matching pattern, use an asterisk * to capture the value and then execute the subsequent command.The following script prompts you to enter 1 to 4 to match each pattern:
Echo ' Input a number be
The syntax format of case is as follows:Case VaR inExp-1) command body 1 ;;Exp-2) command body 2 ;;...*) Command body ;;Shell calculates the value of the variable VAR, and compares the result with the formula exp-1 and exp-2 until a matching formula is found. If a match is found, run the following command until a semicolon (;) is encountered.
Generally, the case
;; *) commond ;; esacIn a case program, you can use the |, meaning, or mean of a condition such as2|3)Command;;2. Script case#!/bin/bashread -p "Please input a number: " n #read命令让用户输入字符串,n表示要捕获的变量。if [ -z "$n" ] then echo "Please input a number." exit 1 fin1=`echo $n
#case: ESAC statement: with switch. Case similar, is a multi-branch selection structure#case语句匹配一个值或一个模式 if the match succeeds in executing the corresponding command, if the match does not execute the * command successfully#;; Similar to break, the delegate jumps to the end of the entire case statement#
Tags: CPU monitoring shell Linux AutomationRequirements: accurately monitor CPU with top commandPreparation knowledge: Top use, basic awk, DC (default bash shell does not support decimal operations)The script is as follows[[emailprotected] scripts]# cat cpu.sh #!/bin/bash -# top -n 参数指定运行次数,1代表运行一次即停止,不再等待top数据更新,使用awk指定分割符,提取数据cpu_us=`top -n 1 | grep ‘Cpu(s)‘ | awk -F‘[" "%]+‘ ‘{print $2}‘`cpu_sy=`top -n 1
Shell programming control structure: expr, let, for, while, until, shift, if, case, break, continue, function, select
1. expr calculates the integer variable value
S = 'expr 2 + 3'
The operator numbers and parameters must be separated by spaces;
2. let command
Let s = (2 + 3) * 4
Echo $ s
3. for statement
For variable in list
Do
Command Line (loop variables are usually used)
Done
#!/bin/b
The case statement and the judgment statement [If...elif...else] function similarly;When logic is relatively simple, the code is much less than the latter.Case usage, use a variable to match a value, if the match succeeds, execute the command below it until;; so far.#!/bin/bash #a =20 #定义变量值case $a in #若变量在下面的某值中, execute the command below ) #值内容 can add |, can have multiple values { e
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.