Shell Step by Step (1) -- Introduction

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.