標籤:shell bash wc
1. 查看目前使用者(使用者數)[email protected]:~# who [ | wc -l]
[email protected]:~# who
kallen tty4 2015-01-16 10:46
kallen tty5 2015-01-16 10:47
kallen tty2 2015-01-16 10:42
kallen tty3 2015-01-16 10:42
root tty6 2015-01-16 10:58
kallen tty1 2015-01-15 13:10
kallen tty7 2015-01-15 12:52
[email protected]:~#
[email protected]:~# who | wc -l
24
將管道l轉化為獨立的命令[email protected]:~# cat > countUsers ---- 建立檔案,使用cat複製終端輸入 [email protected]:~# who | wc -l ---- 指令碼內容
[email protected]:~# Ctrl + D ( end-of-file ) ---- end of file
[email protected]:~# chmod +x countUsers ---- 讓檔案擁有執行許可權
[email protected]:~# ./countUsers ---- 執行指令碼
【附】wc命令# Count Bytesecho Testing one two three |
wc -c print the byte counts (計算位元組數)# Count Rowsecho Testing one two three |
wc -l print the character counts (計算字元數)# Count Lettersecho Testing one two three |
wc -w print the newline counts (計算行數)
2.指令碼聲明#!/bin/bash##!/bin/bash -
- 表示沒有shell選項,這是基於安全上的考慮,可避免某些欺騙式攻擊(spoofing attack)#Shell的三種基本命令: 內建命令、Shell函數、外部命令
# 查看當前Shell類型[email protected]:/# echo $SHELL
/bin/bash
# Shell指令碼執行方式[email protected]:/# source shellFile[email protected]:/# bash shellFile[email protected]:/# ./shellFile (檔案必須有可執行許可權 chmod +x 或 chmod 755)
編寫完指令碼之後,你可以使用sh scriptname,[或者bash scriptname 來調用它.
(不推薦使用sh scriptname,因為這禁用了指令碼從stdin 中讀資料的功能.
使用sh scriptname 來呼叫指令碼的時候將會關閉一些Bash 特定的擴充,指令碼可能 因此而調用失敗.)
更方便的方法是讓指令碼本身就具有可執行許可權,通過chmod 命令可以修改.比如: chmod 555 scriptname (允許任何人都具有可讀和執行許可權)或: chmod +rx scriptname (允許任何人都具有可讀和執行許可權) chmod u+rx scriptname (只給指令碼的所有者可讀和執行許可權)
Shell Step by Step (1) -- Introduction