当前位置:网站首页>Shell notes
Shell notes
2022-06-12 13:54:00 【123axj】
shell Common mistakes
Shell When defining variables , There can be no spaces between variable names and equal signs , Reference resources shell Variable assignment There can be no blank space - swing07 - Blog Garden If there are spaces , Will be submitted to the command not found Error of
If you are windows The next editor shell Script , And then copy to linux perform , Error in finding prompt , May have a look shell Whether the first line of the file is #!/bin/bash Or empty line . If not , Set the first line to #!/bin/bash
Shell Basics
Shell Basic course Shell course | Novice tutorial
shell Get the current user name /user ID echo $UID and echo $USER
wc -l filename, Get the number of file lines , Reference resources Shell(1)—— Assign a value to a variable 、echo、$、wc -l、wc -w、wc -c、ls、cp_liu537192 The column -CSDN Blog
shell Check if a process is running Reference resources shell Script to see if a process is running _dabao87 The blog of -CSDN Blog _shell Determine whether the process is running
#!/bin/bash
PROC_NAME=$1
ProcNumber=`ps -ef |grep -w $PROC_NAME|grep -v grep|wc -l`
if [ $ProcNumber -le 0 ];then
result=0
else
result=1
fi
echo ${result}
Search for all matching in the file , And then through tail The command only prints the last line , That is, get the first one in reverse . grep -n 'something' file | tail -n 1
typeset -l PROJECT_NAME You can put a variable ( Here is PROJECT_NAME) All the characters of are lowercase . Reference resources typeset Common usage of _ Ministry of truth -CSDN Blog ,typeset Used to set variable properties , Such as case , Width , Left right alignment, etc .typeset -u Change variable characters to uppercase .
function your_fun_name { } Used to define a shell Function of , The function argument uses $1 $2 … Express , Can be used directly in the function body . Use... When calling a function your_fun_name Parameters 1 Parameters 2…
String manipulation
Shell For string operations , String interception reference Shell String interception ( Very detailed ), String merge , Reference resources Shell String splicing ( Connect 、 Merge )
Shell The brackets inside ( Including single bracket and double bracket ) It can be used to test some conditions ( Be careful [ And after ] There must be a space before ):
• Arithmetic comparison , For example, whether a variable is 0, [ $var -eq 0 ].
• File attribute test , For example, whether a file exists ,[ -e $var ], Is it a directory ,[ -d $var ].
String comparison , For example, whether two strings are the same , [[ $var1 = $var2 ]].
Reference resources Shell Summary of the use of brackets in | Novice tutorial
Shell Add command line parameter example
A lot of times , We need to read some parameter configurations from the command line , To make our scripts more flexible . Here is a shell Script example , Used to create command line arguments , among VERSION Set the default value , Other -p and -d No default , Can pass [ -z $PROJECT ] To detect whether the user has brought... When executing the script -p Options .
VERSION=1.0
while [[ "$#" > 0 ]]; do case $1 in
-h|--help) show_help ;;
-v|--version) VERSION=$2; shift;;
-p|--project) PROJECT=$2; shift;;
-d|--directory) ROOTPATH=$2; shift;;
*) echo "Unknown parameter passed: $1"; exit 1;;
esac; shift; done
if [ -z $PROJECT ]
then
echo “error, please set -p (--project) “
exit
fi
Shell Array and for loop
Shell Use in () To define an array , Such as version_array =(“0.1” ,”0.2”). coordination for /do /done You can traverse all the elements in the array .
Accessing an element of an array , Use ${version_array[$element_id]}.
Get the length of the array len=${#version_array[@]}
Access the last element of the array last_id = $len -1 ; last_element=${version_array[$last_id]}
version_array =(“0.1” ,”0.2”)
for element in ${version_array[@]}
do
echo $element
done
Shell It also supports similar projects c/c++ Of for Circular format , Reference resources
shell Medium for Detailed explanation of circular usage _ A Peng's notes -CSDN Blog _shell loop
#!/bin/bash
j=$1
for ((i=1; i<=j; i++))
do
touch file$i && echo file $i is ok
done
File content viewing and replacement sed
stay sed The regular expression in is written in /.../ Between two slashes , If you want to replace all the qualified strings in a row , We can use parameters g, For example, the modification command is as follows :sed 's/aa/AA/g' test.txt, Reference resources https://www.jb51.net/article/111306.htm
Sed -i Replace A character with a backslash or a character with quotation marks , have access to #, Such as sed -i "s#abc#cde#g" file , At this moment if abc Contained in the “/” It can be replaced at will ; Reference resources sed Command replacement characters contain slashes \, How to deal with quotation marks - franjia - Blog Garden
sed -n -e But now shell View the contents of a line in the file in the script . Such as sed -n -e "41p" $FILE1 --- see file FILE1 Of 41 The content of the line . Reference resources utilize sed Realize in shell Change the contents of a line in the file in the script _ Na Ran's column -CSDN Blog _shell Modify the contents of the specified line Shell Script sed Command to modify a line of the file - azureology - Blog Garden
边栏推荐
- one × Convolution kernel of 1
- Ffmpeg Learning Guide
- Codeforces 1629 C. Mexico array - simple greed
- [wustctf2020] selfie score query -1
- Xcode debugging OpenGLES
- Mold and remainder
- C language structure
- Codeforces 1637 B. mex and array - reading, violence
- 如何使用android studio制作一个阿里云物联网APP
- C language array and pointer
猜你喜欢
Implementation of Ackermann function with simulated recursion
[WUSTCTF2020]颜值成绩查询-1
Alibaba Cloud Development Board haas510 submission Device Properties
简述CGI与FASTCGI区别
Dismantle and modify the advertising machine - Amateur decompression
Relevant knowledge points of cocoapods
Mold and remainder
Behind the unsealing of Shanghai, this group of developers "cloud gathering" built an AI anti epidemic robot
Codeforces 1629 F2. Game on sum (hard version) - Yang Hui's triangle, violence, finding rules
Display logs in the database through loganalyzer
随机推荐
Alibaba Cloud Development Board haas510 submission Device Properties
字节序数据读写
开发中使用的语言技巧
Ffmpeg Learning Guide
Scyther工具形式化分析Woo-Lam协议
AVFoundation
Seeking magic square of order n with C language
[WUSTCTF2020]颜值成绩查询-1
【视频课】android studio物联网APP设计制作全套教程--国庆期间全掌握
Codeforces 1629 F1. Game on sum (easy version) - DP, game, thinking
初学者入门阿里云haas510开板式DTU(2.0版本)--510-AS
one × Convolution kernel of 1
618进入后半段,苹果占据高端市场,国产手机终于杀价竞争
[semidrive source code analysis] [x9 chip startup process] 25 - Introduction to mailbox inter core communication mechanism (code analysis) rpmsg-ipcc RTOS & QNX
Codeforces 1629 E. grid XOR - simple thinking
Acwing: topology sequence
阿裏雲開發板HaaS510報送設備屬性
Code debugging - print log output to file
C language array and pointer
Interview question 17.14 Minimum number of K (almost double hundreds)