当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- One year after graduation, I took private jobs to earn 10 W and got offers from several big factories!
- JS判断对象类型方法_typeof怎么用_instanceof怎么用_constructor怎么用_Object.prototype.toString()怎么用
- Hadoop learning (3) - Yarn
- Some common types of error exception in Python
- 跟我一起学.NetCore之EF Core 实战入门,一看就会
- How can you be a big data worker with an annual salary of 40W if you don't work hard?
- 技美那么贵,不如找顾问 | AALab企业顾问业务
- Interface tests how to pass files in post requests
- 靠“小抄”进字节:拿到这份模板,薪资能翻倍
- Navigation component of Android architecture (2)
猜你喜欢
leetcode算法(1)
Android字节跳动一面,被面试官吊打!幸得美团内推,三面拿到offer
SEO builders, what are the unspeakable hardships?
博士在读时,把暗恋的师兄变成了老公是种怎样的体验?
Ali, Tencent, Baidu, Netease, meituan Android interview experience sharing, got Baidu, Tencent offer
分库分表的 9种分布式主键ID 生成方案,挺全乎的
导师制Processing网课 双十一优惠进行中
腾讯云服务器搭建wordpress网站的两种方式(详细图文新手版)
解密未来数据库设计:MongoDB新存储引擎WiredTiger实现(事务篇)
Arthas install quick installation document
随机推荐
Android NDK development and actual combat WeChat official account 2-D code detection
What can DNS do besides resolving domain names?
使用art-Template模板获取天气预报信息
SEO建设者,有哪些说不出的苦?
IDEA解决yml配置文件中文输出乱码问题
Interface tests how to pass files in post requests
Viewing PDB files from the angle of assembly
One year after graduation, I took private jobs to earn 10 W and got offers from several big factories!
如何用函数框架快速开发大型 Web 应用 | 实战
写给我自己的一封信
Offline installation method of Arthas without network environment
pytorch加载语音类自定义数据集
Idea solves garbled Chinese output of YML configuration file
从汇编的角度看pdb文件
技美那么贵,不如找顾问 | AALab企业顾问业务
H5公众号点击内置浏览器的关闭(左上角的叉叉)监听到事件
Android studio import customized framework classess.jar As 4.0.1 version is valid for pro test
Android studio AVD
Leetcode algorithm (1)
大厂面试系列(二):并发编程