小脚本示例如下:(可通过改变脚本中的 /u01 为别的目录,达到自己想要的输出)
[grid@node1 u01]$ cat test2
#!/bin/bash
if [ -d /u01 ]
then
echo "The drectory is exists"
cd /u01
echo "pwd"
pwd
echo "ll"
ls -l
else
echo "There is a problem with the given directory"
fi
[grid@node1 u01]$ ./test2
The drectory is exists
pwd
/u01
ll
total 8
drwxrwxr-x 5 grid oinstall 43 Jan 5 06:33 app
-rwxr-xr-x 1 oracle oinstall 124 Jan 8 07:56 test1
-rwxr-xr-x 1 grid oinstall 182 Jan 8 08:19 test2
[grid@node1 u01]$
作者: 王硕 时间: 2016-1-8 21:44
2.查看对象文件是否存在
如果存在目录,先找到目录,然后查找是否存在test3文件。
如果存在添加date到test3文件中去。否则创建新文件。
[grid@node1 u01]$ cat test3
#!/bin/bash
if [ -e /u01 ]
then
echo "OK,on the directory,now let's check the file"
if [ -e /u01/test1 ]
then
echo "Appending date to existing file "
date >> /u01/test1
else
echo "Creating new file "
date > /u01/test1
fi
else
echo "Sorry,you don't have the directory"
fi
[grid@node1 u01]$ ./test3
OK,on the directory,now let's check the file
Appending date to existing file