-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsh.txt
105 lines (91 loc) · 2.61 KB
/
sh.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# bash: the default shell in linux
# change the default shell
$ chsh <username> -s /bin/sh
# output to null
$ <command> >/dev/null
# redirect stderr 2 to stdout
$ <command> 2>&1
# examples:
$ cat <filename> > output.txt 2>&1
$ cat <filename> &> output.txt
> # rediect and overwrite
>> # redirect and append
# set time to seconds in UTC
$ starttime=$(date +%s)
# output from last command
$?
# all params
$@ # $* all params separated by space
# the number of arguments, or the number of positional parameters passed to the script, shell or shell function
$#
# Grammar
# During the start command like ./start.sh, the following sentence will give the value "./" to SDIR
SDIR=$(dirname "$0")
# assign the default value
: ${A:=value}
# Useful tools
# tr command is used to translate, for instance if I want to translate upper case to lower case, the txt file cantains string HELLOWORLD
cat helloworld.txt | tr "[A-Z]" "[a-z]" # will output helloworld
# sed
# -i option(-it for darwin): change the content directly w/o printing on console, otherwise it will just print on console and will not change the original file
# general usage
$ sed -i "s/<beReplacedString>/<replaceString>/g" <filaPath>
# w/o g, it will only change if the beReplacedString shows up at head of each line
# awk
# for more info about powerful "sed" and "awk", refer https://blog.csdn.net/gv7lzb0y87u7c/article/details/80490997
# shell if statements
# basic usages
if [ condition ]; then
commands
elif [ condition ]; then
commands
else
commands
fi
# check if the first arg is an empty string, where ! means 'not', -z meanns empty
if [ ! -z $1 ]; then
# check the arguments number equals 3
if [ $# -eq 3 ]; then
# check if the directory exists
if [ -d "<filePath>" ]; then
# comparison
$ -lt # less than
$ -ne # not equal
# AND/OR
$ -o # or
# -a # and
# case usages
case ${VAR} in
pattern1)
command1
;;
pattern2)
command2
;;
esac
# while
i=1
sum=0
while [ $i -le 100 ]
do
let sum=sum+$i
let i++
done
# until
until [ ! $i -lt 100 ]
do
let sum=sum+$i
let i++
done
# some usages
# get the OS ARCH(My laptop will have the value of darwin-amd64)
$ OS_ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')" | awk '{print tolower($0)}')
# replace the first occurance of a pattern with a given string, use $(parameter/pattern/string}
# replace all occurances of a pattern, use $(parameter//pattern/string}
$ echo $(STRING// /$';'}
# array definition
arr=("a" "b" "c")
# array length
${#arr[@]}
# set domain to ip address
$ vim /etc/hosts # then type the mapping