先看一段shell:
判斷環境中是否存在某命令
check_program_installed() {
hash $1 > /dev/null 2>&1
if [ "$?" != "0" ]; then
print "command $1 not found. is it installed?."
exit 1
fi
}
hash命令:顯示、添加或清除哈希表
1.linux系統下的hash指令
說明:
linux系統下會有一個hash表,當你剛開機時這個hash表為空,每當你執行過一條命令時,hash表會記錄下這條命令的路徑,就相當於緩存一樣。第一次執行命令shell解釋器默認的會從PATH路徑下尋找該命令的路徑,當你第二次使用該命令時,shell解釋器首先會查看hash表,沒有該命令才會去PATH路徑下尋找。
hash表的作用:
大大提高命令的調用速率。
2.參數
-l 顯示hash表內容
-r 清除hash表
-d openssl 刪除表中某一條(刪除openssl)
-t openssl 查看openssl命令路徑(hash表中沒有的話,可以調用which命令)
-p /usr/bin/openssl aliesopenssl 往hash表中添加一條,執行aliesopenssl即執行openssl命令(起別名)