Linux中shell文件操作大全

1280阅读 0评论2014-09-10 jonas_mao
分类:LINUX

1.创建文件夹
#!/bin/sh
mkdir -m 777 "%%1"

2.创建文件
#!/bin/sh
touch "%%1"

3.删除文件
#!/bin/sh
rm -if "%%1"

4.删除文件夹
#!/bin/sh
rm -rf "%%1"

5.删除一个目录下所有的文件夹
#!/bin/bash
direc="%%1" #$(pwd)
for dir2del in $direc/* ; do
if [ -d $dir2del ]; then
  rm -rf $dir2del
fi
done

6.清空文件夹
#!/bin/bash
direc="%%1" #$(pwd)
rm -if $direc/*
for dir2del in $direc/* ; do
if [ -d $dir2del ]; then
  rm -rf $dir2del
fi
done

7.读取文件
#!/bin/sh
7.1.操作系统默认编码
cat "%%1" | while read line; do
echo $line;
done

7.2.UTF-8编码
cat "%%1" | while read line; do
echo $line;
done

7.3.分块读取
cat "%%1" | while read line; do
echo $line;
done

8.写入文件
#!/bin/sh
cat > "%%1" << EOF
%%2
EOF

tee "%%1" > /dev/null << EOF
%%2
EOF

#sed -i '$a %%2' %%2

9.写入随机文件
#!/bin/sh
cat > "%%1" << EOF
%%2
EOF

tee "%%1" > /dev/null << EOF
%%2
EOF

#sed -i '$a %%2' %%2

10.读取文件属性
#!/bin/bash
file=%%1
file=${file:?'必须给出参数'}
if [ ! -e $file ]; then
    echo "$file 不存在"
    exit 1
fi
if [ -d $file ]; then
    echo "$file 是一个目录"
    if [ -x $file ]; then
        echo "可以"
    else
        echo "不可以"
    fi
    echo "对此进行搜索"  
elif [ -f $file ]; then
    echo "$file 是一个正规文件"
else
    echo "$file不是一个正规文件"
fi
if [ -O $file ]; then
    echo "你是$file的拥有者"
else
    echo "你不是$file的拥有者"
fi
if [ -r $file ]; then
    echo "你对$file拥有"
else
    echo "你并不对$file拥有"
fi
echo "可读权限"
if [ -w $file ]; then
    echo "你对$file拥有"
else
    echo "你并不对$file拥有"
fi
echo "可写权限"
if [ -x $file -a ! -d $file ]; then
    echo "你拥有对$file"
else
    echo "你并不拥有对$file"
fi
echo "可执行的权限"

11.写入文件属性
#!/bin/bash
#修改存放在ext2、ext3、ext4、xfs、ubifs、reiserfs、jfs等文件系统上的文件或目录属性,使用权限超级用户。
#一些功能是由Linux内核版本来支持的,如果Linux内核版本低于2.2,那么许多功能不能实现。同样-D检查压缩文件中的错误的功能,需要2.5.19以上内核才能支持。另外,通过chattr命令修改属性能够提高系统的安全性,但是它并不适合所有的目录。chattr命令不能保护/、/dev、/tmp、/var目录。
chattr [-RV] [-+=AacDdijsSu] [-v version] 文件或目录
  -R:递归处理所有的文件及子目录。
  -V:详细显示修改内容,并打印输出。
  -:失效属性。
  +:激活属性。
  = :指定属性。
  A:Atime,告诉系统不要修改对这个文件的最后访问时间。
  S:Sync,一旦应用程序对这个文件执行了写操作,使系统立刻把修改的结果写到磁盘。
  a:Append Only,系统只允许在这个文件之后追加数据,不允许任何进程覆盖或截断这个文件。如果目录具有这个属性,系统将只允许在这个目录下建立和修改文件,而不允许删除任何文件。
  i:Immutable,系统不允许对这个文件进行任何的修改。如果目录具有这个属性,那么任何的进程只能修改目录之下的文件,不允许建立和删除文件。
  D:检查压缩文件中的错误。
  d:No dump,在进行文件系统备份时,dump程序将忽略这个文件。
  C:Compress,系统以透明的方式压缩这个文件。从这个文件读取时,返回的是解压之后的数据;而向这个文件中写入数据时,数据首先被压缩之后才写入磁盘。
  S:Secure Delete,让系统在删除这个文件时,使用0填充文件所在的区域。
  u:Undelete,当一个应用程序请求删除这个文件,系统会保留其数据块以便以后能够恢复删除这个文件。

12.枚举一个目录中的所有文件夹
#!/bin/bash
OLDIFS=$IFS
IFS=:
for path in $( find "%%1" -type d -printf "%p$IFS")
do
#"$path"
done
IFS=$OLDIFS

13.复制文件夹
#!/bin/sh
cp -rf "%%1" "%%2"

14.复制一个目录下所有的文件夹到另一个目录下
#!/bin/bash
direc="%%1" #$(pwd)
for dir2cp in $direc/* ; do
if [ -d $dir2cp ]; then
  cp $dir2cp "%%2"
fi
done

15.移动文件夹
#!/bin/sh
mv -rf "%%1" "%%2"

16.移动一个目录下所有的文件夹到另一个目录下
#!/bin/bash
direc="%%1" #$(pwd)
for dir2mv in $direc/* ; do
if [ -d $dir2mv ]; then
  mv $dir2mv "%%2"
fi
done

17.以一个文件夹的框架在另一个目录下创建文件夹和空文件
#!/bin/bash
direc="%%1" #$(pwd)
OLDIFS=$IFS
IFS=:
for path in $( find $direc -type d -printf "%p$IFS")
do
mkdir -p "%%2/${path:${#direc}+1}"
done
IFS=$OLDIFS
#cp -a "%%1" "%%2"

表达式 含义
${#string}
{#string}
1,取得字符串长度
string=abc12342341          //等号二边不要有空格
echo ${#string}             //结果11
expr length $string         //结果11
expr "$string" : ".*"       //结果11 分号二边要有空格,这里的:根match的用法差不多2,字符串所在位置
expr index $string '123'    //结果4 字符串对应的下标是从0开始的这个方法让我想起来了js的indexOf,各种语言对字符串的操作方法大方向都差不多,如果有语言基础的话,学习shell会很快的。
3,从字符串开头到子串的最大长度
expr match $string 'abc.*3' //结果9个人觉得这个函数的用处不大,为什么要从开头开始呢。
4,字符串截取
echo ${string:4}      //2342341  从第4位开始截取后面所有字符串
echo ${string:3:3}    //123      从第3位开始截取后面3位
echo ${string:3:6}    //123423   从第3位开始截取后面6位
echo ${string: -4}    //2341  :右边有空格   截取后4位
echo ${string:(-4)}   //2341  同上
expr substr $string 3 3   //123  从第3位开始截取后面3位上面的方法让我想起了,php的substr函数,后面截取的规则是一样的。
5,匹配显示内容
//例3中也有match和这里的match不同,上面显示的是匹配字符的长度,而下面的是匹配的内容
expr match $string '\([a-c]*[0-9]*\)'  //abc12342341
expr $string : '\([a-c]*[0-9]\)'       //abc1
expr $string : '.*\([0-9][0-9][0-9]\)' //341 显示括号中匹配的内容这里括号的用法,是不是根其他的括号用法有相似之处呢,
6,截取不匹配的内容
echo ${string#a*3}     //42341  从$string左边开始,去掉最短匹配子串
echo ${string#c*3}     //abc12342341  这样什么也没有匹配到
echo ${string#*c1*3}   //42341  从$string左边开始,去掉最短匹配子串
echo ${string##a*3}    //41     从$string左边开始,去掉最长匹配子串
echo ${string%3*1}     //abc12342  从$string右边开始,去掉最短匹配子串
echo ${string%%3*1}    //abc12     从$string右边开始,去掉最长匹配子串这里要注意,必须从字符串的第一个字符开始,或者从最后一个开始,
7,匹配并且替换
echo ${string/23/bb}   //abc1bb42341  替换一次
echo ${string//23/bb}  //abc1bb4bb41  双斜杠替换所有匹配
echo ${string/#abc/bb} //bb12342341   #以什么开头来匹配,根php中的^有点像
echo ${string/%41/bb}  //abc123423bb  %以什么结尾来匹配,根php中的$有点像

#!/bin/bash
direc=$(pwd)
for file in "$(direc)/*"
do
if [ "${file##*.}" = "sh" ]; then
xterm -e bash $file
elif [ "${file##*.}" = "bin" ]; then
xterm -e $file
elif [ "${file##*.}" = "run" ]; then
xterm -e $file
elif [ "${file##*.}" = "bundle" ]; then
xterm -e $file
elif [ "${file##*.}" = "pl" ]; then
xterm -e perl $file
elif [ "${file##*.}" = "class" ]; then
xterm -e java ${file%.*}
elif [ "${file##*.}" = "rpm" ]; then
xterm -e rpm -ivh $file
elif [ "${file##*.}" = "rb" ]; then
xterm -e ruby $file
elif [ "${file##*.}" = "py" ]; then
xterm -e python $file
elif [ "${file##*.}" = "jar" ]; then
xterm -e java -jar $file
fi
done
OLDIFS=$IFS
IFS=:
for path in $( find $direc -type d -printf "%p$IFS")
do
for file in `ls $path`
do
if [ "${file##*.}" = "sh" ]; then
xterm -e bash """"$path"/"$file""""
elif [ "${file##*.}" = "bin" ]; then
xterm -e """"$path"/"$file""""
elif [ "${file##*.}" = "run" ]; then
xterm -e """"$path"/"$file""""
elif [ "${file##*.}" = "bundle" ]; then
xterm -e """"$path"/"$file""""
elif [ "${file##*.}" = "pl" ]; then
xterm -e perl """"$path"/"$file""""
elif [ "${file##*.}" = "class" ]; then
xterm -e java """"$path"/"${file%.*}""""
elif [ "${file##*.}" = "rpm" ]; then
xterm -e rpm -ivh """"$path"/"$file""""
elif [ "${file##*.}" = "rb" ]; then
xterm -e ruby """"$path"/"$file""""
elif [ "${file##*.}" = "py" ]; then
xterm -e python """"$path"/"$file""""
elif [ "${file##*.}" = "jar" ]; then
xterm -e java -jar """"$path"/"$file""""
fi
done
done
IFS=$OLDIFS

18.复制文件
#!/bin/sh
cp %%1 %%2

19.复制一个目录下所有的文件到另一个目录
#!/bin/bash
direc="%%1" $(pwd)
for file in "$direc/*"
do
cp "$file" "%%1"
done

20.提取扩展名
#!/bin/sh
%%2=${%%1##.}

21.提取文件名
#!/bin/sh
%%2="$(basename %%1)"

22.提取文件路径
#!/bin/sh
%%2="$(dirname %%1)"

23.替换扩展名
#!/bin/sh
%%3="$(basename %%1)$%%2"

24.追加路径
#!/bin/sh
%%3="$(dirname %%1)/$%%2"

25.移动文件
#!/bin/sh
mv "%%1" "%%2"

26.移动一个目录下所有文件到另一个目录
#!/bin/bash
direc="%%1" #$(pwd)
OLDIFS=$IFS
IFS=:
for file in "$(direc)/*"
do
mv "$file" "%%1"
done
IFS=$OLDIFS

27.指定目录下搜索文件
#!/bin/sh
find -name "%%1"

28.打开文件对话框
#!/bin/sh
%%1="$(Xdialog --fselect '~/' 0 0 2>&1)"

29.文件分割
#!/bin/sh
split -b 2k "%%1"

while read f1 f2 f3
do
    echo $f1 >> f1
    echo $f2 >> f2
    echo $f3 >> f3
done


#!/bin/bash
  linenum=`wc   -l   httperr8007.log|   awk   '{print   $1}'`  
  n1=1  
  file=1  
  while   [   $n1   -lt   $linenum   ]  
  do  
                  n2=`expr   $n1   +   999`  
                  sed   -n   "${n1},   ${n2}p"   httperr8007.log >   file_$file.log    
                  n1=`expr   $n2   +   1`  
                  file=`expr   $file   +   1`  
  done  




其中httperr8007.log为你想分割的大文件,file_$file.log  为分割后的文件,最后为file_1.log,file_2.log,file_3.log……,分割完后的每个文件只有1000行(参数可以自己设置)

split 参数:
-b  :后面可接欲分割成的档案大小,可加单位,例如 b, k, m 等;
-l  :以行数来进行分割;



#按每个文件1000行来分割除

split -l 1000 httperr8007.log httperr

httpaa,httpab,httpac ........

#按照每个文件100K来分割

split -b 100k httperr8007.log http

httpaa,httpab,httpac ........

#!/bin/bash
if [ $# -ne 2 ]; then
echo 'Usage: split file size(in bytes)'
exit
fi

file=$1
size=$2

if [ ! -f $file ]; then
echo "$file doesn't exist"
exit
fi

#TODO: test if $size is a valid integer

filesize=`/bin/ls -l $file | awk '{print $5}'`
echo filesize: $filesize

let pieces=$filesize/$size
let remain=$filesize-$pieces*$size
if [ $remain -gt 0 ]; then
let pieces=$pieces+1
fi
echo pieces: $pieces

i=0
while [ $i -lt $pieces ];
do
echo split: $file.$i:
dd if=$file of=$file.$i bs=$size count=1 skip=$i
let i=$i+1
done

echo "#!/bin/bash" > merge

echo "i=0" >> merge
echo "while [ $i -lt $pieces ];" >> merge
echo "do" >> merge
echo " echo merge: $file.$i" >> merge
echo " if [ ! -f $file.$i ]; then" >> merge
echo " echo merge: $file.$i missed" >> merge
echo " rm -f $file.merged" >> merge
echo " exit" >> merge
echo " fi" >> merge
echo " dd if=$file.$i of=$file.merged bs=$size count=1 seek=$i" >> merge
echo " let i=$i+1" >> merge
echo "done" >> merge
chmod u+x merge'

30.文件合并
#!/bin/sh
cp "%%1"+"%%2" "%%3"

exec 3 exec 4 while read f1 <&3 && read f2 <&4
do
    echo $f1 $f2 >> join.txt
done

#!/bin/bash
if [ $# -ne 2 ]; then
echo 'Usage: split file size(in bytes)'
exit
fi

file=$1
size=$2

if [ ! -f $file ]; then
echo "$file doesn't exist"
exit
fi

#TODO: test if $size is a valid integer

filesize=`/bin/ls -l $file | awk '{print $5}'`
echo filesize: $filesize

let pieces=$filesize/$size
let remain=$filesize-$pieces*$size
if [ $remain -gt 0 ]; then
let pieces=$pieces+1
fi
echo pieces: $pieces

i=0
while [ $i -lt $pieces ];
do
echo split: $file.$i:
dd if=$file of=$file.$i bs=$size count=1 skip=$i
let i=$i+1
done

echo "#!/bin/bash" > merge

echo "i=0" >> merge
echo "while [ $i -lt $pieces ];" >> merge
echo "do" >> merge
echo " echo merge: $file.$i" >> merge
echo " if [ ! -f $file.$i ]; then" >> merge
echo " echo merge: $file.$i missed" >> merge
echo " rm -f $file.merged" >> merge
echo " exit" >> merge
echo " fi" >> merge
echo " dd if=$file.$i of=$file.merged bs=$size count=1 seek=$i" >> merge
echo " let i=$i+1" >> merge
echo "done" >> merge
chmod u+x merge'

31.文件简单加密
#!/bin/bash
#make test && make strings && sudo make install
shc -r -f %%1.sh
#%%1.x
#%%1.x.c

32.文件简单解密
#!/bin/bash
#make test && make strings && sudo make install
shc -r -f %%1.sh
#%%1.x
#%%1.x.c

33.读取ini文件属性
#!/bin/bash
if [ "$%%3" = "" ];then
   sed -n "/\[$%%2\]/,/\[.*\]/{
   /^\[.*\]/d
   /^[ ]*$/d
   s/;.*$//
   p
   }" $1
elif [ "$%%4" = "" ];then
   sed -n "/\[$%%2\]/,/\[.*\]/{
   /^\[.*\]/d
   /^[ ]*$/d
   s/;.*$//
   s/^[ |        ]*$%%3[ | ]*=[ |   ]*\(.*\)[ |     ]*/\1/p
   }" $1
else
       if [ "$%%4" = "#" ];then
            sed "/\[$%%2\]/,/\[.*\]/{
            s/^[ |        ]*$%%3[ |    ]*=.*/ /
            }p" $1 > /tmp/sed$$
            mv /tmp/sed$$ $1
       else
            sed "/\[$2\]/,/\[.*\]/{
            s/^[ |        ]*$%%3[ |    ]*=.*/$%%3=$%%4/
            }p" $1 > /tmp/sed$$
            mv /tmp/sed$$ $%%1
       fi
fi

34.合并一个文件下所有的文件
#!/bin/sh
cat $(ls |grep -E '%%1\.') > %%1

#!/bin/bash
OLDIFS=$IFS
IFS=:
for path in $( find %%1 -type d -printf "%p$IFS")
do
for file in $path/*.c $path/*.cpp
do
if [[ ! "$file" =~ \*.[A-Za-z]+ ]]; then
#"$(path)/$(file)"
fi
done
done
IFS=$OLDIFS

#!/bin/bash
cat <<'EOF'> combine.c
#include
int main()
{
FILE *f1,*f2,*f3;
f1=fopen("a1.txt","r");
f2=fopen("a2.txt","r");
f3=fopen("a3.txt","w");
int a,b;
a=getw(f1);   /*从a1.txt和a2.txt中分别取最小的数a和b*/
b=getw(f2);
while(!feof(f1)&&!feof(f2))  /*两个文件都没结束时,执行循环、比较*/
{
if(a<=b)
{
putw(a,f3);
a=getw(f1);
}
else
{putw(b,f3);
b=getw(f2);
}
   }
if(feof(f1))  /*文件a1.txt结束时,把a2.txt中的数全部输入a3.txt*/
{putw(b,f3);
while((b=getw(f2))!=EOF)
putw(b,f3);
}
if(feof(f2))   /*同上*/
{
putw(a,f3);
while((a=getw(f1))!=EOF)
putw(a,f3);
}
fclose(f1);
fclose(f2);
fclose(f3);
printf("已完成!");
return 0;
}
EOF
gcc -o combine combine.c
if [ $? -eq 0 ]; then
./combine
else
echo 'Compile ERROR'
fi

35.写入ini文件属性
#!/bin/bash
if [ "$%%3" = "" ];then
   sed -n "/\[$%%2\]/,/\[.*\]/{
   /^\[.*\]/d
   /^[ ]*$/d
   s/;.*$//
   p
   }" $1
elif [ "$%%4" = "" ];then
   sed -n "/\[$%%2\]/,/\[.*\]/{
   /^\[.*\]/d
   /^[ ]*$/d
   s/;.*$//
   s/^[ |        ]*$%%3[ | ]*=[ |   ]*\(.*\)[ |     ]*/\1/p
   }" $1
else
       if [ "$%%4" = "#" ];then
            sed "/\[$%%2\]/,/\[.*\]/{
            s/^[ |        ]*$%%3[ |    ]*=.*/ /
            }p" $1 > /tmp/sed$$
            mv /tmp/sed$$ $%%1
       else
            sed "/\[$%%2\]/,/\[.*\]/{
            s/^[ |        ]*$%%3[ |    ]*=.*/$%%3=$%%4/
            }p" $1 > /tmp/sed$$
            mv /tmp/sed$$ $%%1
       fi
fi

36.获得当前路径
#!/bin/sh
%%1=$(pwd)

37.读取XML数据库

如何通过shell命令行读取xml文件中某个属性所对应的值?
例如:
BuildVersion 5
我希望能够通过Unix shell命令对属性键的名称BuildVersion进行查询,返回的结果是5,如何实现呀?
#!/bin/bash
grep BuildVersion|sed 's/.*<.*>\([^<].*\)<.*>.*/\1/'

结果返回的是“BuildVersion”,而不是“5”,如果要查询BuildVersion自动返回数值5应当如何写?

应该没错的。试一下: echo "BuildVersion 5"|grep BuildVersion|sed 's/.*<.*>\([^<].*\)<.*>.*/\1/'我在SL的终端里试,返回值是5

目前需要从xml文件提取数据,想做一个xmlparser.sh
xml 类似这样

  



希望输入 xmlparser.sh a.xml hostip可以返回192.168.0.1


#!/bin/sh

if [ $# -ne 2 ];then
   echo "Usage: $0 "
   exit 0
fi

grep $2 $1|awk '{print $2}'|grep -o "[0-9.]*"



grep $2 $1|awk '{print $2}'|grep -o "[0-9.]*"
改成
grep $2 $1|awk '{print $2}'|grep -Eo "[0-9.]+"
楼上这个有问题,如果我要得到的是

  

中的sharename,那么,呵呵,就错了

我觉得应该先定位到第二个参数“$2”的位置,然后再提取“=”后面的内容

这里有个完整的实现:
Parse Simple XML Files using Bash – Extract Name Value Pairs and Attributes



不过需要安装xmllint.

设计到对多个xml文件进行element的读取和列表。有人做过么?
举个例子,
多个xml文件里面都有

        xxx</titlel><br> </article><br><br> 通过shell读取,然后合并到一起,再生成一个新的xml,但是其他元素不变。<br> <article><br>         <title>aaa</titlel><br> </article><br> <article><br>         <title>bbb</titlel><br> </article><br><br> 如果格式异常简单,没有特例,那么可以用shell实现<br> 如果有可能格式复杂,因为shell的命令所使用的正则表达式都不支持跨行匹配,所以用shell来解决这个问题就绕圈子了。<br> 用perl来作这个工作最直接、简单。perl的XML:DOM模块是专门处理XML文件的。<br><br> 偶倒是觉得,用PHP写Scripts也很方便,功能强大,而且,跨平台,<br><br> #!/bin/sh<br><br><br><br> sed -n '/<article>/{<br><br> N;<br><br> /\n[[:space:]]*<title>/{<br><br>     N;<br><br>     /<article>.*<\/article>/p<br><br>     }<br><br> D;<br><br> n<br><br> }'<br><br><br> 这小段代码能把一个xml文件中,你要的东西拿出来.<br> 你可以用for file in $*把这些信息都>>tmpfile中.<br> 然后用sed 在指定文件的指定位置用r命令把tmpfile粘贴进来~~~~<br><br> 大思路如此^_^  我想有这个东西(只要能正确的跑出结果)后面就不难了吧...<br><br> Name<br> xmllint — command line XML tool<br><br> Synopsis<br> xmllint [[--version] | [--debug] | [--shell] | [--debugent] | [--copy] | [--recover] | [--noent] | [--noout] | [--nonet] | [--htmlout] | [--nowrap] | [--valid] | [--postvalid] | [--dtdvalid URL] | [--dtdvalidfpi FPI] | [--timing] | [--output file] | [--repeat] | [--insert] | [--compress] | [--html] | [--xmlout] | [--push] | [--memory] | [--maxmem nbbytes] | [--nowarning] | [--noblanks] | [--nocdata] | [--format] | [--encode encoding] | [--dropdtd] | [--nsclean] | [--testIO] | [--catalogs] | [--nocatalogs] | [--auto] | [--xinclude] | [--noxincludenode] | [--loaddtd] | [--dtdattr] | [--stream] | [--walker] | [--pattern patternvalue] | [--chkregister] | [--relaxng] | [--schema] | [--c14n]] [xmlfile]<br><br> Introduction<br> The xmllint program parses one or more XML files, specified on the command line as xmlfile. It prints various types of output, depending upon the options selected. It is useful for detecting errors both in XML code and in the XML parser itself.<br><br> It is included in libxml2.<br><br> Options<br> --version<br> Display the version of libxml2 used.<br> --debug<br> Parse a file and output an annotated tree of the in-memory version of the document.<br> --shell<br> Run a navigating shell. Details on available commands in shell mode are below.<br> --debugent<br> Debug the entities defined in the document.<br> --copy<br> Test the internal copy implementation.<br> --recover<br> Output any parsable portions of an invalid document.<br> --noent<br> Substitute entity values for entity references. By default, xmllint leaves entity references in place.<br> --nocdata<br> Substitute CDATA section by equivalent text nodes.<br> --nsclean<br> Remove redundant namespace declarations.<br> --noout<br> Suppress output. By default, xmllint outputs the result tree.<br> --htmlout<br> Output results as an HTML file. This causes xmllint to output the necessary HTML tags surrounding the result tree output so the results can be displayed in a browser.<br> --nowrap<br> Do not output HTML doc wrapper.<br> --valid<br> Determine if the document is a valid instance of the included Document Type Definition (DTD). A DTD to be validated against also can be specified at the command line using the --dtdvalid option. By default, xmllint also checks to determine if the document is well-formed.<br> --postvalid<br> Validate after parsing is completed.<br> --dtdvalid URL<br> Use the DTD specified by URL for validation.<br> --dtdvalidfpi FPI<br> Use the DTD specified by the Public Identifier FPI for validation, note that this will require a Catalog exporting that Public Identifier to work.<br> --timing<br> Output information about the time it takes xmllint to perform the various steps.<br> --output file<br> Define a file path where xmllint will save the result of parsing. Usually the programs build a tree and save it on stdout, with this option the result XML instance will be saved onto a file.<br> --repeat<br> Repeat 100 times, for timing or profiling.<br> --insert<br> Test for valid insertions.<br> --compress<br> Turn on gzip compression of output.<br> --html<br> Use the HTML parser.<br> --xmlout<br> Used in conjunction with --html. Usually when HTML is parsed the document is saved with the HTML serializer, but with this option the resulting document is saved with the XML serializer. This is primarily used to generate XHTML from HTML input.<br> --push<br> Use the push mode of the parser.<br> --memory<br> Parse from memory.<br> --maxmem nnbytes<br> Test the parser memory support. nnbytes is the maximum number of bytes the library is allowed to allocate. This can also be used to make sure batch processing of XML files will not exhaust the virtual memory of the server running them.<br> --nowarning<br> Do not emit warnings from the parser and/or validator.<br> --noblanks<br> Drop ignorable blank spaces.<br> --format<br> Reformat and reindent the output. The $XMLLINT_INDENT environment variable controls the indentation (default value is two spaces " ").<br> --testIO<br> Test user input/output support.<br> --encode encoding<br> Output in the given encoding.<br> --catalogs<br> Use the catalogs from $SGML_CATALOG_FILES. Otherwise /etc/xml/catalog is used by default.<br> --nocatalogs<br> Do not use any catalogs.<br> --auto<br> Generate a small document for testing purposes.<br> --xinclude<br> Do XInclude processing.<br> --noxincludenode<br> Do XInclude processing but do not generate XInclude start and end nodes.<br> --loaddtd<br> Fetch external DTD.<br> --dtdattr<br> Fetch external DTD and populate the tree with inherited attributes.<br> --dropdtd<br> Remove DTD from output.<br> --stream<br> Use streaming API - useful when used in combination with --relaxng or --valid options for validation of files that are too large to be held in memory.<br> --walker<br> Test the walker module, which is a reader interface but for a document tree, instead of using the reader API on an unparsed document it works on a existing in-memory tree. Used in debugging.<br> --chkregister<br> Turn on node registration. Useful for developers testing libxml2 node tracking code.<br> --pattern patternvalue<br> Used to exercise the pattern recognition engine, which can be used with the reader interface to the parser. It allows to select some nodes in the document based on an XPath (subset) expression. Used for debugging.<br> --relaxng schema<br> Use RelaxNG file named schema for validation.<br> --schema schema<br> Use a W3C XML Schema file named schema for validation.<br> --c14n<br> Use the W3C XML Canonicalisation (C14N) to serialize the result of parsing to stdout. It keeps comments in the result.<br> Shell<br> xmllint offers an interactive shell mode invoked with the --shell command. Available commands in shell mode include:<br><br> base<br> display XML base of the node<br> bye<br> leave shell<br> cat node<br> Display node if given or current node.<br> cd path<br> Change the current node to path (if given and unique) or root if no argument given.<br> dir path<br> Dumps information about the node (namespace, attributes, content).<br> du path<br> Show the structure of the subtree under path or the current node.<br> exit<br> Leave the shell.<br> help<br> Show this help.<br> free<br> Display memory usage.<br> load name<br> Load a new document with the given name.<br> ls path<br> List contents of path (if given) or the current directory.<br> pwd<br> Display the path to the current node.<br> quit<br> Leave the shell.<br> save name<br> Saves the current document to name if given or to the original name.<br> validate<br> Check the document for error.<br> write name<br> Write the current node to the given filename.<br> Catalogs<br> Catalog behavior can be changed by redirecting queries to the user's own set of catalogs. This can be done by setting the XML_CATALOG_FILES environment variable to a list of catalogs. An empty one should deactivate loading the default /etc/xml/catalog default catalog.<br><br> Debugging Catalogs<br> Setting the environment variable XML_DEBUG_CATALOG using the command "export XML_DEBUG_CATALOG=" outputs debugging information related to catalog operations.<br><br> Error Return Codes<br> On the completion of execution, Xmllint returns the following error codes:<br><br> 0<br> No error<br> 1<br> Unclassified<br> 2<br> Error in DTD<br> 3<br> Validation error<br> 4<br> Validation error<br> 5<br> Error in schema compilation<br> 6<br> Error writing output<br> 7<br> Error in pattern (generated when [--pattern] option is used)<br> 8<br> Error in Reader registration (generated when [--chkregister] option is used)<br> 9<br> Out of memory error<br><br> Parse Simple XML Files using Bash – Extract Name Value Pairs and Attributes<br><br><br> 2 Comments<br> 1<br> Tweet<br><br><br><br><br> Pratik Sinha | July 31, 2010<br><br><br> I have written up a simple routine par***ML to parse simple XML files to extract unique name values pairs and their attributes. The script extracts all xml tags of the format <abc arg1="hello">xyz</abc> and dynamically creates bash variables which hold values of the attributes as well as the elements. This is a good solution, if you don’t wish to use xpath for some simple xml files. However you will need xmllint installed on your system to use the script. Here’s a sample script which uses the par***ML function<br> #!/bin/bash<br> xmlFile=$1<br><br> function par***ML() {<br>   elemList=( $(cat $xmlFile | tr '\n' ' ' | XMLLINT_INDENT="" xmllint --format - | /bin/grep -e "</.*>$" | while read line; do \<br>     echo $line | sed -e 's/^.*<\///' | cut -d '>' -f 1; \<br>   done) )<br><br>   totalNoOfTags=${#elemList[@]}; ((totalNoOfTags--))<br>   suffix=$(echo ${elemList[$totalNoOfTags]} | tr -d '</>')<br>   suffix="${suffix}_"<br><br>   for (( i = 0 ; i < ${#elemList[@]} ; i++ )); do<br>     elem=${elemList[$i]}<br>     elemLine=$(cat $xmlFile | tr '\n' ' ' | XMLLINT_INDENT="" xmllint --format - | /bin/grep "</$elem>")<br>     echo $elemLine | grep -e "^</[^ ]*>$" 1>/dev/null 2>&1<br>     if [ "0" = "$?" ]; then<br>       continue<br>     fi<br>     elemVal=$(echo $elemLine | tr '\011' '\040'| sed -e 's/^[ ]*//' -e 's/^<.*>\([^<].*\)<.*>$/\1/' | sed -e 's/^[ ]*//' | sed -e 's/[ ]*$//')<br>     xmlElem="${suffix}$(echo $elem | sed 's/-/_/g')"<br>     eval ${xmlElem}=`echo -ne \""${elemVal}"\"`<br>     attrList=($(cat $xmlFile | tr '\n' ' ' | XMLLINT_INDENT="" xmllint --format - | /bin/grep "</$elem>" | tr '\011' '\040' | sed -e 's/^[ ]*//' | cut -d '>' -f 1  | sed -e 's/^<[^ ]*//' | tr "'" '"' | tr '"' '\n'  | tr '=' '\n' | sed -e 's/^[ ]*//' | sed '/^$/d' | tr '\011' '\040' | tr ' ' '>'))<br>     for (( j = 0 ; j < ${#attrList[@]} ; j++ )); do<br>       attr=${attrList[$j]}<br>       ((j++))<br>       attrVal=$(echo ${attrList[$j]} | tr '>' ' ')<br>       attrName=`echo -ne ${xmlElem}_${attr}`<br>       eval ${attrName}=`echo -ne \""${attrVal}"\"`<br>     done<br>   done<br> }<br><br> par***ML<br> echo "$status_xyz |  $status_abc |  $status_pqr" #Variables for each  XML ELement<br> echo "$status_xyz_arg1 |  $status_abc_arg2 |  $status_pqr_arg3 | $status_pqr_arg4" #Variables for each XML Attribute<br> echo ""<br><br> #All the variables that were produced by the par***ML function<br> set | /bin/grep -e "^$suffix"<br><br> The XML File used for the above script example is:<br> <?xml version="1.0"?><br> <status><br>   <xyz arg1="1"> a </xyz><br>   <abc arg2="2"> p </abc><br>   <pqr arg3="3" arg4="a phrase"> x </pqr><br> </status><br><br><br> The root tag, which in this case is “status”, is used as a suffix for all variables. Once the XML file is passed to the function, it dynamically creates the variables $status_xyz, $status_abc, $status_pqr, $status_xyz_arg1, $status_abc_arg2, $status_pqr_arg3 and $status_pqr_arg4.<br><br> The output when the script is ran with the xml file as an argument is<br> @$ bash  par***ML.sh test.xml<br> a |  p |  x<br> 1 |  2 |  3 | a phrase<br><br> status_abc=p<br> status_abc_arg2=2<br> status_pqr=x<br> status_pqr_arg3=3<br> status_pqr_arg4='a phrase'<br> status_xyz=a<br> status_xyz_arg1=1<br><br> This script won’t work for XML files like the one below with duplicate element names.<br> <?xml version="1.0"?><br> <status><br>   <test arg1="1"> a </test><br>   <test arg2="2"> p </test><br>   <test arg3="3" arg4="a phrase"> x </test><br> </status><br><br><br> This script also won’t be able to extract attributes of elements without any CDATA. For eg, the script won’t be able to create variables corresponding to <test arg1="1">. It will only create the variables corresponding to <test1 arg2="2">abc</test1>.<br> <?xml version="1.0"?><br> <status><br>   <test arg1="1"><br>     <test1 arg2="2">abc</test1><br>   </test><br> </status><br><br> 38.写入XML数据库<br> #!/bin/bash<br><br> 39.ZIP压缩文件<br> #!/bin/sh<br> zip -r "/%%1" "%%2"<br><br> 40.ZIP解压缩<br> #!/bin/sh<br> unzip -x "/%%1" "%%2"<br><br> 41.获得应用程序完整路径<br> #!/bin/bash<br><br> 42.ZIP压缩文件夹<br> #!/bin/bash<br><br> 43.递归删除目录下的文件<br> #!/bin/bash<br> rm -if "%%1/*"<br> OLDIFS=$IFS<br> IFS=:<br> for path in $( find %%1 -type d -printf "%p$IFS")<br> do<br> for file in $path/*.c $path/*.cpp<br> do<br> if [[ ! "$file" =~ \*.[A-Za-z]+ ]]; then<br> #"$(path)/$(file)"<br> fi<br> done<br> done<br> IFS=$OLDIFS<br><br> 44.IDEA加密算法<br> #!/bin/bash<br><br> 45.RC6算法<br> #!/bin/bash<br> cat <<'EOF'> rc6.c<br> #include<stdio.h><br> /* Timing data for RC6 (rc6.c)<br><br> 128 bit key:<br> Key Setup:    1632 cycles<br> Encrypt:       270 cycles =    94.8 mbits/sec<br> Decrypt:       226 cycles =   113.3 mbits/sec<br> Mean:          248 cycles =   103.2 mbits/sec<br><br> 192 bit key:<br> Key Setup:    1885 cycles<br> Encrypt:       267 cycles =    95.9 mbits/sec<br> Decrypt:       235 cycles =   108.9 mbits/sec<br> Mean:          251 cycles =   102.0 mbits/sec<br><br> 256 bit key:<br> Key Setup:    1877 cycles<br> Encrypt:       270 cycles =    94.8 mbits/sec<br> Decrypt:       227 cycles =   112.8 mbits/sec<br> Mean:          249 cycles =   103.0 mbits/sec<br><br> */<br><br> #include "../std_defs.h"<br><br> static char *alg_name[] = { "rc6", "rc6.c", "rc6" };<br><br> char **cipher_name()<br> {<br>     return alg_name;<br> }<br><br> #define f_rnd(i,a,b,c,d)                    \<br>         u = rotl(d * (d + d + 1), 5);       \<br>         t = rotl(b * (b + b + 1), 5);       \<br>         a = rotl(a ^ t, u) + l_key;      \<br>         c = rotl(c ^ u, t) + l_key[i + 1]<br><br> #define i_rnd(i,a,b,c,d)                    \<br>         u = rotl(d * (d + d + 1), 5);       \<br>         t = rotl(b * (b + b + 1), 5);       \<br>         c = rotr(c - l_key[i + 1], t) ^ u;  \<br>         a = rotr(a - l_key, u) ^ t<br><br> u4byte  l_key[44];  /* storage for the key schedule         */<br><br> /* initialise the key schedule from the user supplied key   */<br><br> u4byte *set_key(const u4byte in_key[], const u4byte key_len)<br> {   u4byte  i, j, k, a, b, l[8], t;<br><br>     l_key[0] = 0xb7e15163;<br><br>     for(k = 1; k < 44; ++k)<br>        <br>         l_key[k] = l_key[k - 1] + 0x9e3779b9;<br><br>     for(k = 0; k < key_len / 32; ++k)<br><br>         l[k] = in_key[k];<br><br>     t = (key_len / 32) - 1; // t = (key_len / 32);<br><br>     a = b = i = j = 0;<br><br>     for(k = 0; k < 132; ++k)<br>     {   a = rotl(l_key + a + b, 3); b += a;<br>         b = rotl(l[j] + b, b);<br>         l_key = a; l[j] = b;<br>         i = (i == 43 ? 0 : i + 1); // i = (i + 1) % 44; <br>         j = (j == t ? 0 : j + 1);  // j = (j + 1) % t;<br>     }<br><br>     return l_key;<br> };<br><br> /* encrypt a block of text  */<br><br> void encrypt(const u4byte in_blk[4], u4byte out_blk[4])<br> {   u4byte  a,b,c,d,t,u;<br><br>     a = in_blk[0]; b = in_blk[1] + l_key[0];<br>     c = in_blk[2]; d = in_blk[3] + l_key[1];<br><br>     f_rnd( 2,a,b,c,d); f_rnd( 4,b,c,d,a);<br>     f_rnd( 6,c,d,a,b); f_rnd( 8,d,a,b,c);<br>     f_rnd(10,a,b,c,d); f_rnd(12,b,c,d,a);<br>     f_rnd(14,c,d,a,b); f_rnd(16,d,a,b,c);<br>     f_rnd(18,a,b,c,d); f_rnd(20,b,c,d,a);<br>     f_rnd(22,c,d,a,b); f_rnd(24,d,a,b,c);<br>     f_rnd(26,a,b,c,d); f_rnd(28,b,c,d,a);<br>     f_rnd(30,c,d,a,b); f_rnd(32,d,a,b,c);<br>     f_rnd(34,a,b,c,d); f_rnd(36,b,c,d,a);<br>     f_rnd(38,c,d,a,b); f_rnd(40,d,a,b,c);<br><br>     out_blk[0] = a + l_key[42]; out_blk[1] = b;<br>     out_blk[2] = c + l_key[43]; out_blk[3] = d;<br> };<br><br> /* decrypt a block of text  */<br><br> void decrypt(const u4byte in_blk[4], u4byte out_blk[4])<br> {   u4byte  a,b,c,d,t,u;<br><br>     d = in_blk[3]; c = in_blk[2] - l_key[43];<br>     b = in_blk[1]; a = in_blk[0] - l_key[42];<br><br>     i_rnd(40,d,a,b,c); i_rnd(38,c,d,a,b);<br>     i_rnd(36,b,c,d,a); i_rnd(34,a,b,c,d);<br>     i_rnd(32,d,a,b,c); i_rnd(30,c,d,a,b);<br>     i_rnd(28,b,c,d,a); i_rnd(26,a,b,c,d);<br>     i_rnd(24,d,a,b,c); i_rnd(22,c,d,a,b);<br>     i_rnd(20,b,c,d,a); i_rnd(18,a,b,c,d);<br>     i_rnd(16,d,a,b,c); i_rnd(14,c,d,a,b);<br>     i_rnd(12,b,c,d,a); i_rnd(10,a,b,c,d);<br>     i_rnd( 8,d,a,b,c); i_rnd( 6,c,d,a,b);<br>     i_rnd( 4,b,c,d,a); i_rnd( 2,a,b,c,d);<br><br>     out_blk[3] = d - l_key[1]; out_blk[2] = c;<br>     out_blk[1] = b - l_key[0]; out_blk[0] = a;<br> };<br> int main()<br> {<br><br> return 0;<br> }<br> EOF<br> gcc -o rc6 rc6.c<br> if [ $? -eq 0 ]; then<br> ./combine<br> else<br> echo 'Compile ERROR'<br> fi<br><br> 46.Grep<br> #!/bin/bash<br> grep -qE %%1 %%2<br><br> 47.直接创建多级目录<br> #!/bin/bash<br> mkdir -p %%1<br><br> 48.批量重命名<br> #!/bin/bash<br> find $PWD -type f -name '*\.cpp' |sed s/'\.cpp'//g|awk '{MV = "mv"};{C = "\.c"};{ CPP="\.cpp"}; {print MV, $1 CPP , $1 C}'|sh<br> ls | awk -F '-' '{print "mv "$0" "$2}' #去掉带'-'的前缀<br><br> 49.文本查找替换<br> #!/bin/bash<br> sed -e 's:%%2:%%3:g' %%1<br> #sed -e 's/%%2/%%3/g' %%1<br><br> 50.文件关联<br> #!/bin/bash<br><br> 51.批量转换编码从GB2312到Unicode<br> #!/bin/bash<br> scode="gbk"<br> dcode="ucs2"<br> for FILE in $(find $(pwd) -type f)<br> do<br> TMP_file=$(mktemp -p $(pwd))<br> if [ -f $FILE ]; then<br> Fright=$(stat -c %a $FILE)<br> Fuser=$(stat -c %U $FILE)<br> Fgrp=$(stat -c %G $FILE)<br> iconv -f $scode -t $dcode $FILE -o $TMP_file<br> mv $TMP_file $FILE<br> chmod $Fright $FILE<br> chown $Fuser.$Fgrp $FILE<br> fi<br> done<br><br> 52.设置JDK环境变量<br> #!/bin/bash<br> find "$PWD" -type f \( -iname '*.bin' \) -print0 | xargs -0 chmod +x<br> find -type f \( -iname '*.bin' \) -print |<br> while read filename<br> do<br>     case "$filename" in<br>     *.bin)<br>         xterm -e "$filename" && rm -if "$filename"<br>         ;;<br>     esac<br> done<br> OLDIFS=$IFS<br> IFS=$'\n'<br> for line in `cat ~/.bashrc`<br> do<br> if [[ "$line" =~ .*export.* ]]; then<br>     if [[ "$line" =~ .*JAVA_HOME=.* ]]; then<br>       if [[ "$line" =~ =(\/([0-9a-zA-Z._]+))+ ]]; then<br>        javahome=$line<br>       fi<br>     fi<br> fi<br> if [[ "$line" =~ export\ PATH=\$PATH:\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin$ ]];then<br>     javapath=$line<br> fi<br> if [[ "$line" =~ export\ CLASSPATH=.:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib$ ]];then<br>     classpath=$line<br> fi<br> done<br> if [ ! -n "$javahome" ]; then<br> sed -i '$a export JAVA_HOME='$(pwd)'/jdk1.6.0_25' ~/.bashrc<br> else<br> sed -i 's:'${javahome//\\/\\\\}':export JAVA_HOME='$(pwd)'/jdk1.6.0_32:g' ~/.bashrc<br> fi<br> if [ ! -n "$javapath" ]; then<br> sed -i '$a export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin' ~/.bashrc<br> fi<br> if [ ! -n "$classpath" ]; then<br> sed -i '$a export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib' ~/.bashrc<br> fi<br> IFS=$OLDIFS<br><br> #!/bin/bash<br> shift<br> OLDIFS=$IFS<br> IFS=$'\n'<br> for line in `cat ~/TestBash.txt` #~/.bashrc<br> do<br>   if [[ "$line" =~ .*export.* ]]; then<br>     if [[ "$line" =~ export\ CLASSPATH=.:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib$ ]]; then<br>       classpath=$line<br>     elif [[ "$line" =~ export\ PATH=\$PATH:\$CATALINA_HOME/bin$ ]]; then<br>       jbosspath=$line<br> fi<br>     if [[ "$line" =~ .*JAVA_HOME=.* ]]; then<br>       if [[ "$line" =~ =(\/([0-9a-zA-Z._]+))+ ]];then<br>        javahome=$line<br>       fi<br>     elif [[ "$line" =~ .*CATALINA_HOME=.* ]];then<br>       if [[ "$line" =~ =(\/([0-9a-zA-Z._]+))+ ]];then<br>        catalinahome=$line<br>       fi<br>     elif [[ "$line" =~ .*TOMCAT_HOME=.* ]];then<br>       if [[ "$line" =~ =(\/([0-9a-zA-Z._]+))+ ]];then<br>        tomcathome=$line<br>       fi<br>     elif [[ "$line" =~ .*CATALINA_BASE=.* ]];then<br>       if [[ "$line" =~ =(\/([0-9a-zA-Z._]+))+ ]];then<br>        catalinabase=$line<br>       fi<br>     elif [[ "$line" =~ .*JBOSS_HOME=.* ]];then<br>       if [[ "$line" =~ =(\/([0-9a-zA-Z._]+))+ ]];then<br>        jbosshome=$line<br>       fi<br>     fi<br>   elif [[ "$line" =~ ^PATH=\$PATH:\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin$ ]];then<br>     javapath=$line<br>   fi<br>   if [[ "$line" =~ export\ CLASSPATH=.:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib$ ]];then<br>     classpath=$line<br>   fi<br>   if [[ "$line" =~ export\ PATH=\$PATH:\$JBOSS_HOME/bin$ ]];then<br>     jbosspath=$line<br>   fi<br> done<br> if [ ! -n "$javahome" ]; then<br> sed -i '$a export JAVA_HOME='$(pwd)'/jdk1.6.0_24' ~/TestBash.txt #~/.bashrc<br> else<br> sed -i 's:'${javahome//\\/\\\\}':export JAVA_HOME='$(pwd)'/jdk1.6.0_24:g' ~/TestBash.txt<br> fi<br> if [ ! -n "$javapath" ]; then<br> sed -i '$a PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin' ~/TestBash.txt #~/.bashrc<br> fi<br> if [ ! -n "$classpath" ]; then<br> sed -i '$a export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib' ~/TestBash.txt #~/.bashrc<br> fi<br> if [ ! -n "$catalinahome" ]; then<br> sed -i '$a export CATALINA_HOME='$(pwd) ~/TestBash.txt #~/.bashrc<br> else<br> sed -i 's:'${catalinahome//\\/\\\\}':export CATALINA_HOME='$(pwd)':g' ~/TestBash.txt<br> fi<br> if [ ! -n "$tomcathome" ]; then<br> sed -i '$a export TOMCAT_HOME='$(pwd) ~/TestBash.txt #~/.bashrc<br> else<br> sed -i 's:'${tomcathome//\\/\\\\}':export TOMCAT_HOME='$(pwd)':g' ~/TestBash.txt<br> fi<br> if [ ! -n "$catalinabase" ]; then<br> sed -i '$a export CATALINA_BASE='$(pwd) ~/TestBash.txt #~/.bashrc<br> else<br> sed -i 's:'${catalinabase//\\/\\\\}':export CATALINA_BASE='$(pwd)':g' ~/TestBash.txt<br> fi<br> if [ ! -n "$jbosshome" ]; then<br> sed -i '$a export JBOSS_HOME='$(pwd) ~/TestBash.txt #~/.bashrc<br> else<br> sed -i 's:'${jbosshome//\\/\\\\}':export JBOSS_HOME='$(pwd)':g' ~/TestBash.txt<br> fi<br> if [ ! -n "$jbosspath" ]; then<br> sed -i '$a export PATH=$PATH:$CATALINA_HOME/bin' ~/TestBash.txt #~/.bashrc<br> fi<br> IFS=$OLDIFS<br><br> 53.批量转换编码从Unicode到GB2312<br> #!/bin/bash<br> scode="ucs2"<br> dcode="gbk"<br> for FILE in $(find $(pwd) -type f)<br> do<br> TMP_file=$(mktemp -p $(pwd))<br> if [ -f $FILE ]; then<br> Fright=$(stat -c %a $FILE)<br> Fuser=$(stat -c %U $FILE)<br> Fgrp=$(stat -c %G $FILE)<br> iconv -f $scode -t $dcode $FILE -o $TMP_file<br> mv $TMP_file $FILE<br> chmod $Fright $FILE<br> chown $Fuser.$Fgrp $FILE<br> fi<br> done<br><br> 54.删除空文件夹<br> #!/bin/bash<br> rmdir -p %%1<br><br> 55.GB2312文件转UTF-8格式<br> #!/bin/bash<br> iconv -f gbk -t utf8 %%1 -o %%2<br><br> 56.UTF-8文件转GB2312格式<br> #!/bin/bash<br> iconv -f utf8 -t  gbk %%1 -o %%2<br><br> 57.获取文件路径的父路径<br> #!/bin/bash<br> %%1=basename $PWD<br><br> 58.Unicode文件转UTF-8格式<br> #!/bin/bash<br> iconv -f ucs2 -t  utf-8 %%1 -o %%2<br><br> 59.CRC循环冗余校验<br> #!/bin/bash<br> cat <<'EOF'> crc.c<br> #include<stdio.h><br><br> unsigned long int crc32_table[256]; <br><br> unsigned long int ulPolynomial = 0x04c11db7; <br><br> unsigned long int Reflect(unsigned long int ref, char ch) <br><br>   {     unsigned long int value(0); <br><br>         // 交换bit0和bit7,bit1和bit6,类推 <br><br>         for(int i = 1; i < (ch + 1); i++) <br><br>          {            if(ref & 1) <br><br>                       value |= 1 << (ch - i); <br><br>                    ref >>= 1;      } <br><br>         return value; <br><br> } <br><br> init_crc32_table() <br><br>   {     unsigned long int crc,temp; <br><br>         // 256个值 <br><br>         for(int i = 0; i <= 0xFF; i++) <br><br>          {   temp=Reflect(i, 8); <br><br>                crc32_table[i]= temp<< 24; <br><br>                 for (int j = 0; j < 8; j++){ <br><br>              unsigned long int t1,t2; <br><br>   unsigned long int flag=crc32_table[i]&0x80000000; <br><br>                 t1=(crc32_table[i] << 1); <br><br>                 if(flag==0) <br><br>                   t2=0; <br><br>                 else <br><br>                   t2=ulPolynomial; <br><br>                 crc32_table[i] =t1^t2 ;        } <br><br>                crc=crc32_table[i]; <br><br>                crc32_table[i] = Reflect(crc32_table[i], 32); <br>         }<br> }<br> unsigned long GenerateCRC32(char xdata * DataBuf,unsigned long  len) <br><br>   { <br><br>         unsigned long oldcrc32; <br><br>         unsigned long crc32; <br><br>         unsigned long oldcrc; <br><br>         unsigned  int charcnt; <br><br>          char c,t; <br><br>         oldcrc32 = 0x00000000; //初值为0 <br><br>      charcnt=0; <br><br>          while (len--) { <br><br>                  t= (oldcrc32 >> 24) & 0xFF;   //要移出的字节的值 <br><br>      oldcrc=crc_32_tab[t];         //根据移出的字节的值查表 <br><br>                  c=DataBuf[charcnt];          //新移进来的字节值 <br><br>                  oldcrc32= (oldcrc32 << 8) | c;   //将新移进来的字节值添在寄存器末字节中 <br><br>                  oldcrc32=oldcrc32^oldcrc;     //将寄存器与查出的值进行xor运算 <br><br>                  charcnt++; <br><br>         } <br><br>          crc32=oldcrc32; <br><br>          return crc32; <br><br> } <br><br> 参数表可以先在PC机上算出来,也可在程序初始化时完成。下面是用于计算参数表的c语言子程序,在Visual C++ 6.0下编译通过。 <br><br> #include <stdio.h> <br><br> unsigned long int crc32_table[256]; <br><br> unsigned long int ulPolynomial = 0x04c11db7; <br><br> unsigned long int Reflect(unsigned long int ref, char ch) <br><br>   {     unsigned long int value(0); <br><br>         // 交换bit0和bit7,bit1和bit6,类推 <br><br>         for(int i = 1; i < (ch + 1); i++) <br><br>          {            if(ref & 1) <br><br>                       value |= 1 << (ch - i); <br><br>                    ref >>= 1;      } <br><br>         return value; <br><br> }<br> int main()<br> {<br>      unsigned long int crc,temp; <br><br>         // 256个值 <br><br>         for(int i = 0; i <= 0xFF; i++) <br><br>          {<br> temp=Reflect(i, 8);<br>                crc32_table[i]= temp<< 24; <br><br>                 for (int j = 0; j < 8; j++){ <br><br>              unsigned long int t1,t2; <br><br>   unsigned long int flag=crc32_table[i]&0x80000000;<br>                 t1=(crc32_table[i] << 1); <br><br>                 if(flag==0) <br><br>                   t2=0; <br><br>                 else <br><br>                   t2=ulPolynomial; <br><br>                 crc32_table[i] =t1^t2 ;       <br> } <br><br>                crc=crc32_table[i];<br>                crc32_table[i] = Reflect(crc32_table[i], 32);<br>         }<br> return 0;<br> }<br> EOF<br> gcc -o crc crc.c<br> if [ $? -eq 0 ]; then<br> ./combine<br> else<br> echo 'Compile ERROR'<br> fi<br><br> 60.判断是否为空文件<br> #!/bin/bash<br><br> 61.终止程序<br> #!/bin/sh<br> kill -KILL pidof %%1 -s<br> #killall %%1<br><br> 62.定时关机<br> #!/bin/sh<br> shutdown -h %%1 & #23:00<br> #shutdown -h now<br> #halt<br> #/sbin/poweroff<br> #init 0<br><br> 63.显示进程列表<br> #!/bin/sh<br> ps aux<br> #fuser -l<br><br> 64.遍历文件夹列出文件大小<br> #!/bin/sh<br> du -sH "%%1/*"<br><br> 65.GOST算法<br> #!/bin/bash<br><br> 66.对目标压缩文件解压缩到指定文件夹<br> #!/bin/bash<br><br> 67.保存文件时重名自动生成新文件<br> #!/bin/bash<br><br> 68.打开网页<br> #!/bin/sh<br> lynx %%1<br><br> 69.删除空文件夹整合操作<br> #!/bin/bash<br><br> 70.获取磁盘所有分区<br> #!/bin/sh<br> df -k<br><br> 71.激活一个程序或程序关联的文件<br> #!/bin/bash<br><br> 72.MP3播放<br> #!/bin/sh<br> amp "%%1"<br><br> 73.WAV播放<br> #!/bin/sh<br> amp "%%1"<br><br> 74.写图像到剪切板<br> #!/bin/bash<br><br> 75.从剪贴板复制图像到窗体<br> #!/bin/bash<br><br> 76.删除文件夹下的所有文件且不删除文件夹下的文件夹<br> #!/bin/sh<br> rm -if "%%1/*"<br><br> 77.XML遍历结点属性值<br> #!/bin/bash<br><br> 78.Unicode文件转GB2312格式<br> #!/bin/sh<br> iconv -f ucs2 -t  gbk %%1 -o %%2<br><br> 79.开源程序库Xercesc-C++代码工程中内联80.提取包含头文件列表<br> #!/bin/bash<br><br> 81.GB2312文件转Unicode格式<br> #!/bin/sh<br> iconv -f gbk -t  ucs2 %%1 -o %%2<br><br> 82.Java程序打包<br> #!/bin/bash<br><br> 83.UTF-8文件转Unicode格式<br> #!/bin/bash<br> iconv -f utf8 -t  ucs2 %%1 -o %%2<br><br> 84.创建PDF文档<br> #!/bin/bash<br><br> 85.创建Word文档<br> #!/bin/bash<br><br> 86.快速高效的文件加密<br> #!/bin/bash<br><br> 87.从CSV文件构造XML文档<br> #!/bin/bash<br><br> 88.从XML文档生成CSV文件<br> #!/bin/bash<br><br> 89.模拟键盘输入字符串<br> #!/bin/bash<br><br> 90.提取PDF文件中的文本<br> #!/bin/bash<br><br> 91.操作内存映射文件<br> #!/bin/bash<br> 91.1发送内存映射数据<br> #!/bin/bash<br><br> 91.2接收内存映射数据<br> #!/bin/bash<br><br> 92.重定向windows控制台程序的输出信息<br> #!/bin/bash<br><br> 93.基数转序数<br> #!/bin/bash<br><br> 94.数字月份转英文<br> #!/bin/bash<br><br> 95.报表相关<br> #!/bin/bash<br><br> 96.根据进程名获取进程ID<br> #!/bin/bash<br> pidof %%1 -s<br><br> 96.BCP导入<br> #!/bin/bash<br><br> 97.BCP导出<br> #!/bin/bash<br><br><br> 98.计算文件MD5值<br> #!/bin/bash<br> md5sum "%%1"<br><br> 99.计算获取文件夹中文件的MD5值<br> #!/bin/bash<br><br> 100.复制一个目录下所有文件到一个文件夹中<br> #!/bin/bash<br> cp $(find "%%1" -name *.*) "%%2"<br><br> 101.移动一个目录下所有文件到一个文件夹中<br> #!/bin/bash<br> mv $(find "%%1" -name *.*) "%%2"<br><br> 102.文件RSA高级加密<br> 十进制到十六进制<br> typeset -i16 BASE_16_NUM<br> BASE_16_NUM=%%1<br> echo $BASE_16_NUM<br><br> 八进制到十六进制<br> #!/bin/bash<br> typeset -i16 BASE_16_NUM<br> BASE_16_NUM=8#%%1<br> echo $BASE_16_NUM<br><br> 十进制到八进制<br> #!/bin/bash<br> printf %o %%1; echo<br><br> 十进制到十六进制<br> #!/bin/bash<br> printf %x %%1; echo<br><br> 103.计算文件大小<br> #!/bin/bash<br> wc "%%1"<br><br> 104.计算文件夹的大小<br> #!/sbin/ksh<br> dir=%%1<br> (cd $dir;pwd)<br> find $dir -type d -print | du | awk '{print $2, "== ("$1/2"kb)"}' |sort -f |<br> sed -e "s,[^ /]*/([^ /]*) ==,|--1," -e"s,[^ /]*/,| ,g"<br><br> 105.快速获得当前程序的驱动器、路径、文件名和扩展名<br><br> 106.磁盘剩余空间计算<br> #!/bin/bash<br> df -k<br><br> 107.获取当前程序进程ID<br> #!/bin/bash<br> pidof %%1 -s<br><br> 108.全盘搜索文件<br> #!/bin/bash<br> #updatedb<br> #locate %%1<br> slocate %%1<br><br> 109.获得当前登录的用户名<br> #!/bin/bash<br> whoami<br><br> 110.获得所有用户名<br> #!/bin/bash<br> who<br><br> 111.创建MySQL管理用户<br> #!/bin/bash<br> mysqladmin -u root password %%1<br><br> 112.管理MySQL数据库服务器<br> #!/bin/bash<br> 112.1.启动MySQL数据库服务器<br> mysqld -console<br><br> 112.2.登录MySQL数据库服务器<br> 112.2.1.登录本地MySQL数据库服务器<br> mysql -uroot -p%%1<br><br> 112.2.2.登录远程MySQL数据库服务器<br> mysql -h %%1 -u %%2 -p%%3<br><br> 112.3.关闭MySQL数据库服务器<br> mysqladmin -u root shutdown<br> #pkill -9 mysql<br><br> 112.4.测试MySQL数据库服务器<br> mysqlshow || mysqlshow -u root mysql || mysqladmin version status || mysql test<br><br> 113.MySQL执行查询<br> #!/bin/sh<br> mysqladmin -u %%1 -p%%2 SELECT * INTO OUTFILE './bestlovesky.xls' FROM bestlovesky WHERE 1 ORDER BY id DESC  LIMIT 0, 50;<br><br> mysql -u %%1 -p%%2 -e "SELECT * INTO OUTFILE './bestlovesky.xls' FROM bestlovesky WHERE 1 ORDER BY id DESC  LIMIT 0, 50;"<br><br> 114.创建Oracle管理用户<br> #!/bin/sh<br> 114.1.创建新用户<br> create user test identified by test default tablespace ts_test temporary<br> tablespace temp;<br><br> 114.2.给用户角色特权<br> grant connect,resource to test;<br><br> 115.登录Oracle数据库<br> #!/bin/bash<br> sqlplusw<br> sqlplus /nolog<br> conn username/password@Oranet<br> conn system/systempwd@whfc<br> conn sys/syspwd@whfc as sysdba<br><br> 115.创建Oracle表空间<br> #!/bin/bash<br> conn system@whfc01<br> create tablespace ts_test datafile '/data2/oradata/ciis/ts_test01.dbf' size<br><br> 116.添加Oracle数据文件<br> #!/bin/bash<br> alter tablespace ts_test add datafile '/data2/oradata/ciis/ts_test02.dbf' size<br><br> 117.查看Oracle表空间大小<br> #!/bin/bash<br> desc DBA_DATA_FILES<br><br> 118.查看Oracle剩余表空间大小<br> #!/bin/bash<br> desc DBA_FREE_SPACE<br><br> 119.查看Oracle当前用户表名<br> #!/bin/bash<br> select * from tab;<br><br> 120.Oracle创建索引<br> #!/bin/bash<br> CREATE INDEX idx_book_bookid ON book(bookname);<br><br> 121.Oracle创建主键约束<br> #!/bin/bash<br> ALTER TABLE book ADD CONSTRAINT pk_book_bookid PRIMARY KEY (bookid);<br><br> 122.Oracle显示表结构<br> #!/bin/bash<br> desc book<br><br> 123.Oracle查看表的索引<br> #!/bin/bash<br> column index_name format a30<br> select table_name, index_name from user_indexes;<br><br> 124.Oracle查看索引列<br> #!/bin/bash<br> select table_name, index_name, column_name, column_position from user_ind_columns;<br><br> 125.Oracle查看数据段占空间大小<br> #!/bin/bash<br> desc user_segments<br><br> 126.Oracle查看表占空间大小<br> #!/bin/bash<br> select segment_name,segment_type,bytes from user_segments where segment_type='TABLE';<br><br> 127.安全删除USB<br> #!/bin/bash<br> rundll32.exe shell32.dll,Control_RunDLL hotplug.dll<br><br> 128.打开SQL Server Management Studio<br> #!/bin/bash<br> sqlwb %%1.sql<br><br> 129.MySQL数据库导出备份<br> #!/bin/bash<br> mysqldump -u %%1 -p %%2 %%3>%%4.sql<br> mysqldump --opt test > mysql.test //将数据库test导出到mysql.test文件,后面是一个文本文件<br> mysqldump -u root -p123456 --databases dbname > mysql.dbname //就是把数据库dbname导出到文件mysql.dbname中。<br><br> 130.MySQL数据库数据导入<br> mysql -u %%1 -p %%2 %%3<%%4.sql<br> mysqlimport -u root -p123456 < mysql.dbname<br> 将文本数据导入数据库:<br> 文本数据的字段之间用tab键隔开<br> use test<br> load data local infile "文件名" into table 表名;<br> eg: load data local infile "D:/mysql.txt" into table mytable;<br> 导入.sql 文件命令<br> use database<br> source d:/mysql.sql;<br><br> 131.MySQL数据库检查<br> mysqlcheck -o %%3 -u %%1 -p %%2<br><br> 132.MySQL数据表文件修复<br> myisamchk -B -o %%1.myd<br><br> 1,查看数据库状态 及启动停止<br> /etc/init.d/mysqld status<br> /etc/init.d/mysqld start<br> /etc/init.d/mysqld stop<br><br> 2,给用户配置初始密码123456:<br> mysqladmin -u root -password 123456<br><br> 3,修改root用户密码为 abc123<br> mysqladmin -u root -p123456 password abc123<br><br> 4,如果想去掉密码:<br> mysqladmin -u root -pabc123 password ""<br><br> 5,root连接数据库有密码和无密码:<br> mysql -u root(-uroot) -p<br> mysql<br><br> 6,增加用户 test1 密码 abc,让它可以在任何主机上登录,并对所有数据库有查询,插入,修改,删除的权限:<br> 格式: grant select on 数据库.* to 用户名@登录主机 identified by "密码"<br> grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";<br><br> 8,增加一个用户test2,让它只可以在localhost上登录,并可以对数据库mydb进行查询,插入,修改,删除的操作,<br> 这样用户即使使用知道test2的密码,他也无法从internet 上直接访问数据库,只能通过mysql主机上的web页面来访问。<br> grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";<br> grant select,insert,update,delete on mydb.* to test2@localhost identified by ""; 设置无密码<br><br> 9,显示数据库列表:<br> show databases;<br> use mysql 打开库<br> show tables;<br><br> 10,表的操作<br> describle 表名; 显示数据表的结构<br> create database 库名;<br> drop database 库名;<br> create table 表名(字段设定列表)<br> drop table 表名;<br> delete from 表名;清空表记录<br> select * from 表名; 显示表中的记录<br> insert into 表名 values(, ,)<br><br> alter table 表名 add column <字段名><字段选项><br><br> 133.检查端口占用<br> #!/bin/bash<br> netstat -ano<br><br> 134.Linux下检查Apache是否安装<br> #!/bin/bash<br> rpm -qa | grep httpd<br><br> 135.Linux下启动Apache服务<br> #!/bin/bash<br> service httpd start<br><br> 136.Linux下停止Apache服务<br> #!/bin/bash<br> service httpd stop<br><br> 137.Linux下重新启动Apache服务<br> #!/bin/bash<br> service httpd restart<br><br> 138.Linux下自动加载Apache 服务<br> #!/bin/bash<br> chkconfig - level 3 httpd on<br><br> 139.Linux下不自动加载Apache 服务<br> #!/bin/bash<br> chkconfig - level 3 httpd off<br><br> 140.Linux下检查VSFTP是否安装<br> #!/bin/bash<br> rpm -qa | grep vsftpd<br><br> 141.Linux下启动VSFTP服务<br> #!/bin/bash<br> service vsftpd start<br><br> 142.Linux下停止VSFTP服务<br> #!/bin/bash<br> service vsftpd stop<br><br> 143.Linux下重新启动VSFTP服务<br> #!/bin/bash<br> service vsftpd restart<br><br> 144.Linux下检查VSFTP是否被启动<br> #!/bin/bash<br> pstree | grep vsftpd<br><br> 145.Linux下检查Sendmail是否安装<br> #!/bin/bash<br> rpm -qa | grep sendmail<br><br> 146.Linux下启动Sendmail服务<br> #!/bin/bash<br> service sendmail start<br><br> 147.Linux下停止Sendmail服务<br> #!/bin/bash<br> service sendma stop<br><br> 148.Linux下重新启动Sendmail服务<br> #!/bin/bash<br> service sendmail restart<br><br> 149.Linux下自动加载Sendmail 服务<br> #!/bin/bash<br> chkconfig - level 3 sendmail on<br><br> 150.Linux下不自动加载Sendmail 服务<br> #!/bin/bash<br> chkconfig - level 3 sendmail off<br><br> 151.Linux下文本图形界面配置启动服务<br> #!/bin/bash<br> ntsysv<br><br> 152.以数组的方式删除文件夹<br><br> 153.GCC批量编译<br> #!/bin/bash<br> find -type f \( -iname '*.c' -o -iname '*.cpp' \) -print |<br> while read filename<br> do<br>     case "$filename" in<br>     *.c)<br>       gcc "$filename" -o "$(dirname "$filename")"/"$(basename "$filename" .c)"<br>         ;;<br>     *.cpp)<br>         gcc "$filename" -o "$(dirname "$filename")"/"$(basename "$filename" .cpp)"<br>         ;;<br>     esac<br> done<br><br> 154.批量赋予可执行权限<br> #!/bin/bash<br> find "$PWD" -type f \( -iname '*.sh' -o  -iname '*.csh' -o  -iname '*.ksh' -o -iname '*.pl' -o -iname '*.bin' -o -iname '*.run' -o -iname '*.bundle' -o -iname '*.rb' -o -iname '*.py' \) -print0 | xargs -0 chmod +x<br><br> #!/bin/bash<br> for file in *.sh *.pl *.bin *.run *.bundle *.rb *.py<br> do<br> if [[ ! "$file" =~ \*.[A-Za-z]+ ]]; then<br> chmod +x "$(file)"<br> fi<br> done<br> OLDIFS=$IFS<br> IFS=:<br> for path in $( find $(pwd) -type d -printf "%p$IFS")<br> do<br> for file in $path/*.sh $path/*.pl $path/*.bin $path/*.run $path/*.bundle $path/*.rb $path/*.py<br> do<br> if [[ ! "$file" =~ \*.[A-Za-z]+ ]]; then<br> chmod +x "$(path)/$(file)"<br> fi<br> done<br> done<br> IFS=$OLDIFS<br><br> 155.批量执行<br> #!/bin/bash<br> find -type f \( -iname '*.sh' -o  -iname '*.csh' -o  -iname '*.ksh' -o -iname '*.pl' -o -iname '*.bin' -o -iname '*.run' -o -iname '*.bundle' -o -iname '*.bin' -o -iname '*.class' -o -iname '*.rpm' -o -iname '*.rb' -o -iname '*.py' -o -iname '*.jar' \) -print |<br> while read filename<br> do<br>     case "$filename" in<br>     *.sh | *.csh | *.ksh)<br> if [ ! "./""$(basename $filename)" = $0 ]; then<br>         xterm -e "$filename"<br> fi<br>         ;;<br>     *.pl)<br>         xterm -e perl "$filename"<br>         ;;<br>     *.bin | *.run | *.bundle)<br>         xterm -e "$filename"<br>         ;;<br>     *.class)<br>         xterm -e java "$(dirname "$filename")"/"$(basename "$filename" .class)"<br>         ;;<br>     *.rpm)<br>         xterm -e rpm -ivh "$filename"<br>         ;;<br>     *.rb)<br>         xterm -e ruby "$filename"<br>         ;;<br>     *.py)<br>         xterm -e python "$filename"<br>         ;;<br>     *.jar)<br>         xterm -e java -jar "$filename"<br>         ;;<br>     esac<br> done<br><br> #!/bin/bash<br> find -maxdepth 1 -type f \( -iname '*.sh' -o -iname '*.pl' -o -iname '*.bin' -o -iname '*.run' -o -iname '*.bundle' -o -iname '*.bin' -o -iname '*.class' -o -iname '*.rpm' -o -iname '*.rb' -o -iname '*.py' -o -iname '*.jar' \) -print<br> while read file<br> do<br>     case "${file##*.}" in<br>         sh ) xterm -e """"$file"""";;<br>         pl ) xterm -e perl """"$file"""";;<br>         bin ) xterm -e """"$file"""";;<br>         run ) xterm -e """"$file"""";;<br>         bundle ) xterm -e """"$file"""";;<br>         class ) xterm -e java """"${file%.*}"""";;<br>         rpm ) xterm -e rpm -ivh """"$file"""";;<br>         rb ) xterm -e ruby """"$file"""";;<br>         py ) xterm -e python """"$file"""";;<br>         jar ) xterm -e java -jar """"$file"""";;<br>     esac<br> done<br><br> 156.获取操作系统版本<br> #!/bin/bash<br> uname -r<br> #uname -a<br><br> 157.自身复制<br> #!/bin/bash<br> cp $0 "%%1"<br><br> 158.GCC批量创建静态库<br> #!/bin/bash<br> find -type f \( -iname '*.c' -o -iname '*.cpp' \) -print |<br> while read filename<br> do<br>     case "$filename" in<br>     *.c)<br>       g++  -c -o "$(dirname "$filename")"/"$(basename "$filename" .c)".o"" "$filename"<br>         ;;<br>     *.cpp)<br>       g++  -c -o "$(dirname "$filename")"/"$(basename "$filename" .cpp)".o"" "$filename"<br>         ;;<br>     esac<br> done<br> OLDIFS=$IFS<br> IFS=:<br> for path in $( find $(pwd) -type d -printf "%p$IFS")<br> do<br> ar ru $path".a" $path"/*.o" && ranlib $path".a"<br> done<br> IFS=$OLDIFS<br> find "$PWD" -type f \( -iname '*.o' \) -print0 | xargs -0 rm -if<br><br> 159.Java批量打包EJB<br> #!/bin/bash<br> find "$PWD" -type f \( -iname '*.java' \) -print0 | xargs -0 javac<br> OLDIFS=$IFS<br> IFS=:<br> for path in $( find $(pwd) -type d -printf "%p$IFS")<br> do<br> jar -cvf "$(path".jar")" "$(path"/*.*")" && cp "$(path".jar")" "$(JBOSS_HOME"/server/default/deploy")"<br> done<br> IFS=$OLDIFS<br><br> find "$PWD" -type f \( -iname '*.class' \) -print0 | xargs -0 rm -if<br><br> 160.获取环境变量<br><br> 161.dd<br> #!/bin/bash<br> dd<br><br> 162.显示只有小写字母的文件<br> #!/bin/bash<br> ls -1|awk '/^[[:lower:]].*/'<br><br> 163.Zip压缩目录中的所有文件<br> #!/bin/bash<br> direc="%%1" #$(pwd)<br> targetpath="%%2"<br> OLDIFS=$IFS<br> IFS=:<br> for path in $( find $direc -type d -printf "%p$IFS")<br> do<br> mkdir -p "$targetpath/${path:${#direc}+1}"<br> for file in $path/*<br> do<br> if [ -f $file ]; then<br> zip -j "$targetpath/${path:${#direc}+1}/${file:${#path}+1}.zip" "$file"<br> fi<br> done<br> done<br> IFS=$OLDIFS<br><br> 164.Zip解压缩目录中的所有文件<br> #!/bin/bash<br> direc="%%1" #$(pwd)<br> targetpath="%%2"<br> OLDIFS=$IFS<br> IFS=:<br> for path in $( find $direc -type d -printf "%p$IFS")<br> do<br> mkdir -p "$targetpath/${path:${#direc}+1}"<br> unzip -x "$path/*.zip" -d "$targetpath/${path:${#direc}+1}"<br> done<br> IFS=$OLDIFS<br><br> 165.分布式复制文件夹<br> #!/bin/bash<br> direc="%%1" #$(pwd)<br> targetpath="%%2"<br> OLDIFS=$IFS<br> IFS=:<br> for path in $( find $direc -type d -printf "%p$IFS")<br> do<br> mkdir -p "$targetpath/${path:${#direc}+1}"<br> rm -if "$targetpath/${path:${#direc}+1}/*.tmp"<br> for file in $path/*<br> do<br> if [ -f $file ]; then<br> cp "$file" "$targetpath/${path:${#direc}+1}/${file:${#path}+1}.tmp"<br> mv "$targetpath/${path:${#direc}+1}/${file:${#path}+1}.tmp" "$targetpath/${path:${#direc}+1}/${file:${#path}+1}"<br> fi<br> done<br> done<br> IFS=$OLDIFS<br><br> 166.注册反注册组件<br> #!/bin/bash<br> regsvr32 "%%1"<br><br> 167.LZMA<br> #!/bin/bash<br><br> 168.CAB压缩文件<br> #!/bin/bash<br><br> 169.CAB解压缩文件<br> #!/bin/bash<br><br><br> 170.锁定屏幕<br> #!/bin/sh<br> RUNDLL32.exe USER32,LockWorkStation<br><br> 171.以其它用户的身份运行程序<br> #!/bin/bash<br><br> 172.添加系统用户<br> #!/bin/sh<br> useradd "%%1"<br><br> 173.删除系统用户<br> #!/bin/sh<br> userdel "%%1"<br><br> 174.添加用户组<br> #!/bin/sh<br> groupadd -g 2000 "%%1"<br><br> 175.删除用户组<br> #!/bin/sh<br> groupdel "%%1"<br><br> 176.赋予管理员权限<br> #!/bin/bash<br><br><br> 177.收回管理员权限<br> #!/bin/bash<br><br><br> 178.遍历目录产生删除文件的脚本<br> #!/bin/bash<br><br><br> 179.LZW压缩文件<br> #!/bin/bash<br> z<br><br> 180.LZW解压缩文件<br> #!/bin/bash<br> z<br><br> 181.递归赋予目录权限<br> #!/bin/bash<br> direc="%%1" #$(pwd)<br> OLDIFS=$IFS<br> IFS=:<br> for path in $( find $direc -type d -printf "%p$IFS")<br> do<br> chown -R root.root "$path"<br> done<br> IFS=$OLDIFS<br><br> 182.卸载RPM包<br> #!/bin/sh<br> rpm -e  "%%1"<br><br> 183.删除源文件中的注释<br> #!/bin/sh<br><br> 184.设置目录下所有文件属性为可写<br> #!/bin/sh<br><br> 185.统计目录下所有文件的总共行数<br> #!/bin/sh<br> cat * |wc<br> ls *|xargs wc -l<br> find ./ -name "*c" | xargs wc -l<br><br> 186.删除自身<br> #!/bin/rm<br> exit 65<br> #rm $0<br><br> 187.打开终端<br> #!/bin/bash -l<br><br> 188.弹出光驱<br> #!/bin/sh<br> eject<br><br> 189.收回光驱<br> #!/bin/sh<br> eject -t<br><br> 190.磁盘总空间计算<br><br> 191.解析CSV文件<br><br> 192.按行保存文件为数组<br><br> 193.MySQL执行SQL文件<br> mysqladmin -u %%1 -p%%2 < %%3.sql<br><br> mysql -u %%1 -p%%2 -e "SOURCE %%3.sql"</span></div> </div> <div class="page">上一篇:<a href="/uid-26421509-id-4415060.html">CPU私有变量(per-CPU变量)</a><br> 下一篇:<a href="/uid-26421509-id-4569391.html">TCP原理学习之 快速恢复算法</a></div> <footer> <a href="http://blog.itpub.net/">ITPUB博客</a> | <a href="http://www.itpub.net">ITPUB论坛</a> | <a href="http://bbs.chinaunix.net/">chinaunix论坛</a><br> 北京皓辰网域网络信息技术有限公司. 版权所有 </footer> <script> $(function(){ $("#fenlei").click(function(){ $("#left_1").show(); $("#left_2").show(); }); }) </script> </body> </html> <script language="javascript" src="http://stat.it168.com/pv.js"></script> <script> function sendPV(){ var pvTrack = new PvTrack(); pvTrack.type = 35; // 频道类别ID pvTrack.channel = 531; // 频道ID pvTrack.pageType = 0; pvTrack.track(); } window.setTimeout("sendPV()", 1000); </script>