当前位置:网站首页>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

 Linux shell How does the script reverse search a specific string from the end of the file ( It only matches once )_360 Question and answer

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
    1. 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

原网站

版权声明
本文为[123axj]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010514261577.html