找不到 bash 指令

找不到 bash 指令

我想知道有人可以幫助我:

if [ -z $1 ]; then                                                                                                                                                                                                                                                             
  user=$(whoami)                                                                                                                                                                                                                                                         
else                                                                                                                                                                                                                                                                           
  if [ ! -d "/home/$1" ]; then                                                                                                                                                                                                                                           
    echo "Requested $1 user home directory doesn't exist."                                                                                                                                                                                                         
    exit 1                                                                                                                                                                                                                                                         
  fi                                                                                                                                                                                                                                                                     
  user=$1                                                                                                                                                                                                                                                                
fi 

我正在研究一些 bash 命令時,看到兩個命令:-z-d。我知道他們做什麼(首先檢查空白變量,第二次檢查是否存在目錄)。我的問題是如何找到有關這些命令的描述(ig 手冊頁 -d/-z)。它們只能與 if-else 語句一起使用嗎?

答案1

和不是命令,而是-d和實用程式的選項。這些實用程式已內建並記錄在手冊中。這些實用程式和這些標誌也恰好由 POSIX 標準化,因此它們可以在任何 POSIX shell 中使用,而不僅僅是.-ztest[bashbashbash

如果您處於互動式會話中,您可以透過鍵入(也可以,但其文字僅引用 的文件)bash來取得這些實用程式的內建變體的文件。help testhelp [test

man test並且man [應該也有效。這些手冊描述了外部的實用程序,可能是/bin/test/bin/[,而不是您在 中預設使用的實用程序bash

例如,

! test -z "$dir" && test -d "$dir" && printf '%s is a directory' "$dir"

完全等於

! [ -z "$dir" ] && [ -d "$dir" ] && printf '%s is a directory' "$dir"

或者,如果你願意的話,

if ! test -z "$dir" && test -d "$dir"; then
    printf '%s is a directory' "$dir"
fi

if ! [ -z "$dir" ] && [ -d "$dir" ]; then
    printf '%s is a directory' "$dir"
fi

! [ -z "$dir" ]可能更常寫為[ ! -z "$dir" ]or [ -n "$dir" ],我只使用了-z上面的測試,因為問題中提到了它,-d對空字串的測試無論如何都會失敗)。

也可以看看:

相關內容