24 Sep

Linux SHELL if 命令参数说明

分类:电脑技术 » linux shell   出处:本站原创   
放着备查

–b 当file存在并且是块文件时返回真
-c 当file存在并且是字符文件时返回真
-d 当pathname存在并且是一个目录时返回真
-e 当pathname指定的文件或目录存在时返回真
-f 当file存在并且是正规文件时返回真
-g 当由pathname指定的文件或目录存在并且设置了SGID位时返回为真
-h 当file存在并且是符号链接文件时返回真,该选项在一些老系统上无效
-k 当由pathname指定的文件或目录存在并且设置了“粘滞”位时返回真
-p 当file存在并且是命令管道时返回为真
-r 当由pathname指定的文件或目录存在并且可读时返回为真
-s 当file存在文件大小大于0时返回真
-u 当由pathname指定的文件或目录存在并且设置了SUID位时返回真
-w 当由pathname指定的文件或目录存在并且可执行时返回真。一个目录为了它的内容被访问必然是可执行的。
-o 当由pathname指定的文件或目录存在并且被子当前进程的有效用户ID所指定的用户拥有时返回真。
UNIX Shell 里面比较字符写法:

-eq   等于
-ne    不等于
-gt    大于
-lt    小于
-le    小于等于
-ge   大于等于
-z    空串
=     两个字符相等
!=    两个字符不等
-n    非空串
zz from http://blog.penner.cn/2007/03/25/linux-shell-if.html
Tags: ,
23 Jul

查找某时间段修改的文件

分类:电脑技术 » linux shell   出处:本站原创   
需要查找某目录下某个时间段的文件,考虑到目录的特殊性(例如含有空格),做了一些特别的处理(感谢阿阿飛兄弟)

#!/bin/bash

find "$1" | while read name; do
  iname=`file "$name"|awk -F: '{print $2}'| sed -e 's/ //g'`
  if [ ! "$iname" == 'directory' ] && [ ! "$iname" == 'symbolic' ]; then
    filetime=`ls -l "$name" --time-style +%Y%m%d | awk '{print $6}'`
    if [ $2 -le $filetime ] && [ $filetime -le $3 ]; then
      echo $name
    fi
  fi
done


执行:
./findfile  /root 20080710 20080720

对于含有空格的目录如根目录下的test test
可以用./findfile  /test\ test/ 20080701 20080723 这样来执行


有任何意见欢迎提出:)
Tags:
14 Jul

帮朋友写的一个脚本

分类:电脑技术 » linux shell   出处:本站原创   
要求:
1、目录/FTP/下有很多用户目录,分别是某一个公司的英文缩写,FTP目录结构为
/FTP/AAA/DOWNLOAD
/FTP/BBB/DOWNLOAD
......
2、在每一个公司的DOWNLOAD下,每天会有很多文本文件上传过来,但为了便于数据统计,导入到XML乃至数据库中,需要对上传的文件进行批量重命名,命名的格式如:CCO_AAA_SZ2008071401_SZ_V04.test1.txt,其中2008071401是文件修改日期。

3、文件内容如:
引用
AAA公司|上传数据|A经理
0012|560|jack|2006
1323|7787|rose|2008
abcd|yes|35|88


重新命名文件之后,需要对每个文件的内容进行修改,修改要求如下:
以|为分隔符分割字段,删除文件的第一行,将剩下内容每一行|之前的字符删除(包括|符号),然后将|替换成||,最后判断剩下的内容,如果剩下文件内容的第一行不是以13开头,删除!

初步代码如下,还不完善,有不妥的地方希望大家批评指正。

#!/bin/bash
#
for path in /FTP/*/DOWNLOAD ; do
    cd $path
    compay=`pwd | awk -F/ '{print $(NF-1)}'`

    ls -l $1 | grep ^[^d] | awk '{print $9}' | sed -e '/^$/d' | while read name;do
    if [ ! `echo $name | awk '/^CCO/'` ]; then
        modifytime=`ls -l $name --time-style +%Y%m%d | awk '{print $6}'`
        newname=CCO_"$compay"_SZ"$modifytime"01_SZ_V04."$name".txt
        echo "rename $path/$name to $path/$newname ..."
        mv $path/$name $path/$newname
        echo "modify the content of $newname"
        sed -i '1d; s/[^|]*|//; s/|/||/g' $newname
        sed -i '1{/^13/!d}' $newname
    fi
    done
done
Tags:
9 Jul

Linux 下批量更改文件名

分类:电脑技术 » linux shell   出处:本站原创   
比如将所有文件名中带有20080707的字符更改成20080709?


root@ubuntu:~/test# cat re
#!/bin/bash
# code by thatday
#
ls -l $1 | grep ^[^d] | awk '{print $9}'| while read line; do
do=`echo $line | grep $2`

if [ ! "$do" = "" ]; then
   newname=`echo $line | sed 's/'$2'/'$3'/g'`
   echo "rename $line to $newname ..."
   mv $1/$line $1/$newname
fi
done

root@ubuntu:~# chmod +x re


root@ubuntu:~# ls -l test/
total 4
-rw-r--r-- 1 root root    0 Jul  9 13:45 20070808jjj
-rw-r--r-- 1 root root    0 Jul  9 13:45 20070809abc
-rw-r--r-- 1 root root    0 Jul  9 13:45 aaa20070809bb
drwxr-xr-x 2 root root 4096 Jul  9 12:05 bak
-rw-r--r-- 1 root root    0 Jul  9 13:45 ccccc200708091
-rw-r--r-- 1 root root    0 Jul  9 13:45 dddd20070809aj

root@ubuntu:~# ./re test/ 20070809 20070810
rename 20070809abc to 20070810abc ...
rename aaa20070809bb to aaa20070810bb ...
rename ccccc200708091 to ccccc200708101 ...
rename dddd20070809aj to dddd20070810aj ...
root@ubuntu:~# ls test/
20070808jjj  20070810abc  aaa20070810bb  bak  ccccc200708101  dddd20070810aj



Tags:
13 Jun

shell中的几个循环语法

分类:电脑技术 » linux shell   出处:本站原创   
比如生成1~100的循环体

index=1
while [ $index -le 100 ]
do

。。。
index=$(($index +1))
done


#!/bin/bash
while :;do
    ((++index))
    echo $index
    ((index==100))&&break
done


for i in {1..100}
  do
   .......
  done


min=1
max=100
while [ $min -le $max ]
    do
       echo $min
        min=`expr $min + 1`
done


seq 1 100


printf '%d\n' {1..100}
Tags: ,
分页: 1/2 第一页 1 2 下页 最后页 [ 显示模式: 摘要 | 列表 ]