Shell Script(bash)--教學例(轉)
Shell Script(bash)--教學例(轉)[@more@]"Hello world" Shell Script
照傳統程式教學例,這一節介紹Shell Script的"Hello World"如何撰寫。
--------------------------------------------------------------------------------
#!/bin/sh
# Filename : hello
echo "Hello world!"
--------------------------------------------------------------------------------
大家應該會注意到第一行的"#!/bin/sh"。在UNIX下,所有的可執行Script,不管是那一種語言,其開頭都是"#!",例如Perl是"#!/usr/bin/perl",tcl/tk是"#!/usr/bin/wish",看您要執行的Script程式位置在那裡。您也可以用"#!/bin/bash"、"#!/bin/tcsh"等等,來指定使用特定的Shell。
echo是個bash的內建指令。
--------------------------------------------------------------------------------
接下來,執行hello這個script:
要執行一個Script的方式有很多種。
--------------------------------------------------------------------------------
第一種 : 將hello這個檔案的許可權設定為可執行。
[foxman@foxman bash]# chmod 755 hello
執行
[foxman@foxman bash]# ./hello
hello world
--------------------------------------------------------------------------------
第二種 : 使用bash內建指令"source"或"."。
[foxman@foxman bash]# source hello
hello world
或
[foxman@foxman bash]# . hello
hello world
--------------------------------------------------------------------------------
第三種 : 直接使用sh/bash/tcsh指令來執行。
[foxman@foxman bash]# sh hello
hello world
或
[foxman@foxman bash]# bash hello
hello world
--------------------------------------------------------------------------------
Bash執行選項
--------------------------------------------------------------------------------
-c string : 讀取string來當命令。
-i : 互動介面。
-s : 由stdin讀取命令。
- : 取消往後選項的讀取。
-norc : 不要讀~/.bashrc來執行。
-noprofile : 不要讀/etc/profile、~/.bash_profile、~/.bash_login、~/.profile等等來執行。
-rcfile filename : 執行filename,而非~/.bashrc
-version : 顯示版本。
-quiet : 啟動時不要哩唆。
-login : 確保bash是個login shell。
-nobraceexpansion : 不要用curly brace expansion({}符號展開)。
-nolineediting : 不用readline來讀取命令列。
-posix : 改採Posix 1003.2標準。
照傳統程式教學例,這一節介紹Shell Script的"Hello World"如何撰寫。
--------------------------------------------------------------------------------
#!/bin/sh
# Filename : hello
echo "Hello world!"
--------------------------------------------------------------------------------
大家應該會注意到第一行的"#!/bin/sh"。在UNIX下,所有的可執行Script,不管是那一種語言,其開頭都是"#!",例如Perl是"#!/usr/bin/perl",tcl/tk是"#!/usr/bin/wish",看您要執行的Script程式位置在那裡。您也可以用"#!/bin/bash"、"#!/bin/tcsh"等等,來指定使用特定的Shell。
echo是個bash的內建指令。
--------------------------------------------------------------------------------
接下來,執行hello這個script:
要執行一個Script的方式有很多種。
--------------------------------------------------------------------------------
第一種 : 將hello這個檔案的許可權設定為可執行。
[foxman@foxman bash]# chmod 755 hello
執行
[foxman@foxman bash]# ./hello
hello world
--------------------------------------------------------------------------------
第二種 : 使用bash內建指令"source"或"."。
[foxman@foxman bash]# source hello
hello world
或
[foxman@foxman bash]# . hello
hello world
--------------------------------------------------------------------------------
第三種 : 直接使用sh/bash/tcsh指令來執行。
[foxman@foxman bash]# sh hello
hello world
或
[foxman@foxman bash]# bash hello
hello world
--------------------------------------------------------------------------------
Bash執行選項
--------------------------------------------------------------------------------
-c string : 讀取string來當命令。
-i : 互動介面。
-s : 由stdin讀取命令。
- : 取消往後選項的讀取。
-norc : 不要讀~/.bashrc來執行。
-noprofile : 不要讀/etc/profile、~/.bash_profile、~/.bash_login、~/.profile等等來執行。
-rcfile filename : 執行filename,而非~/.bashrc
-version : 顯示版本。
-quiet : 啟動時不要哩唆。
-login : 確保bash是個login shell。
-nobraceexpansion : 不要用curly brace expansion({}符號展開)。
-nolineediting : 不用readline來讀取命令列。
-posix : 改採Posix 1003.2標準。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617731/viewspace-947857/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Linux程式設計-11.Shell Script(bash)--(2)教學例(轉)Linux程式設計
- Shell Script(bash)--用於自動備份的Shell Script(轉)
- 20天學會bash shell script (二)
- 20天會學bash shell script(一)
- 20 天學會bash shell script (三)
- Linux程式設計-11.Shell Script(bash)--(3)用於自動備份的Shell Script(轉)Linux程式設計
- Shell Script(轉)
- Linux程式設計-11.Shell Script(bash)--(1)簡介(轉)Linux程式設計
- Linux的shell基礎教學(轉)Linux
- Shell(Bash)學習· 總章
- Shell Script
- shell script
- BASH shell set命令詳解(轉)
- BASH SHELL 程式設計簡介(轉)程式設計
- shell script程式設計小結——附帶例項程式設計
- PHP也可以當成Shell Script使用(轉)PHP
- Bash Shell指令碼中的陣列使用例項指令碼陣列
- shell script to rename directory
- BASH Shell的指令碼程式設計(轉)指令碼程式設計
- shell Bash變數變數
- bash shell tips
- Linux Bash Shell學習(十):流程控制——forLinux
- Linux 的shell基礎教學Linux
- Bash Shell 基礎特性
- 使用 Bash shell 指令碼進行功能測試(轉)指令碼
- Linux Bash Shell學習(九):流程控制——if/elseLinux
- Bash 例項:探討 ebuild 系統(轉)UI
- shell script的簡單使用
- execute shell script from stored procedure
- Linux Bash Shell學習(七):shell程式設計基礎——執行Shell指令碼、functionLinux程式設計指令碼Function
- Linux Bash Shell學習(八):shell程式設計基礎——string操作Linux程式設計
- shell簡介入門bash程式設計之執行(轉)程式設計
- xecuting shell commands with bash
- Bourne shell script中的quoting
- CVS教學(轉)
- bash shell 程式與磁碟資料
- shell程式設計–bash變數程式設計變數
- bash shell計算時間差