当前位置:网站首页>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
边栏推荐
- Matlab comprehensive exercise: application in signal and system
- 渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
- Perinatal Software Industry Research Report - market status analysis and development prospect forecast
- MATLAB综合练习:信号与系统中的应用
- Indonesian medical sensor Industry Research Report - market status analysis and development prospect forecast
- Information security - Analysis of security orchestration automation and response (soar) technology
- 0-1 knapsack problem (I)
- China exterior wall cladding (EWC) market trend report, technical dynamic innovation and market forecast
- Opencv learning log 14 - count the number of coins in the picture (regardless of overlap)
- b站 实时弹幕和历史弹幕 Protobuf 格式解析
猜你喜欢
力扣刷题记录
基于web的照片数码冲印网站
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
[exercise-7] crossover answers
C语言必背代码大全
X-forwarded-for details, how to get the client IP
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
Information security - Analysis of security orchestration automation and response (soar) technology
数据在内存中的存储&载入内存,让程序运行起来
洛谷P1102 A-B数对(二分,map,双指针)
随机推荐
[exercise-8] (UVA 246) 10-20-30== simulation
China earth moving machinery market trend report, technical dynamic innovation and market forecast
0 - 1 problème de sac à dos (1)
TCP的三次握手与四次挥手
VS2019初步使用
STM32如何使用STLINK下载程序:点亮LED跑马灯(库版本)
CS zero foundation introductory learning record
Research Report on printed circuit board (PCB) connector industry - market status analysis and development prospect forecast
信息安全-安全编排自动化与响应 (SOAR) 技术解析
Accounting regulations and professional ethics [1]
Penetration test (1) -- necessary tools, navigation
Cost accounting [15]
Cost accounting [13]
[exercise-7] crossover answers
If you want to apply for a programmer, your resume should be written like this [essence summary]
Record of force deduction and question brushing
【练习-6】(Uva 725)Division(除法)== 暴力
Cost accounting [14]
Gartner: five suggestions on best practices for zero trust network access
Learning record: Tim - Basic timer