当前位置:网站首页>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
边栏推荐
- Opencv learning log 16 paperclip count
- 【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
- Optimization method of path problem before dynamic planning
- [exercise-6] (PTA) divide and conquer
- China chart recorder market trend report, technology dynamic innovation and market forecast
- TCP的三次握手与四次挥手
- Opencv learning log 13 corrosion, expansion, opening and closing operations
- 数据在内存中的存储&载入内存,让程序运行起来
- Cost accounting [21]
- [analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
猜你喜欢
b站 实时弹幕和历史弹幕 Protobuf 格式解析
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
STM32 learning record: LED light flashes (register version)
C语言学习笔记
Penetration test (3) -- Metasploit framework (MSF)
[exercise-5] (UVA 839) not so mobile (balance)
STM32如何使用STLINK下载程序:点亮LED跑马灯(库版本)
动态规划前路径问题优化方式
随机推荐
China's PCB connector market trend report, technological innovation and market forecast
China's peripheral catheter market trend report, technological innovation and market forecast
Opencv learning log 31 -- background difference
C语言数组的概念
Cost accounting [18]
Cost accounting [21]
Learning record: Tim - Basic timer
【练习-9】Zombie’s Treasure Chest
【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)
China's salt water membrane market trend report, technological innovation and market forecast
Accounting regulations and professional ethics [5]
Research Report on market supply and demand and strategy of China's earth drilling industry
【练习-11】4 Values whose Sum is 0(和为0的4个值)
[exercise-6] (PTA) divide and conquer
Matlab comprehensive exercise: application in signal and system
Research Report on market supply and demand and strategy of Chinese graphic screen printing equipment industry
Perinatal Software Industry Research Report - market status analysis and development prospect forecast
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
TCP的三次握手与四次挥手
Cost accounting [13]