重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2809|回复: 2
打印 上一主题 下一主题

[基础命令] SHELL脚本查找目录,并列出pwd,和ll

[复制链接]
跳转到指定楼层
楼主
发表于 2016-1-8 21:21:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 王硕 于 2016-1-8 21:24 编辑

小脚本示例如下:(可通过改变脚本中的 /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]$


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
回复

使用道具 举报

沙发
 楼主| 发表于 2016-1-8 21:44:19 | 只看该作者
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

[grid@node1 u01]$ cat test1
#!/bin/bash
testuser=oracle

if [ $USER = $testuser ]
then
echo "Welcome $testuser"
else
echo "You are not the user"

fi

Fri Jan  8 08:36:45 EST 2016


回复 支持 反对

使用道具 举报

板凳
 楼主| 发表于 2016-1-8 21:55:19 | 只看该作者
3. 检查文件
-e 既适用于文件,也适用于目录。
  要确定指定的对象为文件,需要用-f 比较


-r 确定是否为可读的文件 ,如 -r  /etc/psfile
-s 确定是否为空文件
-w 确定是否能写入文件
-x  确定是否能执行文件
-O 确定是否权限
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2024-6-2 17:35 , Processed in 0.094119 second(s), 20 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表