Link of Lecture

Lecture 1 Shell

$PATH

  1. all of the path in the machine the shell will look for program
  2. which echo: will tell me which echo is gonna run

Permission

  • Read Permission on directory
    To list the content of directory

  • Write Permission on directory
    rename create or remove files within this directory
    if you have write permission on file but not on it’s directory
    you can empty the file, but you can’t delete it

  • Execute permission on directory
    it is known as search permission, are you allow to enter this directory
    If you want to open/read/write a file, you must have execute permisison on all it’s parent directories

  • Example one
    if shell didn’t have write permission on file abc
    then even if you run the “sudo echo 500 > abc” you will still get permission denied
    and you can run it as: “echo 500 | sudo tee abc”

single quote & double quote

1
2
3
foo=bar
echo "Value is $foo" //foo will be replaced
echo 'Value is $foo' //foo won't be replaced'

Lecture 2 Scripting

parameter of shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mcd(){
mkdir -p "$1"
cd "$1"
}
// $0 is the name of file
// $? will get the error code from previous command, and zero means ok
// $_ will get the last argument of the previous command
// $# the number of parameters
// $$ the process id of this command
// $@ is the whole parameter stored
for file in "$@"; do
grep foobar "$file" > /dev/null
if [["$?" -ne 0 ]]; then
echo "File $file does not have any foobar, adding one"
echo "# foobar" >> "$file"
fi
done

get output of command into variable

1
foo=$(pwd)

file evaluation

1
2
3
4
5
6
7
-b file True if file exists and is a block special file
-c file True if file exists and is a character special file
-d file True if file exists and is a drectory
-e file True if file exists and regardless of type
-f file True if file exists and is a regular file
-g file True if file exists and its set group ID flag is set
-h file True if file exists and is a symbolic link

globbing

project* will match project1 project12 project 123 and so on
,and project? will only expand one char

first line of shell

1
#!/usr/bin/env python

to tell the shell what env to trigger

operator

1
2
3
4
5
false || echo "this will be printed"
true || echo "this won't be printed"
// the second one will be excuted only if the first one is false
// and && is the opposite
// use ; it will always print

Find

1
2
3
find . -path '**/test/*.py' -type f
find . -mtime -1 // been modified in the last day
find . -name "*.tmp" -exec rm {} \

reverse searching

click Ctrl+R and type the command you want to search for,in the same time you can click Ctrl+R too
and prints all the command that matches

string operation

  1. Get Length

    1
    2
    3
    string="abcd"
    echo ${#string} #print 4
    length=${array_name[@]} # print length of array_name
  2. Extraction

    1
    2
    string="runoob is a site"
    echo ${string:1:4} # print unoo

equation

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
-eq	检测两个数是否相等,相等则返回true
-ne 检测两个数是否不等,不等则返回true
-gt 检测左边的是否大于右边的,大于则返回true
-lt 检测左边的是否小于右边的,小于则返回true
-ge 检测左边的是否大于等于右边的,大于等于则返回true
-le 检测左边的是否小于等于右边的,小于等于则返回true
! 非运算
-o 或运算
-a 与运算

# 数值测试
num1=100
num2=200
if test $[num1] -eq $[num2]
then
echo '两个数相等'
else
echo '两个数不相等'
fi

# 文件测试
cd /bin
if test -e ./bash
then
echo '文件存在'
else
echo '文件不存在'
fi

flow control

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
for var in item1 item2 ... itemN
do
command1
command2
commandN
done

int=1
while [ $int -lt 5 ]
do
echo $int
let "int++" # let是bash中用于计算的工具,变量计算中不需要加上$表示变量
done
# 运行结果
# 1
# 2
# 3
# 4
# 5

echo '输入1到4之间的数字'
echo '你输入的数字为'
read aNum
case $aNum in
1) echo '你选择了1'
;;
2) echo '你选择了2'
;;
3) echo '你选择了3'
;;
4) echo '你选择了4'
;;
*) echo '你没有输入1到4之间的任何数字'
;;
esac

while true
do
echo -n "输入1到5之间的数字"
read aNum
case $aNum in
1|2|3|4|5) echo "你输入的数字为$aNum!"
;;
*) echo "你输入的数字不是1到5之间的!结束"
break
;;
esac
done