0%

关于Unix的笔记

增加了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
2
3
4
5
6
7
8
9
## input和output使用>和<

cat < in.txt > out.txt

## 从in.txt用cat读取数据,输出到out.txt中

## 同时>>是append的意思

cat >> out

Every linux process has 3 streams, std in(standard input), std err(standard error), std out(standard output).

Why have a separate error stream

  1. separate expected output from unexpected output(error)
  2. stderr id NOT buffered
1
2
3
4
5
6
7
8
9
comd < in.txt > out.txt 2>err.log
comd > out.txt 2>&1

## 这里 2> 是std err 而&1 是std out,也就是coonect steam 2 to 1.
## 注意,这跟

comd 2>&1 > out.txt

## 不一样.

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”


一种快捷链接的意义.
可以把文件所在的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
2
kill PID
kill -9 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
2
nano sample.txt
cat !$

第二行的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
2
3
4
prog1 | prog 2
## 等于

prog1 > temp 和 prog2 < temp

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
#!/bin/bash

这一行叫做shebang line.
这一行必须是第一行顶头.#!叫做bang.
/bin/bash是the path to executable that will run your script.


Run

  1. Assign execute permission, ensure stored in a directory within $PATH, and call it at command line:
     **listFiles**
    
  2. Execute script in the current bash shell using the dot. Not recommended if script contains the exit command.
     **. listFiles**
    
  3. 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
2
name=variable
echo $name

Bash里的variable在命名时不可以有空格.
Name cannot start with a number
用variable的时候要加$ sign.

1
2
3
To pass an argument to the bash script, we use position parameters $1, $2, $3.

echo "Today is $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
2
3
4
5
#!/bin/bash

echo "Enter username : "
read nameVar
echo "Your username is $nameVar"

Bash里面还有array,牛逼.

1
2
3
4
5
#!/bin/bash
names[0]="Bob"
names[1]="Julie"
names[2]="Andrew"
names[3]="Alice"
1
2
#!/bin/bash
names=("Bob" "Julie" "Andrew" "Alice")

We can access the entire array using *.


Arithmetic

We can use $((mathExpression)) or $[mathExpression].

1
2
3
4
5
6
7
8
9
10
echo $((4/2))
echo $[3+2*5]
result=$[3*5]
echo $result
num1=7
num2=2
num3=4
echo $[ ($num1-$num2) * $num3 ]

expr 4 + 7

Bash support integer arithmetic only, but we can use bc command for support of decimal places.

1
2
3
4
5
result1=$(echo "scale=3; 8/3" | bc)
echo $result1

result2=$(echo "scale=3; (8/3) ^ 3" | bc)
echo $result2

Function

1
2
3
4
5
6
7
8
#!/bin/bash
report(){
if [ $1 -eq 31 ]; then
echo "This month is the 31st."
else
echo "This month is the ${1}th"
fi
}
1
2
3
4
5
6
7
function functionName(){
code
}
# or
functionName(){
code
}

Pass arguments.

1
2
3
4
5
6
7
8
9
#!/bin/bash
 
function displayUserDetails() {
echo "Your name is $1 ."
echo "You are $2 years old. "
}

displayUserDetails Alice 25
displayUserDetails Bob 32

Use echo to return a value from a function.

1
2
3
4
5
6
7
8
#!/bin/bash

function getName(){
read -p "Enter your name : " name
echo $name
}

echo "Your name is $(getName)"

By default function variables are global and available for use outside the function.
但是也可以单独define local的variable!


1
2
3
4
5
6
7
function cube() {
x=$[ $1 * $1 * $1 ]
local y=123
}
cube 3
echo "The cube of 3 is $x."
echo "The value of y is : $y."

The cube of 3 is 27.
The value of y is : .

Condition

1
2
3
4
if command
then
code
fi
1
2
3
if command ; then
code
fi

Both ok!

Else if statement.

1
2
3
4
5
6
7
8
9
10
11
12
if command1
then
code
elif command2
then
code
elif commandN
then
code
else
code
fi

Case statement.

1
2
3
4
5
6
7
8
9
10
case value in
pattern1)
code ;;
pattern2)
code ;;
patternN)
code ;;
*)
code ;;
esac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
cat<<eof
ATM
-------------------
1. Deposit
2. Withdraw
-------------------
eof
read -p "Enter an option : " optVar

case $optVar in
1) echo "Deposit option was selected." ;;
2) echo "Withdraw option was selected." ;;
*) echo "Invalid option." ;;
esac


1
2
3
if [ 5 –gt 3 ]
then echo $?
fi

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for i in 1 2 3 4 5 6 7 8 9 10
do
echo $i
done
# or
for ((i=1;i<=10;i++))
do
echo $i
done
# or
for i in $*
do
echo $i
done
# or
for file in $(ls)
do
echo $file
done

While loop.

1
2
3
4
5
6
7
8
while [ condition ]
do
command
done

while [ condition ] ; do
command
done

Input redirection and a while loop can also be combined to read a file line by line.

1
2
3
4
5
6
7
8
9
#!/bin/bash
# script reads line by line the file that was passed
# as a command line argument

while read line
do
echo $line
sleep 0.5
done < $1

Until loop.

1
2
3
4
5
6
7
8
until [ condition ]
do
command
done
# or
until [ condition ] ; do
command
done


Exit



Parameters

详情可以看cheatsheet.

1
2
${1} 是这个script中第一个argument,以此类推 ${2} 是第二个.
也就是说如果被赋值, ${1} 是最先的收到值的.

定义variable的用法,
Rename all .cpp to .cc

1
2
3
4
5
6
7
8
举例:
mv Hello.cpp Hello.cc
(这里mv用来重命名)

filename=Hello.cpp
mv ${filename} ${filename%pp}c

%是drop的意思,在后面直接加上要加的内容“c”.

Condition

shell里的else if,注意,这里的空格相当重要.

1
2
3
4
5
6
7
if [ condition ]; then
...
elif [ condition ]; then
...
else
...
fi

fi是结束else if的意思.

Loop

1
2
3
while [ condition ]; do
...
done



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
2
3
4
chmod u+x dir3/filename
chmod o-r dir3
chmod ug+x,o-w file1
chmod a=rw file2
Octal Text Meaning
4 r Read
2 w Write
1 x Execute
0 No permissions
1
2
chmod 751 dir3/filename
chmod 750 dir3


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