Linux-Shell脚本编程-学习-6-Shell编程-使用结构化命令-文件比较-case编程

简介: 这一片主要说test文件的比较,文件比较在日常使用的频率比较高,这里重点把每个部分都试着说说看

. 检车目录 -d



-d测试会检查指定的文件名是否在系统上以目录的形式存在,当我们要写文件到某个目录之前,或者是将文件放置到某个目录位置的时候,就需要使用-d来检测这个目录是否存在



#!/bin/bash



#look before you leap



if [ -d $HOME ]

   then

   echo " your Home directory exists"

   cd $HOME

   ls -a

else

   echo " there is a problem with your HOME directory"

fi

image.png


运行截图,代码的意思就是要检测HOME这个目录是否存在,如果这个目录存在就切换到这个目录,并且执行 ls -a命令,输出HOme目录下的所有文件




2. 检查对象是否存在 -e



-e比较允许你在脚本中使用对象前检查文件或者目录是否寻再



#!/bin/bash


#chesking if a directory exists


if [ -e $HOME ]

   then

   echo "ok,on the directory.now to check the file"

       if [ -e $HOME/testing ]

           then

               echo "addending date to existing file"

               date >> $HOME/testing

       else

           echo "create a file"

           date > $HOME/testing

       fi

else

   echo "sorry you do not have a Home directory"

fi

这段代码的意思就是先要检测HOME文件是否存在,如果存在,那么就检测testing文件是否存在,如果存在就吧date的数据追加写到testing文件中,如果testing文件补充存在就创建testing文件,并把date数据写入到文件中

 

image.png


3. 检查文件 -f



确定指定的对象是个文件 -f,区分一个文件名是目录还是文件,需要使用 -f



#!/bin/bash


#cheak if a file


if [ -e $HOME ]

   then

       echo "the object exists,is it a file?"

       if [ -f $HOME ]

           then

               echo " yes it is a file"

           else

               echo " no if is not a file"

               if [ -f $HOME/.bash_history ]

                   then

                       echo " but this is a file"

               else

                   echo "this is not a file too"

               fi

       fi

else

   echo "sorry the object dose not exists"

fi


exit 0

image.png

这段代码的意思就是首先检查HOME目录是否存在,如果存在,则判断home是不是一个文件,如果是就是输出这是一个文件,如果不是,就输出这不是一个文件,并且判断home下的bash_history是不是一个文件,如果是就输出是,如果不是就输出不是,最后如果HOME文件不存在,这直接执行最后一段代码,输出这个对象不存在。



4. 检查文件是否可读 -r




当我在尝试从文件中读取数据是,最好先判断一下该文件是否可读。



#!/bin/bash


#testing if you can read a file


pwfile=/etc/shadow


if [ -f $pwfile ];then

   if [ -r $pwfile ];then

       tail $pwfile

   else

       echo " sorry o am unable to read the $pwfile"

   fi

else

   echo " sorry $pwfile is not a file "

fi

/etc/shadow文件还有系统用户加密后的密码,所以他对系统上的普通用户是不可读的


image.png


5.检查空文件-s




当我们要删除一个文件的时候,有时候需要查看这个文件是否是一个空文件,不过这里要注意的是,-s检测到文件是控空文件的时候返回的退出码是1





#!/bin/bash


#testing if a file is empty


file=testfile

touch $file


if [ -s $file ];then

   echo "the $file file exists and has date in"

else

   echo "the $file exists and is empty"

fi

date > $file

if [ -s $file ];then

   echo "the $file file exists and has date in"

else

   echo "the $file exists and is empty"

fi

image.png


6. 检查文件是否可写 -w

判断是是否对该文件有写权限

#!/bin/bash


#check if a file is writeable


logfile=$HOME/logtest

touch $logfile


chmod u-w $logfile


new=`date +%y%m%d-%H%M`


if [ -w $logfile ];then

   echo "the program ran at : $logfile" > $logfile

   echo "the first attempt succeeded"

else

   echo "the first attempt failes"

fi

chmod u+w $logfile


if [ -w $logfile ];then

   echo "the program ran at : $logfile" > $logfile

   echo "the second attempt succeeded"

else

   echo "the second attempt failes"

fi





image.png


7.检查文件是否可执行 -x



-x比较是一个渐变的判断一个特定文件是否有可执行的权限的方法。



#!/bin/bash


#testing file execution


if [ -x shelltest2.sh ];then

   echo " you can run the script:"

   ./shelltest2.sh

else

   echo " you are unable to execute the script"

fi

image.png


8. 检查文件所属关系 -O



-O比较允许你轻松的测试你是否是文件属主



#!/bin/bash


#check file ownership


if [ -O /etc/passwd ];then

   echo " yao are the file owner"

else

   echo " you are not file owner"

fi

image.png


9. 检查默认属组关系




#!/bin/bash


#check file group test


if [ -G $HOME/shellcode/testfile ];then

   echo " yao are in the same grouup"

else

   echo " the file is not owned by tour group"

fi



10. 检查文件日期





#!/bin/bash


#test file dates


if [ $HOME/shellcode/testfile -nt $HOME/shellcode/testfile1 ];then

   echo " testfile new than testfile1"

else

   echo " testfile1 new than testfile"

fi





到这里,文件的test就基本介绍完了,后面的讲继续介绍



还有一部分这里就不做详细解释,大家可以自行百度学习一下



1. 复合条件测试



【】&&【】


【】||【】


2. if-then高级特性




((expression))


【【expression】】



3. case命令




case variable in

pattern1 | pattern2) commands1;;

pattern3)commands2;;

*) default commands;;


目录
相关文章
|
7天前
|
存储 Unix Shell
shell脚本编程基础
【9月更文挑战第4天】
24 12
|
6天前
|
Shell Linux
Shell 编程 编写hello word
Shell 编写hello word
28 5
|
21天前
|
Shell KVM 虚拟化
Shell 数组编程
【8月更文挑战第22天】 Shell 数组编程
38 10
|
24天前
|
Shell 数据处理 C++
【震撼揭秘】Python正则VS Shell正则:一场跨越编程边界的史诗级对决!你绝不能错过的精彩较量,带你领略文本处理的极致魅力!
【8月更文挑战第19天】正则表达式是文本处理的强大工具,在Python与Shell中有广泛应用。两者虽语法各异,但仍共享许多基本元素,如`.`、`*`及`[]`等。Python通过`re`模块支持丰富的功能,如非捕获组及命名捕获组;而Shell则依赖`grep`、`sed`和`awk`等命令实现类似效果。尽管Python提供了更高级的特性和函数,Shell在处理文本文件方面仍有其独特优势。选择合适工具需根据具体需求和个人偏好决定。
22 1
|
4月前
|
Shell
删除常规文件及隐藏文件shell脚本
删除常规文件及隐藏文件shell脚本
72 1
|
Shell Android开发 Apache
常用工具类,文件和内存的大小获取,shell脚本的执行
/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.
786 0
|
Shell Perl Windows
一段文件相关的shell脚本解读
CODE: #!/sbin/ksh dir=${1:-.} (cd $dir;pwd) find $dir -type d -print | du | awk '{print $2, "== ("$1/2"kb)"}' |sort -f | sed ...
762 0
|
Shell
复制查到到的文件到另一个目录(shell脚本)
Test:find ../../res.bak/item/ -type f -name "*.jpg" |xargs -n1 -i echo {}."abc" 控制台上可看到如下信息:   find .
766 0