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

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

写一个脚本,来检查某个文件是否存在,如果存在,则输出它的详细信息,如果不存在,则提示输出文件不存在。在给出这个脚本之前,先来了解一下如下几个命令:文件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

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

【编辑推荐】

  1. Linux脚本Shell命令之葵花宝典
  2. Linux Shell变量的使用轻松学习
  3. linux学习笔记:认识SHELL
责任编辑:赵宁宁 来源: chinaitlab
相关推荐

2019-08-23 06:22:47

LinuxShell监控脚本

2022-10-24 08:17:29

API算法元素

2024-02-05 13:37:16

Go语言方法

2023-02-01 15:00:45

2021-04-22 06:03:17

SonarQube检查项目CI

2016-12-20 09:30:22

shell脚本linux

2014-08-08 16:17:49

shell脚本linux

2018-03-22 19:30:26

LinuxMeltdownSpectre

2009-12-03 10:06:33

Ubuntushell脚本

2024-02-19 16:15:07

2021-04-21 08:03:34

脚本Shell读取

2021-08-20 10:46:25

Shell脚本文件Linux

2010-03-26 15:28:05

Python编写

2019-08-09 13:50:08

shellLinux

2021-01-08 08:06:19

脚本Shell文件

2021-01-12 10:10:41

shell脚本Linux命令

2015-06-02 14:43:43

shell运维

2011-09-27 13:52:41

2020-06-17 10:42:54

shellshell脚本Linux

2023-10-30 10:40:29

检查用户app注册数据库
点赞
收藏

51CTO技术栈公众号