# debian note --- # index <h2>重要指令</h2> * 檢查是否有更新檔 * 關機 * 光碟掛載 * 在Linux執行C語言 <h2>小知識</h2> * 指令常用-號 * fd(file descriptor) * wildcord characters * 特殊變數 * 檢查cpu使用 * linux編輯器 <h2>常用指令</h2> * 條列資料夾內檔案名稱 * 顯示目前所在資料夾位置 * 移動至特定資料夾 * 建立空資料夾 * 移動檔案 * 複製檔案 * 刪除檔案 * 尋找檔案 * 檢查檔案 * 查看目前正在執行的行程 * 查看磁碟 * 追蹤登入在系統上的使用者 * 從檔案中找出含有某字的行數的內容 <h2>relation comparison</h2> * Numberical comparison * String comparison * File comparison * 範例 <h2>loop</h2> --- # 重要指令 ## 檢查是否有更新檔 ``` $ sudo apt update # sudo apt upgrade ``` ## 關機 ``` $ sync;sync;sudo init 0 ``` sync: 把buffer的資料存入硬碟 init: 總共有7個狀態 0:關機 1:單使用者模式(root) 2:多使用者模式,但不能使用NFS 3:完全多使用者模式(標準模式) 4:安全模式 5:以圖形化介面開啟 6:重新開機 ## 光碟掛載 ``` # 將光碟掛載到dev/cdrom $sudo mount /dev/cdrom /mnt # 執行程式 $ sudo exe_file_name # 退出光碟 $ sudo umount ``` ## 在linux環境執行C語言 1. 安裝 gcc編輯器 ``` $ sudo apt-get install gcc ``` 2. 用nano編輯一份.c檔  3. 用gcc編譯C語言 ``` $ gcc C_file $ gcc -o new_file_name C_file ``` 上面指令會產生一個輸出檔,預設檔名是a.out,我們可以用第二行指令來更改產生的輸出檔檔名  4. 呼叫輸出檔來執行編譯後的結果  --- # 小知識 ## 指令常用-號 * Unix style paremeters: proceded by - * BSD style paremeters: no * GNU long parameters: proceded by -- ## fd(file descriptor) * standard input(fd=0) * standard output(fd=1) * standard error(fd=2) ## wildcord characters * \*:0個以上任意字元 * \?:1個任意字元 * \[char]:接受字元 ex:usr/bin/[defg]* 相當於 usr/bin/[d-g]*  ## 特殊變數 * $?: exit status * $$: current processID * $#: number of positional argument * $*: list of positional argument * $+number: like $1, $2, ... is the first argument and second argument ## 檢查cpu使用 ``` $cat /proc/cpuinfo ``` ## linux編輯器 * 常見編輯器種類 * 1. nano * 2. vi --- # 常用指令 ## 條列資料夾內檔案名稱 ``` $ls [] folder_name ``` * \[ ] can put * * -l: long format * * -i: inode * * -la: include hidden file * * F: 資料夾名稱後面會多"/"(可用來判別是否是資料夾) * * -s: sort(by ASCII code) * * * r: reverse * * * S: size * * * R: recursive * output type * * 使用 $ls -l / 輸出結果 * * [file_type(d) /permission]/ linknumber/user/group/size/last modified/file name  * * 使用 $ls -li / 輸出結果 * * * inode/ [file_tupe(d)/ permission]/ linknumber/user/group/size/last modified/file name  inode:檔案或資料夾的id, 相同的檔案或資料夾會有相同的inode,透過部分複製方法可以產生相同內容但不同inode的檔案或資料夾 [file_type/ permission]字串細節 共有10個字元,我們可以將其拆解成1+3+3+3 1是file_type: 表示檔案的型態 * d: directory * -: regular file * c: charactor special file:以字元做傳送 * b: block special file 以block做傳送 * l: link file 後面3個3依序分別代表 user(使用者)/ group(同群組的使用者)/ other(任意使用者)的權限 而3個字母(rwx)分別代表 read/write/execute 的權限, 若該格顯示"-" 則代表無該權限 example: drwxr-x--- 即代表 他是一個資料夾 user可讀寫執行或進入,group member可讀和執行或進入,other(其他使用者)無法讀寫執行或進入 此外,rwx也可以使用數字代表,rwx=(111)2 = 7 如上例(rwxr-x---)=(111101000)2=7(user permission)5(group permission)0(other permission) ## 顯示目前所在資料夾位置 ``` $ pwd ``` ## 移動至特定資料夾 ``` $ cd directory_path ``` cd: change directory 絕對路徑: /(代表root): /tmp 代表從root開始找 找下方名為tmp的資料夾 相對路徑: * .: current directory * ..: parent directory * example: ../tmp:從目前所在資料夾向上找到parent directory,從裡面找到名為tmp的資料夾 ~: home directory = $ cd (空格) ## 建立空資料夾 ``` $ mkdir file_name ``` 或可以使用 ``` $ touch 欲修改時間 file_name ``` 如果該檔案不存在則創建一個新的檔案 ## 移動檔案 ``` $ mv source target ``` 此外該指令也可以用來修改檔案名稱 ``` #將A檔案移至同一個資料夾內並取名為B $ mv A ./B ``` ## 複製檔案 ``` $ cp source target ``` 若要更改檔名可以載target中指定 ``` #複製A到資料夾f1_1中(file_name仍然是A) $ cp A f1/f1_1 #複製A到資料夾f1_1中並改名叫B $ cp A f1/f1_1/B ``` ### share file ``` # 複製A到f1_1資料夾中, 且可以共同使用 cp -l A f1/f1_1 ``` * -l(hard link) * * hard link 限制 * * * 不能對資料夾做hard link * * * 不能對file system做hard link * -s(soft link): 載兩個目錄或是file system 中做link ``` $ cp -l = ln $ cp -s = ln -s ```  從上圖可以看到 A,A_hardlink01, A_hardlink02三個檔案使用的inode是相同的,且第三個欄位的數字有幾個檔案使用相同的inode  從上圖可以看到 A,A_softlink01, A_softlink02三個檔案使用的inode並不相同,A_softlink是產生一個link檔指向A檔案,若我們將A刪除則可以看到A_softlink檔指向無效位置  ## 刪除檔案 ``` $ rm file_name ``` 若要刪除資料夾含整個資料夾內所有檔案可以使用 -r(recursion) ``` $ rm -r directory_name ``` 有些檔案可能會無法順利刪除,因此可以使用-rf(force)來強制執行 ``` $ rm -rf firectory_name ``` ## 找檔案 ``` $ find 找尋位置 -name 找尋字 $ find /usr/bin -name [d-g][d-g][d-g]* ```  有時候會碰到無法查閱的檔案,這時會出現standard error並且印在螢幕上 如下圖部分行數顯示"Permission denied" 這個錯誤意思是我們無法訪問(沒有權限)該檔案  我們可以使用以下程式將standard error去除 ``` find 找尋位置 -name 找尋字 2> /dev/null find /etc -name [a]* 2> /dev/null ``` 2:standard error \>:redirection 將(左)指向給右 dev/null:垃圾桶 tee:既輸出給螢幕也將執行結果存放至檔案  ## 檢查檔案 ### 檢查檔案型態 ``` $ file file_name ``` ### 檢查檔案資訊 ``` $ stat file_name ``` Access:最後被修改時間 modify:最後更新時間 change:最後內容改變時間 birth:生成時間 ### 檢查檔案內容 ``` $ cat file_name ```  加入 -n可以在左邊顯示行號 加入 -b同樣會加入行號,但空白行會跳過 ``` $ cat -n file_name $ cat -b file_name ```  ``` $ cat >file_name ``` 可以接續寫入資料至該檔案內 ## 查看目前正在執行的行程 ``` $ ps ```  PID: processID TTY: terminal Time: 使用CPU的時間 CMD: command process name ## 查看磁碟 ``` $ df # 我們可以添加 -h使得結果方便使用者閱讀 $ df -h # 或是我們可以使用du來檢查磁碟 $ du $ du -h ``` 這兩者間有哪裡不同可以參考以下[連結](https://jackyu.medium.com/%E7%A7%91%E6%99%AE-df-%E5%92%8C-du-%E6%8C%87%E4%BB%A4%E7%82%BA%E4%BD%95%E6%9C%89%E6%99%82%E5%80%99%E9%A1%AF%E7%A4%BA%E4%B8%8D%E5%90%8C%E5%92%8C%E5%A6%82%E4%BD%95%E8%A7%A3%E6%B1%BA-91ed73c15dbe) ## 追蹤登入在系統上的使用者 ``` cat /etc/passwd ```  每一行的資訊分別是(第一個數字是行數) root\:x:0:0:root:/root:bin/bash root\(user_name\):x\(加密的密碼):0\(userID\):0\(groupID\):root\(full_name\):/root\(home_directory\):/bin/bash\(login shell\) ## 從檔案中找出含有某字的行數的內容 ``` $ grep \[] found_text file_name ``` \[]內可以放 * -v:不含有 * -n:列出行數和內容 * -c:含有某個字的各數 * -e: or ## 壓縮檔案 ``` # 壓縮檔案 $ bzip2 File_name $ gzip File_name $ zip rar_file_name File_name # 解壓縮檔案 $ bunzip2 rar_file_name $ gunzip rar_file_name ``` ## 把變數變成global variable ``` $ export variable_name # 把變數從global variable 刪除 $ unset variable_name ``` ## 任意精度计算器语言 ``` $bc # 輸入quit可以退出 # 若想省略掉指令下方說明 可以寫 $ bc -q ```  * scale= number: 可以決定計算結果要取到小數點後第幾位 ## 各引號使用效果 * ''(single quote):將裡面認為是一個字串 * ""(double quote):將裡面變數轉換成常數後再當成字串 * \`\`(back quote):將裡面的字串當成指令執行 * $( ):先做裡面的指令再當成一個變數回傳  --- # relational operators ## Numberical comparison * -gt: > * -ge: >= * -lt: < * -le: <= * -eq: == * -ne: != ## String comparison * \> * \>= * < * <= * -n: 檢查字串長度是否>0 * -z: 檢查字串是否為空 ## File comparison * -e: check if file exist * -f: check if file exist and is a file * -d: check if file exist and is a directory * -r: check if file exist and is readable * -w: check if file exist and is wirtable * -x: check if file exist and is executable * f1 -nt f2: file1 is newer than file2 * f1 -ot f2: file1 is older than file2 ## 範例 ### numberical comparison * code  * 執行結果  其中可以看到三種不同表達式的寫法皆代表同一個表達式 ``` [_$1_-gt_$2_]: _代表空白 test_$1_-gt_$2 (($1_>_$2)) ``` ### string comparison * code  * 執行結果  其中可以看到有兩種不同表達式的寫法皆代表同一個表達式 ``` [[_$1_>_$2_]] _:代表空白 [_$1_\>_$2_] \>:代表真正的>符號,而不是指向符號 ``` # loop ## for loop基礎寫法 ``` for $item in list do do something done ```  後方list參數是用空白鍵隔開,若list內容有引號要注意可能會被當成一個元素 若要將其效果消除,可以在引號前多加\\取消效果  for1和for2的執行結果,可以看到for2被引號括起來的空白並沒有發揮分隔元素的功用  若我們不想以空白當成分隔符號 可以修改全域變數IFS ``` $ IFS=' \n' ```   $Home/.* 指的是HOME(/home/user)內所有隱藏檔案 for 迴圈也可以用類似C語言的寫法來編寫 ``` for (( i=1; i<=100;i++ )) ``` #未整理 # define function ``` function func_name { commands } ``` ## 不傳參數 不回傳 ``` function func1 { echo "Hello world!" } for ((i=1;i<=5;i++)) do func1 done exit 0 ``` ## 傳參數 回傳 ``` function func2 { echo $[$1 + $2] } res=$(func2 3 17) exit 0 ``` ## function 另類寫法 ``` calc() { echo $[$1 $2 $3] } echo $(calc 3 + 17) echo $(calc 3 - 17) echo $(calc 3 \* 17) echo $(calc 17 / 3) exit 0 ``` ``` #! /bin/bash calc() { if [ $# -ne 3 ] then echo "error: calc op1 oper op2" exit 123 else echo $[$1 $2 $3] fi } res=$(calc 12 5) echo '$?='$? echo 'res='$res echo $(calc 12 - 5n echo $(calc 12) echo '$?='$? exit 0 ``` ``` #! /bin/bash func1() { local temp=$[ $temp + 5] res=$[$temp \* 2] } func2() { temp=$[ $temp + 5] res=$[ $temp \* 2] } temp=4 func1 echo "The result is $res, and temp is $temp" temp=4 func2 echo "Thr result is $res, and temp is $temp" exit 0 ``` # array ``` array_name = (item1 item2 item3) ``` ``` $ myarr = (Jan Feb Mar Apr May) $ echo $myarr # print Jan $ echo ${myarr[2]} # print Mar $ echo ${myarr[*]} # print Jan Feb Mar Apr May $ myarr[2]=March $ echo ${myarr[*]} # print Jan Feb March Apr May ``` $@:all list item $ newarr=($@) #若無小括號則變成 ``` #! /bin/bash function testit { echo "the parameters are $@" arr = $@ echo "the recive parameter are ${arr[*]}" } myarr = (1 2 3 4 5) echo "The original parameters are ${myarr[*]}" testit ${myarr[*]} exit 0 ``` 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up