We collected a bunch of shell scripting skills, I said, I write a blog mainly to make some study notes, to facilitate their access to,
so I would come up with such an article, there is no incomprehensible. About the origin of these skills, eh,
I forgot, it may come from theunixschool, commandlinefu, cool ground network and igigo.net, of course, there are some of my own ideas and experiences,
who cares, into my mind that I of the.
0. shell debugging
Copy the code code below: sh -x somefile.sh
Plus set + x set-x in somefile.sh file
1. && || simplified if else
Copy the code code below: gzip -t a.tar.gz
gzip -t a.tar.gz
if [[ 0 == $? ]]; then
echo “good zip”
else
echo “bad zip”
fi
Can be simplified to: Copy the code code below:
gzip -t a.tar.gz && echo “good zip” || echo “bad zip”
2 to determine the file is not empty
Copy the code following code:
if [[ -s $file ]]; then
echo “not empty”
fi
3 Get File Size
Copy the code code below:
stat -c %s $file
stat –printf=’%s\n’ $file
wc -c $file
4 string replacement
Copy the code following code:
${string//pattern/replacement}
a=’a,b,c’
echo ${a//,/ /}
5. Contains substring?
string=”My string”
if [[ $string == *My* ]]; then
echo “It’s there!”
fi
6. rsync backup
Copy the code code below:
rsync -r -t -v /source_folder /destination_folder
rsync -r -t -v /source_folder [user@host:/destination_folder
7 batch rename files
Txt file with all .bak suffix:
Copy the code code below:
rename ‘.txt’ ‘.txt.bak’ *.txt
Remove all bak suffix:
rename ‘*.bak’ ” *.bak
All the spaces into underscores:
Copy the code code below:
find path -type f -exec rename ‘s/ /_/g’ {} \;
The file names are changed to uppercase:
Copy the code code below:
find path -type f -exec rename ‘y/a-z/A-Z/’ {} \;
8. for / while loop
for ((i=0; i < 10; i++)); do echo $i; done
for line in $(cat a.txt); do echo $line; done
for f in *.txt; do echo $f; done
while read line ; do echo $line; done < a.txt
cat a.txt | while read line; do echo $line; done
9 delete blank lines
cat a.txt | sed -e ‘/^$/d’
(echo “abc”; echo “”; echo “ddd”;) | awk ‘{if (0 != NF) print $0;}’
10. compare file modification time
[[ file1.txt -nt file2.txt ]] && echo true || echo false
[[ file1.txt -ot file2.txt ]] && echo true || echo false
11. achieve Dictionary structure
Copy the code code below: hput () {
eval “hkey_ $ 1” = “$ 2”
}
hget () {
eval echo ‘$ {‘ “hkey_ $ 1” ‘}’
}
$ Hput k1 aaa
$ Hget k1
aaa
12 removed from the second row
Copy the code the code below:
$echo ‘a b c d e f’ | cut -d ‘ ‘ -f1,3-
$a c d e f
13 Save the stderr output to a variable
Copy the code the code below:
$ a=$( (echo ‘out’; echo ‘error’ 1>&2) 2>&1 1>/dev/null)
$ echo $a
error
14)
3 lines deleted before 14
$cat a.txt | sed 1,3d
15. read multiple domains into a variable
Copy the code the code below:
read a b c <<< “xxx yyy zzz”
16. iterate
Copy the code code below:
array=( one two three )
for i in ${array[@]}
do
echo $i
done
17 view directory size
Copy the code following code:
du –sh ~/apps
18 View CPU information
Copy the code following code:
cat /proc/cpuinfo
19. date
$ date +%Y-%m-%d
2012-12-24
$ date +%Y-%m-%d –date ‘-1 day’
2012-12-23
$ date +%Y-m-%d –date ‘Dec 25′
2011-12-25
$ date +%Y-m-%d –date ‘Dec 25 – 10 days’
2011-12-15
20. get the path and file name
Copy the code the code below:
$ dirname ‘/home/lalor/a.txt’
/home/lalor
$ basename ‘/home/lalor/a.txt’
a.txt
21. union and intersection
comm can be used to seek union, intersection, difference, assuming that there are now two documents a and b,
which reads as follows:
Copy the code following code:
$cat a
1
3
5
$cat b
3
4
5
6
7
$ cat b
3
4
5
6
7
$ comm a b
1
3
4
5
6
7
$ comm -1 -2 a b # intersection
3
5
$ comm a b | sed ‘s/\t//g’ # and set
1
2
3
4
5
6
7
$ comm -1 -3 a b | sed ‘s/\t//g’ # b-a
4
6
7
22. awk complex delimiter
Multi-character as the delimiter
Copy the code following code:
Multiple delimiters 1
Copy the code the code below:
$ echo “a||b||c||d” | awk -F ‘[|][|]’ ‘{print $3}’
c
Multiple delimiters 2
Copy the code following code:
$echo “a||b,#c d” | awk -F ‘[| ,#]+’ ‘{print $4}’
d
$echo “a||b##c|#d” | awk -F ‘([|][|])|([#][#])’ ‘{print $NF}’
c|#d
23 generates a random number
Copy the code the code below:
echo $ RANDOM
24. The mode split file
Copy the code code below:
csplit server.log /PATTERN/ -n 2 -s {*} -f server_result -b “%02d.log” -z
/ PATTERN / used to match a row, the segmentation process starts
{*} Based on the matching, segmentation is repeated
-s silent mode
filename suffix after -n split, the number of digits
file name prefix -f divided
Specify suffix format -b
25. get the file name or extension
Copy the code following code: var = hack.fun.book.txt
var=hack.fun.book.txt
echo ${var%.*}
hack.fun.book
echo ${var%%.*}
hack
echo ${var#.*}
fun.book.txt
echo ${var##.*}
txt
26. to the root account to perform on a command.
Copy the code the code below:
$ sudo !!
Among them: * !! refers to the previous command * The last parameter $ on a command parameter * on * all * of a command:!!! First three parameters of a command on 3
For example:
$ls /tmp/somedir
ls: cannot access /tmp/somedir: No such file or directory
$mkdir !$
mkdir /tmp/somedir
27. use python to build a simple Web server via http://$HOSTNAME:8000 visit.
Copy the code the code below:
python -m SimpleHTTPServer
28. in Vim without permission to save the file to edit.
Copy the code code below :w !sudo tee %
29. on a command replaces foo bar, and executed.
Copy the code code below: ^foo^bar
30. quickly backup or copy files.
Copy the code code below: cp filename{,.bak}
31. ssh keys will be copied to the user @ host to enable SSH login without a password.
Copy the code code below: $ssh-copy-id user@host
32. the linux desktop recording video.
Copy the code code below: ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg
33. man Magical
Copy the code code below:
man ascii
man test
34 on the vim to edit a command
Copy the code code below: fc
35. delete 0 byte files or junk files
Copy the code code below:
find . -type f -size 0 -delete
find . -type f -exec rm -rf {} \;
find . -type f -name “a.out” -exec rm -rf {} \;
find . type f -name “a.out” -delete
find . type f -name “*.txt” -print0 | xargs -0 rm -f
36 When writing SHELL display multiple lines of information
Copy the code the code below:
cat << EOF
+————————————————————–+
| === Welcome to Tunoff services === |
+————————————————————–+
EOF
Note that when you specify the terminator, it must be the only contents of the line, and the line must begin with this character.
37 How to build mysql soft link
Copy the code code below:
cd /usr/local/mysql/bin
for i in *
do ln /usr/local/mysql/bin/$i /usr/bin/$i
done
38 to obtain an IP address:
ifconfig eth0 |grep “inet addr:” |awk ‘{print $2}’|cut -c 6-
39 the number of open files
Copy the code code below: lsof
40. Clear zombie process
Copy the code code below:
ps -eal | awk ‘{ if ($2 == “Z”){ print $4}}’ | kill -9
41. print only lines
Copy the code code below:
awk ‘!a[$0]++’ file
42 print odd line
Copy the code code below:
awk ‘i=!i’ file
awk ‘NR%2’ file
43. print matching lines after a row
Copy the code code below:
seq 10 | awk ‘/4/{f=4};–f==0{print;exit}’
44 After printing a line 10 rows behind
cat file | grep -A100 string
cat file | grep -B100 string # front
cat file | grep -C100 string # before and after
sed -n ‘/string/,+100p’
awk ‘/string/{f=100}–f>=0’
45. get the last argument on the command line
Copy the code following code:
echo ${!#}
echo ${$#} #wrong attempt
46. ??output redirection
If you would like to you, STDERR and STDOUT output can be redirected to an output file,
therefore, bash provides special redirection symbols &>
ls file nofile &> /dev/null
How do we redirect script inside? Nothing special, and the general redirection same.
Copy the code following code:
#!/bin/bash
#redirecting output to different locations
echo “now redirecting all output to another location” &>/dev/null
The problem comes if we want to redirect all output to a file it?
We do not want to redirect the output every time about it, as the saying goes, Hermit own good ideas.
We can use exec to permanent redirect, as follows:
#!/bin/bash
#redirecting output to different locations
exec 2>testerror
echo “This is the start of the script”
echo “now redirecting all output to another location”
exec 1>testout
echo “This output should go to testout file”
echo “but this should go the the testerror file” >& 2
The output is as follows:
This is the start of the script
now redirecting all output to another location
lalor@lalor:~/temp$ cat testout
This output should go to testout file
lalor@lalor:~/temp$ cat testerror
but this should go the the testerror file
lalor@lalor:~/temp$
Additional ways to redirect:
Copy the code following code: exec 3 >> testout
Cancel redirection:
Copy the code following code: exec 3> –
47. function
Any variables are global variables defined elsewhere, if you want to define local variables need to add local keywords
the shell function can also be used recursively
Copy the code following code:
#!/bin/bash
function factorial {
if [[ $1 -eq 1 ]]; then
echo 1
else
local temp=$[ $1 – 1 ]
local result=`factorial $temp`
echo $[ $result * $1 ]
fi
}
result=`factorial 5`
echo $result
Create a library
The function of a set in another file, and then load the file into the current command by source
At the command line using the function
The function is defined in ~ / .bashrc in to
Passing arrays to functions
Copy the code the code below:
#!/bin/bash
#adding values in an array
function addarray {
local sum=0
local newarray
newarray=(`echo “$@”`)
for value in ${newarray[*]}
do
sum=$[ $sum+$value ]
done
echo $sum
}
myarray=(1 2 3 4 5)
echo “The original array is: ${myarray[*]}”
arg1=`echo ${myarray[*]}`
result=`addarray $arg1`
echo “The result is $result”
48. regex
Match ip address: ?d+.d+.d+.d+
Commentary: When extracting useful ip address
Match a specific number:
^[1-9]d*$ // match the positive integers
^-[1-9]d*$? // match negative integers
^-?[1-9]d*$? // match integers
^[1-9]d*|0$ // match non-negative integer (positive integer + 0)
^-[1-9]d*|0$? // matching non-positive integers (negative integer + 0)
^[1-9]d*.d*|0.d*[1-9]d*$ // match float
^-([1-9]d*.d*|0.d*[1-9]d*)$ // match negative float
^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$ // match float
^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$ // match non-negative floating point numbers (positive float + 0)
^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$? // matching non-float (negative float + 0 )
Commentary: When handling large amounts of data useful to note that amendments to the specific application
Match a specific string:
^[A-Za-z]+$ // match a string of 26 English letters
^[A-Z]+$ // match by 26 uppercase letters of the alphabet composed of a string
^[a-z]+$ // match a string of 26 lowercase letters of the alphabet consisting of
^[A-Za-z0-9]+$ // match the string of numbers and 26 letters of the English
^ w + $ // matching string by numbers, 26 English letters or underscores the
Recent Comments