all of the path in the machine the shell will look for program
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
-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