鍍金池/ 問(wèn)答/Linux/ shell 腳本幾行代碼的意思

shell 腳本幾行代碼的意思

if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

我想問(wèn)一下shell 這幾行代碼的-f 和-r 啥意思

回答
編輯回答
巴扎嘿

-e filename 如果 filename存在,則為真
-d filename 如果 filename為目錄,則為真
-f filename 如果 filename為常規(guī)文件,則為真
-L filename 如果 filename為符號(hào)鏈接,則為真
-r filename 如果 filename可讀,則為真
-w filename 如果 filename可寫,則為真
-x filename 如果 filename可執(zhí)行,則為真
-s filename 如果文件長(zhǎng)度不為0,則為真
-h filename 如果文件是軟鏈接,則為真

2017年5月6日 23:24
編輯回答
逗婦乳

有個(gè)網(wǎng)站可以解釋 shell 命令:https://explainshell.com

write down a command-line to see the help text that matches each argument

比如把你腳本里面的 if [ -f /etc/bash.bashrc ]; then . /etc/bash.bashrc; fi 放到 explainshell 里面:

https://explainshell.com/expl...

clipboard.png

如上圖,-f 有明確的解釋。如此類推,可自行查看其它內(nèi)容。

2018年7月28日 09:56