2017年11月28日 星期二

Bash if

if [[ -n "${VAR}" ]]; then
  echo variable VAR is set and value is ${VAR}
fi

if [[ -z "${VAR}" ]]; then
  echo variable VAR is unset or empty
fi

                        +-------+-------+-----------+
                VAR is: | unset | empty | non-empty |
+-----------------------+-------+-------+-----------+
| [ -z "${VAR}" ]       | true  | true  | false     |
| [ -z "${VAR+set}" ]   | true  | false | false     |
| [ -z "${VAR-unset}" ] | false | true  | false     |
| [ -n "${VAR}" ]       | false | false | true      |
| [ -n "${VAR+set}" ]   | false | true  | true      |
| [ -n "${VAR-unset}" ] | true  | false | true      |
+-----------------------+-------+-------+-----------+

if [[ -e file.txt ]]; then
  echo file.txt is exist
fi

if [[ ! -e file.txt ]]; then
  echo file.txt is NOT exist
fi

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

沒有留言:

張貼留言