Reading notes of Linux shell script introduction


For more interesting content, please pay attention to WeChat official account:Back end technology cabin

Introduction to Linux shell script is a book suitable for beginners to systematically learn bash shell. Wall crack is recommended. The following are my notes on reading this book, hoping to be useful to readers.

  1. Output color character
Echo - e "\ E [1:41m" # 1 indicates the background color
  1. Echo print! Need escape
echo "hello, bash!"  #  Report an error because! It is a special character in the shell and needs to be escaped
echo "hello, bash\!" #  No error,! Escaped
echo 'hello, bash!'  #  No error, in single quotation marks! No transfer required
  1. Printf can be used to format the output
#The usage is similar to ` printf in C`
printf "format"  var1 var2 ...
  1. Echo common options
Echo - n # does not add a newline character to the end of the string

Echo - e # supports escape character representation
Echo - e "\ E [ID XXXXXX" # displays the color font, where id represents the background color ID
  1. pgrep
#Get process ID based on process name
Pgrep # is equivalent to PS - EF  

Cat / proc / $PID / environ # view environment variables during process running
  1. Arithmetic operation with (())
#There are two ways to calculate the sum of two numbers:
C = $((a + b)) # assignment method 1
((C = a + b)) # assignment method 2
  1. Output redirection
#Redirect standard output and standard error output to run.log
./binary >run.log 2>&1 

#More concise: note that there should be no spaces between & and > 
sh &>run.log
  1. Create file descriptor with Exec
exec 4 < log.txt
exec 5 > log.txt
exec 6 >> log.txt
  1. Hash array
declare -a map 


#Get all keys
declare -a HTTP_RESPONSE=(
        [400]="Bad Request"
        [404]="Not Found"
        [405]="Method Not Allowed"
        [500]="Internal Server Error"
  1. Date get time
#Display seconds
date +%s 

#Convert seconds to string
Date - D @ seconds

11 tput control terminal cursor

#Set cursor position
Tput cup line number column number

#Save cursor position
tput sc 

#Return to the saved cursor position
tput rc

#Clear the current cursor to the end of the line
tput ed
  1. Read read standard input
#Set no echo
read -s
#Set timeout 
read -t 
#Sets the number of bytes read in
Read - N bytes
#Set prompt
read -p "please input passwd"
  1. Cat display file content
#Ignore extra blank lines when displaying 
cat -s file

#Displays the line number of each line
cat -n file 

#Print out tabs
cat -T file
  1. File search with find
#Print files or directories that match the search criteria
find  -name XXX  -print 

#Delete eligible files or directories
find   -name XXX -exec rm -rf {} \;
  1. Xargs: import assembly commands from standard
#Common usage
cat file | xargs

#Sets the number of fields per row
cat file | xargs -n 3 

#Newline with null
cat file | xargs -0

#Replace position in command
cat file | xargs -I{} rm -rf {}
  1. Tr: replace or delete standard input characters
TR character set 1 character set 2 

TR - D character set

TR - D - C character set to leave

#Remove duplicate spaces
tr -s " "
  1. Md5sum: generate MD5 checksum for file
#Generate MD5 value
md5sum file  > file.md5

md5sum -c file.md5
  1. Sort: sort the contents of the file
#Sort by string 
cat file | sort 

#Sort by number
cat file | sort -n 

#Reverse sort
cat file | sort -r

#Sort by column 
Cat file | sort - K number of columns
  1. DD: copy and convert files
#Generate file from / dev / zero 
dd if=/dev/zero of=data.file bs=100k count=1
  1. Split: split large files
#Divide the file into small files with a size of 10K, and the suffix length in the name of small files is 3
split data.file -b 10k -a 3
  1. String segmentation
#Get file suffix
echo ${filename##*.}

#Get file name
echo ${filename%.*}
  1. Word list
cat /usr/share/dict/words
  1. Expect: automated interactive input
# expect 
spawn  ./ 
expect "Password:"
send "XXX\n"
  1. Comm: find the intersection / difference / Union of two files
#Difference set a-b
comm a.txt b.txt -1

#Find a + B Union
comm a.txt b.txt 

#Find AB intersection
comm a.txt b.txt -1 -2
  1. Chatr: modify file properties
#Make the file non modifiable
chattr +i file
#Remove non modifiable attributes
chattr -i file
  1. Find soft connection
#Method 1
ll -rt | grep -P "^l" | awk '{print $8}'

#Method 2
find . -type l
  1. Diff: compare two files (or directories)
diff -abru 1.txt 2.txt 
diff -abru 1.txt 2.txt > 12.patch
  1. Patch: patch files
patch -p1 1.txt < 12.patch
  1. Command line current directory management
#Pushd: push the current directory onto the stack
#POPD: pop up the directory from the stack and use it as the current directory
#Dirs: displays the directory in the stack

#General usage
pushd  directory

#Specify directory
pushd +N
popd +N
  1. Grep: search file contents
#Matching content display color
grep --color=auto 

#Count the number of matching rows
grep -c 

#Show matching line numbers
grep -n 

#Displays the offset of the matching character
grep -o -b

#Displays a list of matching files
grep -l  1.txt 2.txt

#Use file matching
grep -f pattern.txt  file.txt

#Silent mode
grep -q 

#Location code
grep -R -n 

#Ignore case
grep -i 

#Match multiple styles
grep -e pattern1 -e pattern2

#Show n rows before matching row
grep -A 

#Show n rows after matching rows
grep -B 

#Show n rows before and after matching rows
grep -C
  1. Sed: text filtering and conversion tool
#Sed "s // /" replace
echo  "hello wolrd" | sed "s/hello/goodbye/"

#Sed - I replace file contents
sed -i 's/hello/goodbye/' 1.txt

#Sed's // / g 'replace all matches
echo "thisthisthisthis" | sed 's/this/THIS/g'

#Sed's // / ng 'Replace from match n
echo "thisthisthisthis" | sed 's/this/THIS/2g'

#Sed '// D' delete matching line
cat | sed '/^$/d' 

#Match string tag
echo this is an example | sed 's/\w\+/[&]/g'

#Capture string
echo "this is a digit 7 in a numbger" | sed 's/digit \([0-9]\)//'
  1. Paste: merge files
#Merge files by column
paste file1 file2
paste file1 file2 -d  ","
  1. Rev: string output in reverse order
echo "1234" | rev
  1. TAC: output in reverse order for multiline files
#Row reverse order
seq 1 10 | tac
  1. Set command line editing mode
set -o vi

Recommended reading

More interesting content, please scan code concern WeChat official account:Back end technology cabin。 If you think the article is helpful to you, please share, forward and read it.

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]