1. 程式人生 > 其它 >Linux Shell 判斷目錄或檔案是否存在

Linux Shell 判斷目錄或檔案是否存在

技術標籤:ShellLinuxshelllinux

判斷目錄是否存在

#!/bin/bash

DIR="/usr/passwd"
# 判斷目錄是否存在,!代表反義,-d代表判斷目錄存在
if [! -d $DIR ];then
	# 建立目錄
	mkdir -p $DIR
	# 列印訊息
	echo "Path Not Exist,Creat in $DIR"
fi

判斷檔案是否存在

#!/bin/bash

File=/root/test.txt

if [ ! -f $File ];then
	ifconfig > $File
else
	cat $File
fi