当前位置:网站首页>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
边栏推荐
- [wustctf2020] selfie score query -1
- CUDA error: CUBLAS_ STATUS_ NOT_ INITIALIZED when calling `cublasCreate(handle)`
- 字节序数据读写
- Web3.0, the era of "stimulating creativity"
- xcode 调试openGLES
- Codeforces 1629 D. pecuriar movie preferences - simple thinking, palindrome strings
- 上海解封背后,这群开发者“云聚会”造了个AI抗疫机器人
- Rk3399 platform development series explanation (kernel debugging chapter) 2.50 use of systrace
- Greed issues - Egypt scores
- Install RPM package offline using yum
猜你喜欢

List of common ACM knowledge points (to be continued)

Alicloud development board vscode development environment setup
Ffmpeg Learning Guide

肝了一个月的原创小袁个人博客项目开源啦(博客基本功能都有,还包含后台管理)

Acwing: topology sequence
颜色编码格式介绍

【SemiDrive源码分析】【X9芯片启动流程】25 - MailBox 核间通信机制介绍(代码分析篇)之 RPMSG-IPCC RTOS & QNX篇

Dismantle and modify the advertising machine - Amateur decompression
![[wustctf2020] selfie score query -1](/img/90/e4c2882357e0a1c6a80f778887e3f5.png)
[wustctf2020] selfie score query -1

Greed issues - Egypt scores
随机推荐
Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform
开发中使用的语言技巧
GPUImage链式纹理的简单实现
[advanced MySQL] evolution of MySQL index data structure (IV)
280 weeks /2171 Take out the least number of magic beans
Return value of WaitForSingleObject
聊聊MySQL的10大经典错误
Mold and remainder
Qt5 plug-in production
AVFoundation
jupyternotebook有汉字数据库吗。在深度学习中可以识别手写中文吗
Time processing in C language (conversion between string and timestamp)
Implementing singleton mode of database under QT multithreading
Compile and install lamp architecture of WordPress and discuz for multi virtual hosts based on fastcgi mode
After reading the question, you will point to offer 16 Integer power of numeric value
Paw 高级使用指南
Simple implementation of gpuimage chain texture
Talk about the top 10 classic MySQL errors
Alibaba cloud development board haas510 parses serial port JSON data and sends attributes
618进入后半段,苹果占据高端市场,国产手机终于杀价竞争