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

 1. 判断文件是否存在

 -e   $filename

例如:

 

[c-sharp]  view plain copy
  1. #!/bin/bash  
  2. filename=/home/jifeng/shell/file  
  3. if [ -e $filename ]  
  4. then  
  5.  echo "$filename exited"  
  6. fi  

 

 

 

 

2. 判断文件是否为空

[ ! -s $filename ]

特别注意:如果文件存在且为空,-s代表存在不为空,!将他取反

例子:

 

[c-sharp]  view plain copy
  1. #!/bin/bash  
  2. filename=/home/jifeng/shell/file  
  3. echo $filename  
  4. if [[ ! -s $filename ]]  
  5. then  
  6.  echo "file is null"  
  7. else  
  8.  echo "file is not null"  
  9. fi  

 

 

 

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

 

[c-sharp]  view plain copy
  1. #!/bin/bash  
  2. readpath="/home/jifeng/AndesProject"  
  3. for file in $readpath/*  
  4. do  
  5.        echo "$file"  
  6. done  

 

 

你可能感兴趣的:(linux,shell,脚本)