增加了cs246课堂笔记的补丁.
Also known as bash.
Command line shortcuts.
up/down arrows: review previous command
ctrl + a: move cursor to start of line
ctrl + e: move cursor to end of line
tab: try to complete the command or filename
Bash
man + theCommand 可以查看command说明.
uname用来check Unix version.
Input
1 | ## input和output使用>和< |
Every linux process has 3 streams, std in(standard input), std err(standard error), std out(standard output).
Why have a separate error stream
- separate expected output from unexpected output(error)
- stderr id NOT buffered
1 | comd < in.txt > out.txt 2>err.log |
dev/null则是一个黑洞directory,可以用来skip output掉不需要的东西.
2>&1 redirect stderr to stdout.
Text editors
- ed: edit text
- vi: visual editing mode
- vim: vi improved
- emacs: editor macros
- pico: pine composer
- nano: 1000 times larger than pico
在学校里学的是vi,用vi写了很久的代码,从bash写到cpp.
结果被飞哥震惊:你怎么不用code editor.
谢谢教我.
cd
cd路径.
1 | cd /User/document |
cd后面斜杠是绝对路径.
1 | cd ~ |
Back to home.
1 | cd - |
Last directory you were in.
Find
Grep
Search file content.
1 | grep [-options] pattern filename |
grep –E(egrep)
search for string or pattern based on extended regular expressions
grep –F(fgrep)
search for string data, ignoring the special meaning of any characters
Difference between egrep and grep:
The escape character rules are different.
Grep用于查询file里面是否有同样pattern的内容.
如果要查file类型,用string command.
Reading files
- cat
- more : can get paginated output
- less : less replaced more, but less is more than more! (better memory use)
cat是学校里学过的,用来读取整个文件信息.
less更像是用来阅读的,可以选择部分文件,并上下移动.
用space or F key can read to the next page. B to the back page. H for help menu. Q for exit.
Overwriting
- mv -f : force overwriting (default)
- mv -n : no overwriting
- mv -i : interactive overwriting, “ask me”
- cp -R : recursive copy directories
- cp -f : force overwriting (default if empty)
- cp -n : no overwriting
- cp -i : interactive overwriting, “ask me”
Symlink
一种快捷链接的意义.
可以把文件所在的directory存成一个快捷链接.
1 | ln theDirectory linkName |
要给directory instead of file 做symlink,有一点点不同.
1 | ln -s theDirectory linkName |
Kill a process
一般我会使用ctrl+c来终止一个程序,但这是属于还可以对着那个程序操作的情况.
由此run hexo,把窗口关掉了,就没法在对着那个在跑的hexo来终止它(找不到了嘛).
可以top(所有),ps(现在在run的)来check有什么process在running.
同时跑的程序也有PID,process ID.
ps –f full list of all your processes
jobs –l long list of background and stopped processes
知道PID后可以用kill command来终止程序.
1 | kill PID |
有时用9有时不需要.
Header One | Header Two |
---|---|
kill pid | terminated and removed from memory |
kill -9 pid | forcefully terminated |
kill -19 pid | pauses, or stops a background process |
kill -18 pid | restarts a stopped process into the background |
ctrl + Z pauses, or stops, the foreground process.
nohup prevents a background process from aborting when you exit the shell.
1 | nohup batchprog > bpresults 2> bperrors & |
History
bash里可以查看history command.
1 | history |
同时也有command前的id.
1 | !commandID |
可以跑历史里的command.
还有种快捷跑history command的方法.
1 | !cat |
会跑出上一次使用cat的情况,比如cat sample.txt
也就是说忘记pass给cat的argument是什么也能再跑一次.
还有$会store上一次的argument.
1 | nano sample.txt |
第二行的cat可以默认成cat sample.txt
- !3 : ref history cmmd #3
- !-2 : ref cmmd which was two cmmd back
- !cat : ref recent cmmd beginning with “cat”
- !! : ref prev cmmd
- !$ : ref prev cmmd arguments
1 | sudo !! |
Do the last command, but with sudo.
Unix file system
- /bin : command/programs
- /etc : configurations
- /home : user home directories
- /lib : system libraries
- /tmp : temporary files
- /usr : unix system resources
- /var : variable system data files
Pipes
Connect the output(stdout) of one program to the input(stdin) of anothers.
(可以当作sql的nested querry来理解.)
1 | prog1 | prog 2 |
Example: Count the # of words in the first 20 lines of sample.txt.
1 | head -20 sample.txt | wc - w |
Example: Suppose files words1.txt, words2.txt, etc. Contain words one per line. Print a dulicate free list if all words that occur in any of these files.
1 | cat words*.txt | sort | uniq |
Uniq remove the duplicate that adjcent to each other.
Use the output of a program as an argument to another program.
1 | echo "Today is $(date) and I am $(whoami)" |
Use $(command) to enable commands.
1 | echo 'Today is $(date)' |
This won’t work.
Single quote will suppress embedded commands.
这里更多syntax可以查看Cheatsheet中的Patterns.
Corn[https://en.wikipedia.org/wiki/Cron]
比较advance的 corn job command.
可以设定每天运行.
Script
Naming
- 255 char maximum
- Use A-Z, a-z, 0-9, underscore, hyphen, period
- Avoid \ / * & % ? | ^ ~ > < and other symbols
- Case sensitive
- Prefer underscores over spaces
- Escape spaces with \
- Use quotes around names with spaces
Coding
Shebang
如果要写shell script必须要加
1 |
这一行叫做shebang line.
这一行必须是第一行顶头.#!叫做bang.
/bin/bash是the path to executable that will run your script.
Run
- Assign execute permission, ensure stored in a directory within $PATH, and call it at command line:
**listFiles**
- Execute script in the current bash shell using the dot. Not recommended if script contains the exit command.
**. listFiles**
- Execute script in a bash sub-shell with bash command.
**bash listFiles**
Debug
- Add echo commands to display the variable value
- Split code into chunks and test before moving on
- Run script in trace mode:
1
bash -x listFiles
Variable
1 | name=variable |
Bash里的variable在命名时不可以有空格.
Name cannot start with a number
用variable的时候要加$ sign.
1 | To pass an argument to the bash script, we use position parameters $1, $2, $3. |
Header One | Header Two |
---|---|
$0 | script name |
$# | # of command line parameters |
$* | all command line parameters |
$@ | all command line arguments |
$? | exit status of the most recently executed command |
$$ | Process ID of the shell |
$! | process ID of the most recently executed background command |
Can also use read to read input.
1 |
|
Bash里面还有array,牛逼.
1 |
|
1 |
|
We can access the entire array using *.
Arithmetic
We can use $((mathExpression)) or $[mathExpression].
1 | echo $((4/2)) |
Bash support integer arithmetic only, but we can use bc command for support of decimal places.
1 | result1=$(echo "scale=3; 8/3" | bc) |
Function
1 |
|
1 | function functionName(){ |
Pass arguments.
1 |
|
Use echo to return a value from a function.
1 |
|
By default function variables are global and available for use outside the function.
但是也可以单独define local的variable!
1 | function cube() { |
The cube of 3 is 27.
The value of y is : .
Condition
1 | if command |
1 | if command ; then |
Both ok!
Else if statement.
1 | if command1 |
Case statement.
1 | case value in |
1 |
|
1 | if [ 5 –gt 3 ] |
Spaces are required.
-e name File name exists.
-d name File name is a directory.
-f name File name is a regular file.
-h name File name is a symbolic link.
-r name File name exists and is readable.
-w name File name exists and is writeable.
-x name File name exists and is executable.
-s name File name exists and has nonzero size.
s1 = s2 String s1 equals string s2
s1 != s2 String s1 does not equal string s2
-z s1 String s1 has zero length
-n s1 String s1 has nonzero length
n1 –eq n2 Integer n1 and n2 are equal
n1 –ne n2 Integer n1 and n2 are not equal
n1 –lt n2 Integer n1 is less than n2
n1 –le n2 Integer n1 is less than or equal to n2
n1 –gt n2 Integer n1 is greater than n2
n1 –ge n2 Integer n1 is greater than or equal to n2
You can combine more than one expression by using conditional operators.
! t1 Negate operator. Negate the test
t1 II t2 OR operator. Either test t1 or test t2 are true
t1 –o t2 Shell syntax of OR
t1 && t2 AND operator. Both test t1 and test t2 are true
t1 –a t2 Shell syntax of AND
Loop
For loop.
1 | for i in 1 2 3 4 5 6 7 8 9 10 |
While loop.
1 | while [ condition ] |
Input redirection and a while loop can also be combined to read a file line by line.
1 |
|
Until loop.
1 | until [ condition ] |
Exit
Parameters
详情可以看cheatsheet.
1 | ${1} 是这个script中第一个argument,以此类推 ${2} 是第二个. |
定义variable的用法,
Rename all .cpp to .cc
1 | 举例: |
Condition
shell里的else if,注意,这里的空格相当重要.
1 | if [ condition ]; then |
fi是结束else if的意思.
Loop
1 | while [ condition ]; do |
CheatSheet
感谢cs246教授赞助的linux command summary.
Commands
Command | Meaning | Options |
---|---|---|
exit | log out | |
passwd | change your password | |
clear | clear screen | |
man command | show the manual page for command | man -k word show a list of man pages that mention word |
history | show all previously-issued commands | |
!! | execute most recently-issued command | |
!c | execute most recently-issued command starting with c | |
whoami | display your login name | |
date | display current date and time | |
pwd | display current directory | |
ls | list contents of current directory | ls -a show all files, including hidden files |
ls -l show in long format | ||
cp file1 file2 | copy file1 to file2 | cp -R dir1 dir2 recursively copy dir1 to dir2(including all the files in one dir) |
mv file1 file2 | move file1 to file2 (also use to rename) | |
rm file | remove file | can be used to recursively remove a directory, if -r option is used |
touch file | update file’s last modified time to current time | can be used to create an empty file if file does not exist |
cd dir | change directory to dir | cd - return to most recently visited directory |
mkdir dir | create new directory dir in current directory | mkdir - p can specify more than one directory at once(including parent directory, like mkdir -p project1/project2) |
rmdir dir | remove directory dir | only works if dir is empty; if not empty, use rm -r dir ; can specify more than directory at once |
echo string | display string to screen | |
chmod perms file | set permissions on file to perms | |
chfn | change personal info (name, address, etc.) on Unix system | |
ps | display current processes | ps -a show all users’ processes |
ps -A show ALL processes (incl. system processes) | ||
kill pid | kill process with number pid | kill -9 pid more forceful kill, for stubborn processes |
who | show who is logged into this machine | |
finger username | show personal info for username | |
time command | show amount of time taken executing command | |
fg | bring background job to the foreground | useful if you accidentally ran vi or emacs with an & |
find dir -name “pattern” | find all files whose names match pattern in dir and its subdirectories |
Tools
Tool | Purpose | Options |
---|---|---|
cat f1 f2 … | display files f1, f2, … one after the other | cat -n f1 f2 … attaches line numbers |
more file | cdisplay file one screen at a time | |
diff f1 f2 | compare files f1 and f2 ; outputs instructions for converting f1 to f2 | diff -w f1 f2 ignores whitespace |
cmp f1 f2 | compare files f1 and f2 ; outputs the first position where they differ | |
wc file | count the number of words, lines, and characters in file | wc -c file show just the number of characters |
wc -l file show just the number of lines | ||
wc -w file show just the number of words | ||
egrep pat file | print all lines in file that contain pattern pat | egrep -n pat file print matching lines with line numbers |
egrep -v pat file print lines that do not match pat | ||
head file | print first 10 lines of file | -num prints num lines (e.g. head -5 file) |
tail file | like head, but prints last 10 lines of file | |
sort file | sorts the lines of file | sort -n file sorts strings of digits in numerical order |
uniq file | removes consecutive duplicate lines from file | removes all duplicates if file is sorted |
Programs
Program | Purpose | Options |
---|---|---|
vi file | invoke vi text editor on file | |
emacs file | invoke emacs text editor on file | |
pico file | invoke pico text editor on file | |
pine (or alpine) | read email | |
wget url | fetch file from the web at url | |
xpdf file | display pdf file | |
lpr file | print file to printer | lpr -Pljp 3016 file specifies the printers in MC3016 |
lpq | checks the print queue | |
lprm jobno | removes job jobno (must belong to you!) from print queue | |
ssh machine | make SSH connection to machine; opens a secure shell on remote machine; type exit to end SSH connection | ssh -Y (or -X) machine enable X forwarding (must have X server running on local machine) |
scp mach1 :file1 mach2 :file2 | securely copy file1 on mach1 to file2 on mach2 | can omit mach1 if it is the local machine; similarly for mach2 |
Permissions
Permission can be changed with chmod.
How to read permission:
Symbol | Meaning |
---|---|
u | file’s owner |
g | members of the file’s group, other than the owner |
o | other users |
a | all users (equivalent to ugo) |
+ | add permission bit |
− | revoke permission bit |
= | set permission bits exactly |
r | read permission. for files—file’s contents can be read. for directories—directory’s contents can be listed |
w | write permission. for files—file’s contents can be modified. for directories—files can be added/renamed/removed in the directory |
x | execute permission. for files—file may be executed as a program or script. for directories—directory can be traversed (i.e. can cd into the directory) |
1 | chmod u+x dir3/filename |
Octal | Text | Meaning |
---|---|---|
4 | r | Read |
2 | w | Write |
1 | x | Execute |
0 | No permissions |
1 | chmod 751 dir3/filename |
Script Conditional Operators
Operator | Meaning |
---|---|
= | string equality |
!= | string inequality |
-eq | integer equality |
-ne | integer inequality |
-gt | integer greater than |
-ge | integer greater than or equal to |
-lt | integer less than |
-le | integer less than or equal to |
-a | and |
-o | or |
! | not |
\ ( , \ ) | parentheses for grouping |
-d | file exists and is a directory |
-e | file exists |
-f | file exists and is a regular file |
-r | file exists and is readable |
-w | file exists and is writable |
-x | file exists and is executable |
Globbing Patterns(Wildcards)
Operator | Meaning |
---|---|
* | matches 0 or more characters |
? | matches one character |
[abxy] | matches exactly one of the characters in brackets |
[!abxy] | matches any character except the ones in the brackets |
[a-z] | matches any character in the given range |
{pat1,pat2} | matches either pat1 or pat2 (technically not a glob; note no spaces) |
Regular expression
Directories
Directory | Meaning |
---|---|
. | Current directory |
.. | Parent of current directory |
~ | Your home directory |
/ | Root directory |
Starts with / or ~ | Absolute path |
Does not start with / or ~ | Relative path |
Variable
The exit command terminates the script and passes an exit status to the shell.
By convention, scripts should return 0 for success, and 1 or any non-zero value upon failure.
exit command with a non-zero exit status is useful for terminating a script when normal processing cannot continue.
If exit command is not used, the exit status of the script will be the exit status returned by the last command in the script