当前位置:网站首页>A quick start to Shell Scripting
A quick start to Shell Scripting
2020-11-09 14:29:00 【Zhu Zilong 2018】
1.#! The beginning of the script
#!/bin/bash
2. Script properties
Add executable properties ,chmod +x Or use “.” function , For example, run the current directory of a.sh Executable command “. ./a.sh” The file format should be unix, stay linux There is no problem with the file created under , stay windows Write the script to pay special attention to the file format View file format vim Open file set ff View file format set ff=type Format file ,type Is the file format
3. Variable
Letters start with an underline , It can be followed by letters, underscores and numbers assignment : a=1 There can be no spaces on either side of the equal sign ; Cancel variables unset Using weak variables , There can be no type , If you don't define a type, treat it as a string , If you don't add local modification , Then it's a global variable , Works across the entire script use export You can export script variables as environment variables Positional arguments $0 For the script name $n For the script's section n Parameters $? Command and script return values $* or $# Indicates the number of command line parameters A read-only variable readonly
4. Array
Definition :a=('1' '2'); a The value of is enclosed in brackets , Members are enclosed in single quotation marks , Members are separated by spaces Assign member assignment a=([2]=1 [3]=2); quote :${a[1]} ${array[@]} ${array[*]} Get the entire array element Same output , but @ The result is the elements separated by spaces ,* The output is an entire string Array truncation :${array[@]:1:2} Take the first to second elements of the array Linked array c=(${a[@]} ${b[@]}) Replacement elements c=(${c[@]/str1/str2})
5. Paraphrase and quote
\ Using escape characters makes \ The following characters are used only as characters " " Part quote , In part of the quotation $ `( The quotation marks ) \ It's still going to be interpreted as having a special meaning ' ' Full quote , There are many characters in full references, which are just ordinary characters
6. Command substitution
Assign the standard output of the command as an assignment to a variable Use (`) or $() $() Only in bash shell With effective With date Command as an example DATE=`date' DATE=$(date)
7. Operator
1. Arithmetic operator + - * / % ** ( Power operation )+= -= *= /= %= ++ -- Use let Command calculation let a=1+2 2. An operator , << >> & | ^ ~ Also use let Command to operate 3. Use $[] or $(()) operation echo $[2+3] 4. Use expr operation expr 1 + 1 Ask the operands and operator times to be separated by spaces 5.declare command use declare Declare variable type , You can do it directly later declare -i a; a=1+1; There must be no spaces on either side of the operator 6. Use bc Do floating point operations e=$("scale=3;$a/$b"|bc); scale Set the actual number of floating-point numbers , If it is not set, it will be displayed as an integer result
8. Test and judge
test [ ] Format test expression perhaps [ expression ] Notice that there are spaces on both sides of the expression ; 1. File test -b FILE Returns true if the file exists and is a block device -c FILE Returns true if the file exists and is a character device -d FILE Returns true when the file exists and is a directory -e FILE The existence of a file or directory returns true -f FILE Return true when the file exists and is a normal file -x FILE If it is true, the executable file is returned -w FILE Returns true if the file exists and is writable -r FILE Returns true when the file exists and is readable -l FILE Returns true when the file exists and is a link -p FILE Returns true if the file exists and is a pipe -s FILE File exists and size is not 0 Back to true -S FILE The file exists and is socket Back to true -g FILE The file exists and is set to SGID Back to true -u FILE The file exists and is set to SUID Back to true -k FILE The file exists and is set to sticky Back to true -G FILE True is returned when the file exists and belongs to a valid user group -O FILE True is returned when the file exists and belongs to a valid user FILE1 -nt FILE2 When file 1 Than file 2 New time returns to true FILE1 -ot FILE2 When file 1 Than file 2 It's true to return to the old days
2. String test -z “string” Returns true when the string is empty -n “string” Returns true when the string is not empty “string1”= “string2” The same returns true “string1”!= “string2” Different return true “string1” > “string2” Dictionary sort ,string1 be ranked at string2 Back before, really “string1”< “string2” Dictionary sort ,string1 be ranked at string2 And then back to true 3. Integer comparison "num1" -eq "num2" equal "num1" -gt "num2" Greater than "num1" -lt "num2" Less than "num1" -ge "num2" Greater than or equal to "num1" -le "num2" Less than or equal to "num1" -ne "num2" It's not equal to 4. Logic tester !expression Not expression1 -a expression2 And expression1 -o expression2 or 5. Logical operators ! Logic is not , Take the opposite of true and false && Logic and , Join two expressions , Only when both expressions are true , The result is true || Logic or
9. Logical structure The branch of judgment
1.if
// The basic structure
if expression;then
command1
command2
fi
//if/else structure
if expression;then
command1
command2
else
command3
fi
//if/elif/else structure
if expression;then
command
elif expression2;then
command
fi
2.case
case VAR in
var1) command;;
var2) command;;
var3) command;;
*)command;;
esac
10. Logical structure loop
1.for
for VARIABLE in (list) // If the variable list is a continuous number , It can be written as {1..n} $(seq 1 n) perhaps $(seq 1 2 100) From 1 To 100, Each time 2, namely 1 To 100 The odd number
do
command
done
// class C Of for loop
for((expression1;expression2;expression))
do
command
done
2.while
while expression
do
command
done
//while Dead cycle
while (1)
while true
while:
3.until
unitl wxpression
do
command
done
//until Dead cycle
until ((0))
until false
4.slect And single parameter for be similar , It can also be used. if case Realization
select varible in (list)
do
command
done
5. Cycle control break continue break One by one break, Jump out of current loop ,break n, Jump out of the present n Layer cycle continue End the current cycle , Enter next cycle ,continue n, End the present n Layer cycle ,
11. function
1. Definition
function FUNCTION_NAME() // keyword function It can be omitted
{
command1;
command2;
return n; // When a function has a return value , adopt $? To receive the return value
}
2. Function parameter passing Even global variables , Not passed to the function , Functions cannot be used , contrary , If the variable inside the function is not declared local, Then the variables inside the function can be used in the whole script Function transfer parameter form
function $a
版权声明
本文为[Zhu Zilong 2018]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢
导师制Processing网课 双十一优惠进行中
Ali, Tencent, Baidu, Netease, meituan Android interview experience sharing, got Baidu, Tencent offer
03. Priority link model
03.优先链接模型
IDEA rest-client,会了它我还没打开过postman
CCF BDCI热门赛题:非结构化商业文本信息中隐私信息识别
Tutorial system unity online course double 11 preferential registration is in progress
懒得写文档,swagger文档导出来不香吗
Android studio AVD
Idea rest client, yes, I haven't opened postman yet
随机推荐
乘风破浪的技术大咖再次集结 | 腾讯云TVP持续航行中
Well, the four ways to query the maximum value of sliding window are good
决策树算法-理论篇
【亲测有效】Github无法访问或者访问速度的解决方案
高德全链路压测——语料智能化演进之路
H5 official account listens to events in the closed browser (left fork).
Position promotion | intelligent multimedia group of Microsoft Asia research institute recruits computer vision algorithm Intern
The use of Android studio Aidl
OpenYurt 深度解读:如何构建 Kubernetes 原生云边高效协同网络?
Notes for csp-j / s 2020
JVM学习(五) -执行子系统
接口测试如何在post请求中传递文件
Several methods of initializing singleton variable in go language
Online course of tutorial system processing is in progress
中国程序员 VS 日本程序员,满屏的羞羞!
JS method of judging object type_ How to use typeof_ How to use instanceof_ How to use constructor_ Object.prototype.toString How to use ()
I interviewed a 33 year old Android programmer, who could only program for Baidu, but wanted 25K, which was met by me
shell脚本快速入门----shell基本语法总结
Idea solves garbled Chinese output of YML configuration file
Reduce of Flink