1#!/bin/Bash2 3#shell使用的熟练成都反映用户对Unix/how proficient Linux is used4 #shell There are two ways to execute commands: Interactive and batch processing5#常见的shell脚本解释器有bash,SH, CSH, etc.6#chmod+x./hello.SH#使脚本具有执行权限7#./hello.SH#执行脚本8 9 #交互输入Ten Echo "Whats your name?" One Read person A Echo "Hello, $person" - - #变量 definition does not add $, reference to add $ theMyname="shiqing" - Echo${myname} - - #特殊变量 + Echo "filename:$0" - Echo "First parameter:$1" + Echo "Second parameter:$2" A Echo "All Parameter:[email protected]" at Echo "All parameter:$*" - Echo "Total number of parameters:$#" - - #命令替换 ~ down ' command ' -Users= 'W.H.O.|WC-l ' - Echo "logged in user is $USERS" in -#表达式, native bash does not support simple math operations,Expris an expression-calculation tool toVal= 'Expr 2+9` + Echo "value is $val" -A=Ten theb= - *Val= 'Expr$a \*$b ' $ Echo "A * b: $val"Panax Notoginseng - #字符串 theStr="ABCD" + Echo${#str} A Echo${STR:1:4} the + #数组 -Arr=(a,b,c,d) $ Echo "First : ${arr[0]}" $ Echo "all:${arr[*]}"#获取所有元素 - Echo "length:${#arr [*]}"#获取数组长度 - the #ifElif...else ..., conditional expressions are placed between square brackets and have spaces -A=TenWuyib= - the - if[$a = =$b] Wu Then - Echo "A is equal to B" About elif[$a-GT $b] $ Then - Echo "A is G reater than B" - Else - Echo "A is less than B" A fi + the# Case... .. easc ... - Echo 'Input A number between 1 to 4' $ Echo 'Your number is:\c' the Read Anum the Case$aNuminch the 1)Echo 'You select 1' the ;; - 2)Echo 'You select 2' in ;; the 3)Echo 'You select 3' the ;; About 4)Echo 'You Select 4' the ;; the*)Echo 'You does not select a number between 1 to 4' the ;; + Esac - the# for...inch...Bayi forLoopinch 1 2 3 4 5 the Do the Echo "The value is: $loop" - Done - the# while... Do... Done... the Echo 'type <CTRL-D> to terminate' theI=0 the whileRead FILM - Do the Echo "yeah! Great film The $FILM" theI= 'Expr$i +1` theb=394 if[$i-Gt$b] There must be a space between the #expression and square brackets ([]), or there will be a syntax error. the Then the Break the fi98 Done About -#until... Do... Done101A=0102 until[! $a-ltTen ]103 Do104 Echo$a theA= 'Expr$a +1`106Val= 'Expr$a +$b '107 Done108 109 the #函数111 Funwithparam () { the Echo "The value of the first parameter is $ !"113 Echo "The value of the second parameter is $!" the Echo "The value of the tenth parameter is $!" the Echo "The value of the tenth parameter is ${10}!" the Echo "The value of the eleventh parameter is ${11}!"117 Echo "The amount of the parameters is $#!"# Number of parameters118 Echo "The string of the parameters is $*!"# All arguments passed to the function119 } -Funwithparam1 2 3 4 5 6 7 8 9 the the121 122 Funwithreturn () {123 Echo "The function is to get the sum of the numbers ..."124 Echo-N"Input First Number:" the Read Anum126 Echo-N"Input Another number:"127 Read Anothernum - Echo "The numbers is $aNum and $anotherNum!"129Return $ (($aNum +$anotherNum)) the }131 Funwithreturn theRet=$? #函数返回值在执行函数后通过 $?Get133 Echo "The sum of numbers is $ret!"134 135#输入输出重定向 >,>> (additional), <136 W.H.O.>Users137 WC-l<users
Getting started with Linux bash