2017年3月12日 星期日

Bash Shell

前陣子發現學校有提供這個課程,這是個兩天的課,早上九點到下午四點,學生和博士後才 CAD$25,加手續費也不過 CAD$27,而且還有附中餐,真是佛心來著的。



Software Carpentry: Programming in Python, The Unix Shell, Version Control with Git

這個課程主要是教 Bash Shell, Python 和 Git,課程大綱和需要安裝的東西看這裡,Mac OS 只要裝 Python 就好了,其他都是內建的,這就是為什麼很多人說 Mac 比較適合用來寫程式的原因嗎?XD

如果沒學過這幾個程式,想要學些入門的話,很建議拿這個課程。後來發現美、加很多學校都有,有興趣的可以看自己的學校有沒有提供,只是我不知道其他學校的上課情形是怎樣。我覺得 UBC 的很好,課程中助手很多可以隨時提供幫助,而且有配合用 Etherpad,雖然講得有點快,但也不致於跟不上。這個入門課程幫你上手之後,其他你就可以自己上網查語言要怎麼用,然後就可以自己玩了。XD

先講一下心得好了,Bash Shell 和 Git 主要用來編輯和整理檔案,如果用 Mac 的話,兩個都是直接從 Terminal 下指令。Git 的話可以和 GitHub 連結,然後也可以從 Terminal 下指令改 GitHub 上的內容,把檔案放在 GitHub 上的話,主要就是可以和人共筆,你可以改別人的檔案內容,也可以看到編輯資料。Python 的話就是寫程式,和 C++ 還有 R 比較像,但我覺得 Python 比 R 有趣。Bash Shell 的有些指令(command)都和 Python 共用,像是下面提到的 cd, ls 等等,也在 Python 可以用。

先這樣吧,下面放 Bash Shell 的筆記。


======


叫出 Terminal,在這裡下指令。 (用 Mac 的話,就從右上角的搜尋功能直接打 Terminal 就會出來了。)

註:以下指令的部分用粗體字,command 表示要下的指令,也就是粗體字(在例子中用 "" 標註)的地方。Output 表示下指令後會顯示的東西或結果。

pwd - show which directory you are currently in

Ex: command "pwd"
output: "Users/Linda/Desktop" (= you are now in "Desktop")

cd /User/Linda/dir - to enter designated directory

Ex: command "cd /Users/Linda/Desktop" (ps. home directory is: Users/Linda)

If you are already in that directory, then you can go to subdirectory directly by "cd directory_name"
e.g., if you are now in "Users/Linda/Desktop",
command "cd BashShell"
這時候如果你下 pwd 確認,會出現:Users/Linda/Desktop/BashShell

cd - back to home directory

Ex: command "cd"
output: "Users/Linda" (= go from Users/Linda/Desktop/BashShell" back to "Users/Linda")

cd .. - go to upper directory, not home directory

Ex: command "cd .."
output: "Users/Linda/Desktop" (= return from "Users/Linda/Desktop/BashShell" back to "Users/Linda/Desktop"

ls - list all files in the current directory

* - arbitrary mark

Ex: command "ls *.pdb"
output: list of all the .pdb files

mkdir FolderName - create a subdirectory in current directory

e.g., if you are already in Users/Linda/Desktop,
command "mkdir BushData" (= create the "BushData" folder in the "Desktop" directory)

vim - create a file

Ex: command "vim bash1" (= create "bash1" file in the "BashShell" folder)

nano - enter a file and edit script

Ex: command "nano bash1"
Now you can type in the file, e.g., type "Hello, this is Linda."
Then, create another file called "bash2", and in this file, type "Today is Sat, March 11."

cp - copy file

:wq - to leave bash files

less - enter a file, but can not edit

Ex: command "less bash1" (ps. need to command ":wq" to leave file screen)

more - show content in a file

Ex: command "more bash1"
output: "Hello, this is Linda."

cat - move content in the assigned files into a single designated file

Ex1: command
"cat bash1 bash2 >> bash3.txt"
"more bash3.txt"
output:
"Hello, this is Linda.
Today is Sat, March 11"

Ex2: command
"cat *.dat >> sugar.txt"
"more sugar.txt"
output: will show content from all .dat files

If you want to create a file that contains filenames of certain type of file,

Ex1: create a new file "allsugar.txt" that contains the filenames of all ".dat" files
command
"for filename in *.dat
do ls $filename >> allsugar.txt
done"
output: list of all files in ".dat"

command "more allsugar.dat"
output: list of all files in ".dat"

Ex2: create a new file 'allmolecules.txt' that contains the filenames of all ".pdb" files
command "for filename in *.pdb; do ls $filename >> allmolecules.txt; done"

mv - move or rename a file

Ex1: command "mv bash3.txt BashShell"
output: move "bash3.txt" to the folder "BashShell"

Ex2: command "mv sucrose.dat Sugar"
output: move "sucrose.dat" to the folder "Sugar"

Ex3: command "mv glucose-lact.dat glucose.dat"
output: change filename "glucose-lact.dat" to "glucose.dat"

rm - remove a file (在這裡刪了就很難救回來)

tree - show the structure (不是所有電腦都有這個功能,我的就沒有。QQ)

wc - word counts

e.g., command "wc *.pdb"

sort - sort context in files
command "sort -n" (= sorting by numbers)

Ex: command "sort *.dat >> sorted.txt"
output: list of sorted context in all .dat files is saved in the file "sorted.txt"

history - show history of the command you used

*If you want to rename a bunch of files, command:
for file in *.xml
do
mv $file $file-original.xml (e.g., $elem $elem-original.xml)
done

output: 所有的 .xml 檔案的名字後面都會加 "-original.xml"

* Extract commands (擷取部分資料)

head -15 (= extract the first 15 lines from a file)
head -15 | tail -6 (= extract first 15 lines, and then last 6 lines of the first 15 lines = line 10 to 15)

* to search a word in a file, command:
grep word filename (e.g., grep not haiku.txt)

* to search a word not part of the other word, command:
grep -w word filename (e.g., grep -w day haiku.txt)

* to search a phrase, command:
grep -w phrase filename (e.g., grep -w "is not" haiku.txt)

man grep - function manual of grep

find . -type f = give you all the files in the directory; "." = current directory

find . -name *.txt = give you files in .txt in current directory

find . -name '*.txt' = give you file in .txt in all subdirectories




Other resources:

Code programming - rapid table










沒有留言:

張貼留言

歡迎發表意見