---
# System prepended metadata

title: ShellScript腳本撰寫
tags: [shellscript, Linux]

---

# ShellScript腳本撰寫

![](https://hackmd.io/_uploads/SJYL41TLh.png)


![](https://hackmd.io/_uploads/S1C9U1TL3.png)

*詳圖兩個location路徑要一樣,不然執行腳本會出現異常*

**對專案右鍵>New>file>au/命名(英文字要.sh)**
![](https://hackmd.io/_uploads/H1ef51pI3.png)

**編輯完腳本按run即可執行(以下為date舉例)**
![](https://hackmd.io/_uploads/rJxIjypIh.png)






**編輯設定:edit configuration**
![](https://hackmd.io/_uploads/rJElHJT8h.png)

![](https://hackmd.io/_uploads/SyvXIJpL2.png)



*路徑為起初專案建立路徑下的bash.exe*


![](https://hackmd.io/_uploads/rJ5dKy6Lh.png)



#  **腳本範例1:印出年齡,姓名**
![](https://hackmd.io/_uploads/Hkmurc-v2.png)

![](https://hackmd.io/_uploads/r182LcZPh.png)

![](https://hackmd.io/_uploads/SJMpU9Wvn.png)



echo -把變數印出來

my_name= 等於前後不能空白

-n 讀取使用者輸入的參數

read age - age是變數

# **腳本範例2:IF-ELSE**

![](https://hackmd.io/_uploads/HyIIT5bvh.png)


![](https://hackmd.io/_uploads/HyBwTcbv2.png)
 
 
-n 前後要有空格 -參數跟字串前後要有空格

Fi > 前面的條件都執行完後 會執行此行(單純印出)


>  腳本裡的IF ELSE寫法 (跟一般程式不一樣):
>  
>  
>  #-eq：等於
> #-ne：不等於
> #gt：大於
> #ge：大於等於
> #lt：小於
> #le：小於等於


# **腳本範例3:case in** (多條件情況用)

![](https://hackmd.io/_uploads/Skl7xi-v3.png)


![](https://hackmd.io/_uploads/HJNmWsZPh.png)


# **腳本範例4:loop** 

![](https://hackmd.io/_uploads/ByprMi-wh.png)


![](https://hackmd.io/_uploads/BkWSMi-v2.png)



LOOP 變數,123塞到LOOP變數裡
依次代入變數,執行三次
loop 循環, 變數寫多少多長 不限字數

# 腳本範例5: function(函式)

![](https://hackmd.io/_uploads/Bk-UHiZw2.png)


前三行定義函式, 定義{ } , function 後面的名稱可以自己命名
最後echohello 呼叫前面的函數,把world,rock 參數傳到前面的echohello

0,1,2 > 印的順序

# 腳本範例6:基本宣告、建立目錄、刪除目錄.. (詳pycharm)



![](https://hackmd.io/_uploads/Syhr753v2.png)


![](https://hackmd.io/_uploads/rkb9wq3v2.png)


![](https://hackmd.io/_uploads/HkAGd52Ph.png)

![](https://hackmd.io/_uploads/rJHSc9nPh.png)



---







**step1**
![](https://hackmd.io/_uploads/rJ5FPibvh.png)

**step2 (專案整個勾選起來)**
![](https://hackmd.io/_uploads/H1dJOjWP3.png)




**step3**
![](https://hackmd.io/_uploads/ryqwYs-Pn.png)
本地跟server端 都要同個分支, 預設是master

**step4**
![](https://hackmd.io/_uploads/rJqroo-w3.png)

**step5**

只需寫儲存庫名稱 & 預設分支

![](https://hackmd.io/_uploads/rkkkthZvh.png)

![](https://hackmd.io/_uploads/HkWf3sWwh.png)

**step6**
![](https://hackmd.io/_uploads/H1n-5sWPn.png)
web用http,遠端用ssh,故此處選ssh url



**step7**
![](https://hackmd.io/_uploads/BklVnjWvn.png)


貼上ssh url 

**step8**
![](https://hackmd.io/_uploads/rkuKV2bw3.png)

登入gitea帳號






# 建立本地端git存放庫


![](https://hackmd.io/_uploads/rkI0r3bv3.png)


![](https://hackmd.io/_uploads/rJNJU2bwh.png)


![](https://hackmd.io/_uploads/Hk1x83Zvn.png)


![](https://hackmd.io/_uploads/BJpxU3-vh.png)
(gitea ssh url)


![](https://hackmd.io/_uploads/BkVV8hZv3.png)


![](https://hackmd.io/_uploads/BJq8Ln-P3.png)




![](https://hackmd.io/_uploads/Sy6v83WPh.png)



![](https://hackmd.io/_uploads/B13KIh-v2.png)

=========

# **若server端與本機端分支無法match 就用clone的**

![](https://hackmd.io/_uploads/ryqp83ZP3.png)


![](https://hackmd.io/_uploads/rJvZv3ZDn.png)


![](https://hackmd.io/_uploads/rJQmD2bDn.png)
(gitea ssh url)





# 從pychan連到webserver(同步)


![](https://hackmd.io/_uploads/ByJakKhv2.png)




![](https://hackmd.io/_uploads/HJUGetnD3.png)



![](https://hackmd.io/_uploads/BkILeFhD2.png)
(按ACCEPT)-信任SERVER憑證

# 匯出PDF,MD檔

![](https://hackmd.io/_uploads/SkM3fFnPh.png)

![](https://hackmd.io/_uploads/Sya2fK3D2.png)




![](https://hackmd.io/_uploads/BkfRztnD2.png)

(開始撰寫,撰寫完成後可存成PDF,MD,HTML...)



![](https://hackmd.io/_uploads/rybUl5nvh.png)
(同步於SERVER端)


commit




# 參考網站:
[http://gogojimmy.net/2012/01/17/how-to-use-git-1-git-basic/](https://)


###### tags: `Linux` `shellscript`


![](https://hackmd.io/_uploads/HyAzN7kdh.png)



