Freebsd下 批量修改dos格式文件为unix格式 并删除空行

# !/usr/local/bin/php -q
<? php
/*
    用于批量修改windows下的文件格式为unix下的文件格式,
    同时把连续的空白行去掉
*/
if ( $argc > 1 )
{
    
array_shift ( $argv );
    
$dir   =   $argv [ 0 ];
    
$type   =   $argv [ 1 ];
    
if ( $argv [ 2 ])
    {
        
$ext   =   "  -maxdepth $argv[2] " ;
    }
}
else
{
    
exit ( ' no parames ' );
}
chdir ( $dir );
$command   =   sprintf ( ' find . -type f -name "*.%s" ' , $type );
if ( $ext )
{
    
$command   .=   $ext ;
}
// $command = 'find . -type f';
$str   =   shell_exec ( $command );
$arr   =   explode ( " " , $str );
$command2   =   " /usr/local/bin/dos2unix _FILE_ " ;
$command3   =   " /usr/bin/sed '/^$/d' _FILE_ > _FILE2_ " ;
for ( $i = 0 ; $i < count ( $arr ); $i ++ )
{    
    
if ( strlen ( $arr [ $i ]) > 3  )
    {
        
$cmd2   =   str_replace ( " _FILE_ " , $arr [ $i ] , $command2 );
        
print   $cmd2 . " " ;
        
shell_exec ( $cmd2 );
        
        
$filename2   =   $arr [ $i ] . ' .bak ' ;
        
$command33   =   str_replace ( " _FILE_ " , $arr [ $i ] , $command3 );
        
$cmd3   =   str_replace ( " _FILE2_ " , $filename2 , $command33 );
        
print   $cmd3 . " " ;
        
shell_exec ( $cmd3 );
        
        
$cmd4   =   " /bin/mv $filename2 $arr[$i] " ;
        
print   $cmd4 . " " ;
        
print   " ---------------------------------------------------------- " ;
        
shell_exec ( $cmd4 );
        
        
unset ( $cmd2 , $cmd3 , $cmd4 , $filename2 );
    
    }
}
print   " + ====================================================================+ " ;
print   " parse file number:  " . count ( $arr ) . " " ;
?>
 

你可能感兴趣的:(unix,command,cmd,dos,File,FreeBSD)