iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 8
1

網址:
https://www.guru99.com/introduction-to-shell-scripting.html
作業系統有很多部分,但是有兩個主要的部份:
Kernel
Shell
如圖:
Kernel在比較靠近硬體這側,shell靠近使用者這側
https://www.guru99.com/images/ShellScripting.png
https://upload.wikimedia.org/wikipedia/commons/thumb/d/d8/Operating_system_architecture.svg/400px-Operating_system_architecture.svg.png

A Kernel is at the nucleus of a computer. It makes the communication between the hardware and software possible. While the Kernel is the innermost part of an operating system, a shell is the outermost one.

Kernel負責軟硬體溝通,屬於作業系統的最內側,shell屬於作業系統的最外側

A shell in a Linux operating system takes input from you in the form of commands, processes it, and then gives an output. It is the interface through which a user works on the programs, commands, and scripts. A shell is accessed by a terminal which runs it.

shell取得在終端機輸入的指令,執行指令,得到輸出結果,shell是一個介面,當使用者在終端機programs, commands, and scripts,shell一直都在終端機執行。

When you run the terminal, the Shell issues a command prompt (usually $), where you can type your input, which is then executed when you hit the Enter key. The output or the result is thereafter displayed on the terminal.

當執行終端機,Shell發布一個符號:$,可以輸入指令,當按下enter時,結果會顯示在終端機

The Shell wraps around the delicate interior of an Operating system protecting it from accidental damage. Hence the name Shell.

Shell像是貝殼保護作業系統的內部,所以才叫貝殼

Shell的主要種類

  1. The Bourne Shell: The prompt for this shell is $ and its derivatives(後繼版本) are listed below:
    一 POSIX shell(sh)
    二 Korn Shell(sh)
    三 Bourne Again Shell(bash) (most popular)
    Bourne shell:https://zh.wikipedia.org/wiki/Bourne_shell
  2. The C shell: The prompt for this shell is %, and its subcategories(後繼版本) are:
    一 C shell(csh)
    二 Tops C shell (tcsh)
    C Shell:https://zh.wikipedia.org/wiki/C_Shell

什麼是Shell Scripting?

Shell scripting is writing a series of command for the shell to execute. It can combine lengthy and repetitive sequences of commands into a single and simple script, which can be stored and executed anytime. This reduces the effort required by the end user.

Shell scripting可以寫一系列的指令來執行,可以將lengthy(冗長) and repetitive (重覆)sequences of commands組合成a single and simple script,script可以存儲和執行任何時間,降低使用者的工作量

開始寫shell scripting
範例一:
1創建文字檔(選擇一個文字編輯器,這邊用nano)
nano scriptsample.sh
2進到文字編輯器畫面
第一行輸入#!/bin/sh
接著每行只要打要執行的指令就可以了
離開編輯器Ctrl+x-->Y(確認要不要修改文字檔內容)-->enter(確定檔名)
https://ithelp.ithome.com.tw/upload/images/20181021/201119948iyqkKzYT2.png
3 bash scriptsample.sh
畫面就會有命令的結果了
https://ithelp.ithome.com.tw/upload/images/20181021/201119948I7RdKJijD.png

範例二:
1 nano helloscript.sh
2編輯器打字
#代表註解
https://ithelp.ithome.com.tw/upload/images/20181021/20111994gkYtWwQvED.png
3 bash helloscript.sh
https://ithelp.ithome.com.tw/upload/images/20181021/20111994Px18UZYEhj.png

範例三:
http://linux.vbird.org/linux_basic/0340bashshell-scripts.php#script_why
隨日期變化檔名
1 nano create_3_filename.sh
2編輯文字檔
echo:畫面中顯示的內容提醒使用者要創三個文字檔
read:請使用者輸入東西,這個東西就會是fileuser這個變數
{fileuser:-“filename”}:如果使用者有輸入東西就是fileuser這個變數,如果使用者直接按空白鍵就會是“filename”這個字串
date1、date2、date3代表前天、昨天、今天日期
file1、file2、file3代表檔名
touch:創建檔案
https://ithelp.ithome.com.tw/upload/images/20181021/20111994rgCRaBXMOS.png
3
https://ithelp.ithome.com.tw/upload/images/20181021/20111994DUy6aC1PEO.png
https://ithelp.ithome.com.tw/upload/images/20181021/20111994aOL7TBTtmv.png
範例四:
http://linux.vbird.org/linux_basic/0340bashshell-scripts.php#script_why
兩數相乘
1nano
2編輯文字檔
https://ithelp.ithome.com.tw/upload/images/20181021/20111994JvX9RqFu6h.png
3bash
https://ithelp.ithome.com.tw/upload/images/20181021/20111994kb4myt9pTl.png

還有很多的Shell Scripting可以練習,看要不要再寫一篇Shell Scripting,或是自己練習


上一篇
Linux輸入多重指令
下一篇
比較Windows和 Linux
系列文
Linux學習30

尚未有邦友留言

立即登入留言