shell脚本--检查文件是否存在

写一个脚本,来检查某个文件是否存在,如果存在,则输出它的详细信息,如果不存在,则提示输出文件不存在。在给出这个脚本之前,先来了解一下如下几个命令:文件upload.zip为例

  1. # ll -h upload.zip


  -rw-r--r-- 1 root root 3.3M 06-28 23:21 upload.zip


  2. # file upload.zip


  upload.zip: Zip archive data, at least v1.0 to extract


  3. # ls -i upload.zip


  1427041 upload.zip


  4. # df -h upload.zip


  文件系统              容量  已用 可用 已用% 挂载点


  /dev/hda3             9.5G  5.7G  3.4G  64% /


  下面的脚本将把这些命令融合在一起,来显示一个文件的详细信息。


  #!/bin/bash


  # This script gives information about a file.


  FILENAME="$1"


  echo "Properties for $FILENAME:"


  if [ -f $FILENAME ]; then


  echo "Size is $(ls -lh $FILENAME | awk "{ print $5 }")"


  echo "Type is $(file $FILENAME | cut -d":" -f2 -)"


  echo "Inode number is $(ls -i $FILENAME | cut -d" " -f1 -)"


  echo "$(df -h $FILENAME | grep -v 文件系统 | awk "{ print "On",$1", \


  which is mounted as the",$6,"partition."}")"


  else


  echo "File does not exist."


  fi


  记得要赋予脚本可执行权限哦!!!!


  chomd u+x wenjian.sh


  执行脚本的结果如下:


  # /jiaoben/wenjian.sh upload.zip


  Properties for upload.zip:


  Size is 3.3M


  Type is  Zip archive data, at least v1.0 to extract


  Inode number is 1427041


  On /dev/hda3, which is mounted as the / partition.


  这样就比我们一个一个敲命令来检查文件的信息要方便多了。


  如果对cut命令不是很了解,可以参考以下说明:


  -----------------------------------------------------------------------


  -----------------------------------------------------------------------


  cut命令可以从一个文本文件或者文本流中提取文本列。


  命令用法:


  cut -b list [-n] [file ...]


  cut -c list [file ...]


  cut -f list [-d delim][-s][file ...]


  上面的-b、-c、-f分别表示字节、字符、字段(即byte、character、field);


  list表示-b、-c、-f操作范围,-n常常表示具体数字;


  file表示的自然是要操作的文本文件的名称;


  delim(英文全写:delimiter)表示分隔符,默认情况下为TAB;


  -s表示不包括那些不含分隔符的行(这样有利于去掉注释和标题)


  上面三种方式中,表示从指定的范围中提取字节(-b)、或字符(-c)、或字段(-f)。


 


  范围的表示方法:


  M


  只有第M项


  M-


  从第M项一直到行尾


  M-N


  从第M项到第N项(包括N)


  -N


  从一行的开始到第N项(包括N)


  -


  从一行的开始到结束的所有项


  范例:


  # cat example


  test2


  this is test1


  # cut -c1-6 example ## print 开头算起前 6 个字元


  test2


  this i


  -c m-n 表示显示每一行的第m个字元到第n个字元。例如:


  ---------file-----------


  wokao 84 25000


  ---------file-----------


  # cut -c 1-5,10-25 file


  wokao 25000


  -f m-n 表示显示第m栏到第n栏(使用tab分隔)。例如:


  ---------file-----------


  wokao 84 25000


  ---------file-----------


  # cut -f 1,3 file


  wokao 25000


  我们经常会遇到需要取出分字段的文件的某些特定字段,例如 /etc/password就是通过":"分隔各个字段的。可以通过cut命令来实现。例如,


  我们希望将系统账号名保存到特定的文件,就可以:


  cut -d":" -f 1 /etc/passwd > /tmp/users


  -d用来定义分隔符,默认为tab键,-f表示需要取得哪个字段


  如:


  使用|分隔


  cut -d"|" -f2 1.test>2.test


  使用:分隔


  cut -d":" -f2 1.test>2.test


  这里使用单引号或双引号皆可。

#!/bin/bash

# This script tar the firmware.if find the firmware file,overide the comresponding file
if [ -f "firmware.tar.gz" ]; then
echo "File does exist."
gzip -d /firmware.tar.gz
tar xvf /firmware.tar -C /
rm -rf /firmware.tar

else
echo "File does not exist."
fi



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值