您的位置:首页 > 运维架构 > Shell

Linux脚本(shell)编程(三) 文件操作

2012-12-02 16:30 786 查看
1. 判断文件是否存在

-e $filename
例如:

[c-sharp] view plaincopy

#!/bin/bash

filename=/home/jifeng/shell/file

if [ -e $filename ]

then

echo "$filename exited"

fi

2. 判断文件是否为空
[ ! -s $filename ]
特别注意:如果文件存在且为空,-s代表存在不为空,!将他取反
例子:

[c-sharp] view plaincopy

#!/bin/bash

filename=/home/jifeng/shell/file

echo $filename

if [[ ! -s $filename ]]

then

echo "file is null"

else

echo "file is not null"

fi

3. 遍历一个目录下的所有文件

[c-sharp] view plaincopy

#!/bin/bash

readpath="/home/jifeng/AndesProject"

for file in $readpath/*

do

echo "$file"

done

本文出自 “IT-人生精彩” 博客,请务必保留此出处http://mrwlh.blog.51cto.com/2238823/1076508
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: