当前位置:网站首页>Shell Scripting
Shell Scripting
2022-07-06 15:59:00 【Star age Cao Botao】
Shell Programming
Once in a while Linux Write something simple in the environment Shell Script , This article can be used as a reference , Write some simple scripts .
Grammatical norms
Define the execution environment
#!/usr/bin/bash
perhaps
#!/usr/bin/env bash | python |perl
perform
Modify the permissions
chmod +x ./demo.sh
perhaps
chmod 700 ./demo.sh
./demo.sh
perhaps
bash ./demo.sh
sh ./demo.sh
# adopt bash sh There is no need to modify permissions , Specify the execution environment
Special symbols
Symbol | explain |
---|---|
~ | Home directory |
- | Upper level directory |
!! | Execute the last order |
$ | Get environment variables ${USER} |
±*/% | Operator |
* | wildcard Multiple |
? | One |
| | Pipe, |
\ | Escape character |
> | Redirect (echo abc > ./demo.txt) |
>> | Redirect (echo abc >> ./demo.txt) Additional |
< | wc < abc.txt |
<< |
notes
:<<EOF
The comment ...
The comment ...
The comment ...
EOF
Variable
# Defining variables
name="zhangsan"
age=18
# read-only
readonly name
# Delete variables
unset age
# visit
echo ${name}
# Get string length
echo ${
#name}
# Extract substring
echo ${string:0:2} # Output unoo
Array
arr=("a" "b" "c" "d")
echo arr[0]
# Get all
echo arr[@]
echo arr[*]
# Gets the length of the array
length=${
#arr[@]}
operation
Native bash Simple mathematical operations are not supported , But you can do it with other commands , for example awk and expr,expr The most commonly used .
$(( Arithmetic expression )) or $[ Arithmetic expression ], More efficient .
#!/bin/bash
val=`expr 2 + 2`
echo " The sum of the two is : $val"
# 1.(())
echo $((1+1))
# 2.let
let a+=6 c=a+b
# Be careful , For similar let x+y This way of writing ,Shell Although the calculation x+y Value , But discard the result ; If you don't want to , have access to let sum=x+y take x+y The result of is saved in the variable sum in .
read Read console input
Two points attention :
Space between expression and operator , for example 2+2 It is not right , Must be written as 2 + 2, This is different from most programming languages we are familiar with .
The complete expression is to be `` contain , Note that this character is not a common single quotation mark , stay Esc Key bottom .
Conditional expressions should be placed between square brackets , And there should be spaces , for example : [ a = = a== a==b] It's wrong. , Must be written as [ $a == $b ].
Multiplication sign (*) There must be a backslash in the front () To achieve multiplication ;
Operator | explain | give an example |
---|---|---|
! | Non operation , Expression for true Then return to false, Otherwise return to true. | [ ! false ] return true. |
-o | Or operations , There is an expression for true Then return to true. | [ $a -lt 20 -o $b -gt 100 ] return true. |
-a | And operation , Both expressions are true To return to true. | [ $a -lt 20 -a $b -gt 100 ] return false. |
The file test operator is used to detect Unix Various attributes of the file .
The operator | explain | give an example |
---|---|---|
-b file | Check if the file is a block device file , If it is , Then return to true. | [ -b $file ] return false. |
-c file | Check whether the file is a character device file , If it is , Then return to true. | [ -c $file ] return false. |
-d file | Check if the file is a directory , If it is , Then return to true. | [ -d $file ] return false. |
-f file | Check if the file is a normal file ( It's not a catalog , It's not a device file ), If it is , Then return to true. | [ -f $file ] return true. |
-g file | Check if the file is set SGID position , If it is , Then return to true. | [ -g $file ] return false. |
-k file | Check whether the file is set with the adhesive bit (Sticky Bit), If it is , Then return to true. | [ -k $file ] return false. |
-p file | Check if the file is a named pipeline , If it is , Then return to true. | [ -p $file ] return false. |
-u file | Check if the file is set SUID position , If it is , Then return to true. | [ -u $file ] return false. |
-r file | Check whether the file is readable , If it is , Then return to true. | [ -r $file ] return true. |
-w file | Check whether the file is writable , If it is , Then return to true. | [ -w $file ] return true. |
-x file | Check if the file is executable , If it is , Then return to true. | [ -x $file ] return true. |
-s file | Check if the file is empty ( Is the file size greater than 0), Not empty return true. | [ -s $file ] return true. |
-e file | Test files ( Including directory ) Whether there is , If it is , Then return to true. | [ -e $file ] return true. |
Other checkers :
- -S: Determine whether a file is socket.
- -L: Check whether the file exists and is a symbolic link .
Process control
if
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
for
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
for((i=1;i<=5;i++))
do
echo " This is the first $i Secondary call ";
done;
while
while condition
do
command
done
# Example
#!/bin/bash
int=1
while(( $int<=5 ))
do
echo $int
let "int++"
done
File contains
adopt . Import the files that need to be included
边栏推荐
- Penetration test (3) -- Metasploit framework (MSF)
- Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
- 1010 things that college students majoring in it must do before graduation
- Optimization method of path problem before dynamic planning
- CEP used by Flink
- Cost accounting [23]
- C语言必背代码大全
- Research Report on market supply and demand and strategy of geosynthetics industry in China
- 力扣刷题记录
- b站 实时弹幕和历史弹幕 Protobuf 格式解析
猜你喜欢
信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
[exercise-5] (UVA 839) not so mobile (balance)
滲透測試 ( 1 ) --- 必備 工具、導航
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
Learning record: how to perform PWM output
X-Forwarded-For详解、如何获取到客户端IP
Nodejs+vue网上鲜花店销售信息系统express+mysql
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
数据在内存中的存储&载入内存,让程序运行起来
随机推荐
Market trend report, technical innovation and market forecast of geosynthetic clay liner in China
China earth moving machinery market trend report, technical dynamic innovation and market forecast
Cost accounting [19]
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
[exercise-2] (UVA 712) s-trees
信息安全-安全专业名称|CVE|RCE|POC|VUL|0DAY
Path problem before dynamic planning
Record of force deduction and question brushing
Opencv learning log 32 edge extraction
通俗地理解什么是编程语言
0 - 1 problème de sac à dos (1)
Opencv learning log 31 -- background difference
滲透測試 ( 1 ) --- 必備 工具、導航
Matlab comprehensive exercise: application in signal and system
对iptables进行常规操作
渗透测试 ( 4 ) --- Meterpreter 命令详解
frida hook so层、protobuf 数据解析
B - 代码派对(女生赛)
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
Research Report on market supply and demand and strategy of China's land incineration plant industry