溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用Bash Shell

發(fā)布時間:2021-09-28 11:05:37 來源:億速云 閱讀:126 作者:iii 欄目:開發(fā)技術

本篇內(nèi)容主要講解“如何使用Bash Shell”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何使用Bash Shell”吧!

作為一個命令行探索者,你或許發(fā)現(xiàn)你自己一遍又一遍重復同樣的命令。如果你總是用ssh進入到同一臺電腦,如果你總是將一連串命令連接起來,如果你總是用同樣的參數(shù)運行一個程序,你也許希望在這種不斷的重復中為你的生命節(jié)約下幾秒鐘。

解決方案是使用一個別名(alias)。正如你可能知道的,別名用一種讓你的shell記住一個特定的命令并且給它一個新的名字的方式。不管怎么樣,別名有一些限制,它只是shell命令的快捷方式,不能傳遞或者控制其中的參數(shù)。所以作為補充,bash 也允許你創(chuàng)建你自己的函數(shù),這可能更長一些和復雜一點,它允許任意數(shù)量的參數(shù)。

當然,當你有美食時,比如某種湯,你要分享它給大家。我這里有一個列表,列出了一些最有用bash別名和函數(shù)的。注意“最有用的”只是個說法,別名的是否有用要看你是否每天都需要在 shell 里面用它。

在你開始你的別名體驗之旅前,這里有一個便于使用的小技巧:如果你的別名和原本的命令名字相同,你可以用如下技巧來訪問原本的命令(LCTT 譯注:你也可以直接原本命令的完整路徑來訪問它。)

  \command

例如,如果有一個替換了ls命令的別名 ls。如果你想使用原本的ls命令而不是別名,通過調用它:

  \ls

    提升生產(chǎn)力

這些別名真的很簡單并且真的很短,但他們大多數(shù)是為了給你的生命節(jié)省幾秒鐘,最終也許為你這一輩子節(jié)省出來幾年,也許呢。

  alias ls="ls --color=auto"

簡單但非常重要。使ls命令帶著彩色輸出。

  alias ll="ls --color -al"

以彩色的列表方式列出目錄里面的全部文件。

  alias grep='grep --color=auto'

類似,只是在grep里輸出帶上顏色。

  mcd() { mkdir -p "$1"; cd "$1";}

我的最愛之一。創(chuàng)建一個目錄并進入該目錄里: mcd [目錄名]。

  cls() { cd "$1"; ls;}

類似上一個函數(shù),進入一個目錄并列出它的的內(nèi)容:cls[目錄名]。

  backup() { cp "$1"{,.bak};}

簡單的給文件創(chuàng)建一個備份: backup [文件] 將會在同一個目錄下創(chuàng)建 [文件].bak。

  md5check() { md5sum "$1" | grep "$2";}

因為我討厭通過手工比較文件的md5校驗值,這個函數(shù)會計算它并進行比較:md5check[文件][校驗值]。

如何使用Bash Shell

  alias makescript="fc -rnl | head -1 >"

很容易用你上一個運行的命令創(chuàng)建一個腳本:makescript [腳本名字.sh]

  alias genpasswd="strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo"

只是瞬間產(chǎn)生一個強壯的密碼。

如何使用Bash Shell

  alias c="clear"

清除你終端屏幕不能更簡單了吧?

  alias histg="history | grep"

快速搜索你的命令輸入歷史:histg [關鍵字]

  alias ..='cd ..'

回到上層目錄還需要輸入 cd 嗎?

  alias ...='cd ../..'

自然,去到上兩層目錄。

 extract() { 
    if [ -f $1 ] ; then 
     case $1 in 
      *.tar.bz2)  tar xjf $1   ;; 
      *.tar.gz)  tar xzf $1   ;; 
      *.bz2)    bunzip2 $1   ;; 
      *.rar)    unrar e $1   ;; 
      *.gz)    gunzip $1   ;; 
      *.tar)    tar xf $1   ;; 
      *.tbz2)   tar xjf $1   ;; 
      *.tgz)    tar xzf $1   ;; 
      *.zip)    unzip $1    ;; 
      *.Z)     uncompress $1 ;; 
      *.7z)    7z x $1    ;; 
      *)   echo "'$1' cannot be extracted via extract()" ;; 
       esac 
     else 
       echo "'$1' is not a valid file" 
     fi 
  }

很長,但是也是最有用的。解壓任何的文檔類型:extract: [壓縮文件]
系統(tǒng)信息

想盡快地知道關于你的系統(tǒng)一切信息?

  alias cmount="mount | column -t"

按列格式化輸出mount信息。

如何使用Bash Shell

  alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/  /' -e 's/-/|/'"

以樹形結構遞歸地顯示目錄結構。

  sbs() { du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e';}

安裝文件在磁盤存儲的大小排序,顯示當前目錄的文件列表。

  alias intercept="sudo strace -ff -e trace=write -e write=1,2 -p"

接管某個進程的標準輸出和標準錯誤。注意你需要安裝了 strace。

  alias meminfo='free -m -l -t'

查看你還有剩下多少內(nèi)存。

如何使用Bash Shell

  alias ps? = "ps aux | grep"

可以很容易地找到某個進程的PID:ps? [名字]。

  alias volume="amixer get Master | sed '1,4 d' | cut -d [ -f 2 | cut -d ] -f 1"

    顯示當前音量設置。

如何使用Bash Shell

    網(wǎng)絡

對于所有用在互聯(lián)網(wǎng)和本地網(wǎng)絡的命令,也有一些神奇的別名給它們。

  alias websiteget="wget --random-wait -r -p -e robots=off -U mozilla"

下載整個網(wǎng)站:websiteget [URL]。

  alias listen="lsof -P -i -n"

顯示出哪個應用程序連接到網(wǎng)絡。

如何使用Bash Shell

  alias port='netstat -tulanp'

顯示出活動的端口。

  gmail() { curl -u "$1" --silent "https://mail.google.com/mail/feed/atom" | sed -e 's/<\/fullcount.*/\n/' | sed -e 's/.*fullcount>//'}

大概的顯示你的谷歌郵件里未讀郵件的數(shù)量:gmail [用戶名]

  alias ipinfo="curl ifconfig.me && curl ifconfig.me/host"

獲得你的公網(wǎng)IP地址和主機名。

  getlocation() { lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\';}

返回你的當前IP地址的地理位置。
也許無用

所以呢,如果一些別名并不是全都具有使用價值?它們可能仍然有趣。

  kernelgraph() { lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -;}

繪制內(nèi)核模塊依賴曲線圖。需要可以查看圖片。

  alias busy="cat /dev/urandom | hexdump -C | grep 'ca fe'"

在那些非技術人員的眼里你看起來是總是那么忙和神秘。

如何使用Bash Shell

做為獎勵,這里有我提到的全部別名和函數(shù)的純文本版本,隨時可以復制粘貼到你的.bashrc。(如果你已經(jīng)一行一行的復制到這里了,哈哈,你發(fā)現(xiàn)你又浪費了生命的幾秒鐘~)   

#Productivity
  alias ls="ls --color=auto"
  alias ll="ls --color -al"
  alias grep='grep --color=auto'
  mcd() { mkdir -p "$1"; cd "$1";}
  cls() { cd "$1"; ls;}
  backup() { cp "$1"{,.bak};}
  md5check() { md5sum "$1" | grep "$2";}
  alias makescript="fc -rnl | head -1 >"
  alias genpasswd="strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo"
  alias c="clear"
  alias histg="history | grep"
  alias ..='cd ..'
  alias ...='cd ../..'
  extract() {
    if [ -f $1 ] ; then
     case $1 in
      *.tar.bz2)  tar xjf $1   ;;
      *.tar.gz)  tar xzf $1   ;;
      *.bz2)    bunzip2 $1   ;;
      *.rar)    unrar e $1   ;;
      *.gz)    gunzip $1   ;;
      *.tar)    tar xf $1   ;;
      *.tbz2)   tar xjf $1   ;;
      *.tgz)    tar xzf $1   ;;
      *.zip)    unzip $1    ;;
      *.Z)     uncompress $1 ;;
      *.7z)    7z x $1    ;;
      *)   echo "'$1' cannot be extracted via extract()" ;;
       esac
     else
       echo "'$1' is not a valid file"
     fi
  }
   
  #System info
  alias cmount="mount | column -t"
  alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/  /' -e 's/-/|/'"
  sbs(){ du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e';}
  alias intercept="sudo strace -ff -e trace=write -e write=1,2 -p"
  alias meminfo='free -m -l -t'
  alias ps?="ps aux | grep"
  alias volume="amixer get Master | sed '1,4 d' | cut -d [ -f 2 | cut -d ] -f 1"
   
  #Network
  alias websiteget="wget --random-wait -r -p -e robots=off -U mozilla"
  alias listen="lsof -P -i -n"
  alias port='netstat -tulanp'
  gmail() { curl -u "$1" --silent "https://mail.google.com/mail/feed/atom" | sed -e 's/<\/fullcount.*/\n/' | sed -e 's/.*fullcount>//'}
  alias ipinfo="curl ifconfig.me && curl ifconfig.me/host"
  getlocation() { lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\';}
   
  #Funny
  kernelgraph() { lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -;}
  alias busy="cat /dev/urandom | hexdump -C | grep \"ca fe\""

到此,相信大家對“如何使用Bash Shell”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)
推薦閱讀:
  1. bash 基礎
  2. Bash Shell

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI