当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢

分库分表的 9种分布式主键ID 生成方案,挺全乎的

你这么不努力,怎么做年薪40W的大数据打工人?

【分布式】分布式锁都有哪些实现方案?

The way of a million year salary Architect: on the architecture design of application system

Get this template, double your salary

Well, these four ways to query the maximum value of sliding window are good

JVM学习(五) -执行子系统

除了解析域名,DNS还能干吗?

OpenYurt 深度解读:如何构建 Kubernetes 原生云边高效协同网络?

Android byte beat one side, was hanged by the interviewer! Fortunately, we got the offer from three sides
随机推荐
靠“小抄”进字节:拿到这份模板,薪资能翻倍
Infrastructure testing based on chef inspec
Vscode plug-in configuration pointing North
PAT_甲级_1074 Reversing Linked List
「代码整洁之道-程序员的职业素养」读书笔记
10款必装软件,让Windows使用效率飞起!
Use treeview tree menu bar (recursively call database to create menu automatically)
Put method of immutablemap
CCF BDCI热门赛题:非结构化商业文本信息中隐私信息识别
Efficient Estimation of Word Representations in Vector Space 论文笔记
03. Priority link model
Is multithreading really faster than single threading?
H5公众号点击内置浏览器的关闭(左上角的叉叉)监听到事件
苏宁基于知识图谱的大规模告警收敛和根因定位实践
程序员过高工资导致加班?应该降低程序员工资?网友:放过其他苦逼的程序员吧
Lazy to write a document, swagger document does not smell
Reduce of Flink
“开源软件供应链点亮计划 - 暑期 2020”公布结果 基于 ChubaoFS 开发的项目获得最佳质量奖
接口测试如何在post请求中传递文件
Rainbow sorting | Dutch flag problem