Bash a few common code
######## #最简单的bash
#! /bin/bash #解析器路径 env #查看环境
######### #看运行时间
#!/bin/time. SH start=$ (date +%s) #commands; #statements; Sleep Ten ; #command End=$ (date +%s) Difference=$ ((End- start))echo time taken To execute $0 is $difference seconds.
########## while loop
#!/bin/-i i= 1sum=0while ((i<=)) do sum + = i + I done echo $ sum
########## about the interrupt cursor
#!/bin/bash# file name:Sleep.SHEcho-n count:# tput SC storage cursor position tput Sccount=0; while true; Do if[$count-lt + ]; ThenLet count++; Sleep 1; # The command to restore the cursor position is tput RC tput RC # tput Ed clears all content from the current cursor position to the end of the line tput EdEcho-n $count; ElseExit0; fi Done
########### #shell判断
#! /bin/Bashdeclare-I. num=0if[$num-lt0] There is a space between the #一定要注意在 [or] and the operand Then Echo "<";elif[$num-eq0 ] Then Echo "==";Else Echo ">";fi# [$var 1-ne0-A $var 2-GT2] #使用逻辑与-a# [$var 1-ne0-O VAR2-GT2] #逻辑或-O
########### #shell Debug
#!/bin/bash# file name: Debug. SH for inch {1.. 6 }; Do -x echo $i +xdone echo " Script executed "
########### #break
#!/bin/Bash while : Do Echo-N"Input A number between 1 to 5:"Read Anum Case$aNuminch 1|2|3|4|5)Echo "Your number is $aNum!" ;; *)Echo "You does not select a number between 1 to 5, the game is over!"Break ;; Esac Done
############# #case
#! /bin/BashEcho 'Input A number between 1 to 4'Echo 'Your number is:\c'Read Anum Case$aNuminch 1)Echo 'You select 1' ;; 2)Echo 'You select 2' ;; 3)Echo 'You select 3' ;; 4)Echo 'You Select 4' ;; *)Echo 'You does not select a number between 1 to 4' ;;Esac
######## #function
function fname () { echo $1, $2; #访问参数1和参数2 echo" [email protected] " ; #以列表的方式一次性打印所有参数 Echo " $* " ; #类似于 [email protected], but the parameter is used as a single entity 0 -F fname
Bash Shell Basics