Bash shell 基礎

一、Linux 如何進入Bash ?linux

  Linux系統默認開啓的終端,通常都是Bash Shell,能夠經過以下命令肯定當前運行的默認Shell。shell

  echo $shellbash

  

  admin指示的就是登陸人是誰,localhost指示登陸到的主機,~表示當前操做目錄,$表示的是命令提示符(若是登錄人是root的話就顯示爲#),表示等待輸入命令。spa

二、Linux中執行shell腳本的方法:htm

 在運行腳本Hello.sh腳本以前須要有對文件的執行權限:blog

 a) 切換到shell腳本所在的目錄執行shell腳本 —— cd /test/shell   ./Hello.sh字符串

    b) 以絕對路徑的方式去執行bash shell腳本 —— /test/shell/Hello.shget

  c) 直接使用 bash 或 sh 去執行bash shell 腳本 —— cd /test/shell     bash Hello.sh or sh Hello.shstring

  d) 在當前的 shell 環境中執行bash shell 腳本 —— cd /test/shell     .  Hello.sh   or   source Hello.shit

三、Linux expr  [轉自:http://www.linuxidc.com/Linux/2012-04/58095.htm]

     a) expr length $string 求出字符串的長度,echo ${#string}也能夠計算字符串的長度。

# string="hello,everyone my name is xiaoming"  
# echo ${#string}  
34  
# expr length "$string"  
34 

     b) expr index $string substring,在字符串$string上找出substring中字符第一次出現的位置,若找不到則expr index返回0或1。

# string="hello,everyone my name is xiaoming"   
# expr index "$string" my
11

     c) expr match $string substring,在string字符串中匹配substring字符串,而後返回匹配到的substring字符串的長度,若找不到則返回0。

# string="hello,everyone my name is xiaoming"   
# expr match "$string" my  
0

  d)在shell中能夠用{string:position}:從position位置開始抽取直到字符串結束;{string:position:length}:從position位置開始抽取長度爲length的子串;兩種形式進行對string字符串中字符的抽取。

# string="hello,everyone my name is xiaoming"   
# echo ${string:10}   ==》yone my name is xiaoming  
# echo ${string:10:5}  ==》yone  

  e)刪除字符串和抽取字符串類似${string#substring}爲刪除string開頭處與substring匹配的最短字符子串,而${string##substring}爲刪除string開頭處與substring匹配的最長字符子串。

# string="20091111 readnow please"   
# echo ${string#2*1}   ==》111 readnow please  
# echo ${string##2*1}  ==》readnow please  

  f)替換子串${string/substring/replacement}表示僅替換一次substring相配字符,而${string//substring/replacement}表示爲替換全部的substring相配的子串。

# string="you and you with me"   
# echo ${string/you/me}   ==>me and you with me  
# echo ${string//you/me}   ==>me and me with me  

四、/dev/null

       /dev/null   null是一個名叫null小桶的東西,若是命令的輸出不想要即想丟棄輸出的內容,既不想在標準輸出與不想重定向到某個文件,就可將命令的輸出重定向到/dev/null。這樣作的好處是不會由於輸出的內容過多而致使文件大小不斷的增長。

相關文章
相關標籤/搜索