在 Bash 中檢查檔案是否存在
Fumbani Banda
2022年5月14日

本教程使用 test
命令檢查 bash 中是否不存在常規檔案。
在 Bash 中檢查檔案是否不存在 test
test
命令有一個邏輯非運算子,即 !
。我們還將新增 -f
選項來指定檔案。在這種情況下,條件測試檔案是否不
存在。
#!/bin/bash
if [ ! -f check_fil.sh ];
then
printf 'No such file\n'
else
printf 'File exists\n'
fi
輸出:
No such file
作者: Fumbani Banda