Linux中shell文件操作大全

释放双眼,带上耳机,听听看~!

Linux中shell文件操作大全

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.删除一个目录下所有的文件夹


1
2
3
4
5
6
7
8
1#!/bin/bash  
2direc="%%1" #$(pwd)  
3for dir2del in $direc/* ; do  
4if [ -d $dir2del ]; then  
5  rm -rf $dir2del  
6fi  
7done  
8

6.清空文件夹


1
2
3
4
5
6
7
8
9
1#!/bin/bash  
2direc="%%1" #$(pwd)  
3rm -if $direc/*  
4for dir2del in $direc/* ; do  
5if [ -d $dir2del ]; then  
6  rm -rf $dir2del  
7fi  
8done
9

7.读取文件
#!/bin/sh
7.1.操作系统默认编码


1
2
3
4
1cat "%%1" | while read line; do  
2echo $line;  
3done  
4

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.写入文件


1
2
3
4
5
6
7
8
9
1#!/bin/sh  
2cat > "%%1" << EOF  
3%%2  
4EOF  
5tee "%%1" > /dev/null << EOF  
6%%2  
7EOF  
8sed -i '$a %%2' %%2  
9

9.写入随机文件


1
2
3
4
5
6
7
8
9
1#!/bin/sh  
2cat > "%%1" << EOF  
3%%2  
4EOF  
5tee "%%1" > /dev/null << EOF  
6%%2  
7EOF  
8sed -i '$a %%2' %%2  
9

10.读取文件属性


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
1#!/bin/bash  
2file=%%1  
3file=${file:?'必须给出参数'}  
4if [ ! -e $file ]; then  
5    echo "$file 不存在"  
6    exit 1  
7fi  
8if [ -d $file ]; then  
9    echo "$file 是一个目录"  
10    if [ -x $file ]; then  
11        echo "可以"  
12    else  
13        echo "不可以"  
14    fi  
15    echo "对此进行搜索"    
16elif [ -f $file ]; then  
17    echo "$file 是一个正规文件"  
18else  
19    echo "$file不是一个正规文件"  
20fi  
21if [ -O $file ]; then  
22    echo "你是$file的拥有者"  
23else  
24    echo "你不是$file的拥有者"  
25fi  
26if [ -r $file ]; then  
27    echo "你对$file拥有"  
28else  
29    echo "你并不对$file拥有"  
30fi  
31echo "可读权限"  
32if [ -w $file ]; then  
33    echo "你对$file拥有"  
34else  
35    echo "你并不对$file拥有"  
36fi  
37echo "可写权限"  
38if [ -x $file -a ! -d $file ]; then  
39    echo "你拥有对$file"  
40else  
41    echo "你并不拥有对$file"  
42fi  
43echo "可执行的权限"  
44

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.枚举一个目录中的所有文件夹


1
2
3
4
5
6
7
8
9
1#!/bin/bash  
2OLDIFS=$IFS  
3IFS=:  
4for path in $( find "%%1" -type d -printf "%p$IFS")  
5do  
6#"$path"  
7done  
8IFS=$OLDIFS  
9

13.复制文件夹
#!/bin/sh
cp -rf "%%1" "%%2"
14.复制一个目录下所有的文件夹到另一个目录下


1
2
3
4
5
6
7
8
1#!/bin/bash  
2direc="%%1" \#$(pwd)  
3for dir2cp in $direc/\* ; do  
4if [ -d $dir2cp ]; then  
5  cp $dir2cp "%%2"  
6fi  
7done
8

15.移动文件夹
#!/bin/sh
mv -rf "%%1" "%%2"
16.移动一个目录下所有的文件夹到另一个目录下


1
2
3
4
5
6
7
8
1#!/bin/bash  
2direc="%%1" \#$(pwd)  
3for dir2mv in $direc/\* ; do  
4if [ -d $dir2mv ]; then  
5  mv $dir2mv "%%2"  
6fi  
7done  
8

17.以一个文件夹的框架在另一个目录下创建文件夹和空文件


1
2
3
4
5
6
7
8
9
10
11
1#!/bin/bash  
2direc="%%1" #$(pwd)  
3OLDIFS=$IFS  
4IFS=:  
5for path in $( find $direc -type d -printf "%p$IFS")  
6do  
7mkdir -p "%%2/${path:${#direc}+1}"  
8done  
9IFS=$OLDIFS  
10#cp -a "%%1" "%%2"  
11

表达式 含义
${#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中的$有点像


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
1#!/bin/bash  
2direc=$(pwd)  
3for file in "$(direc)/\*"  
4do  
5if [ "${file##*.}" = "sh" ]; then  
6xterm -e bash $file  
7elif [ "${file##*.}" = "bin" ]; then  
8xterm -e $file  
9elif [ "${file##*.}" = "run" ]; then  
10xterm -e $file  
11elif [ "${file##*.}" = "bundle" ]; then  
12xterm -e $file  
13elif [ "${file##*.}" = "pl" ]; then  
14xterm -e perl $file  
15elif [ "${file##*.}" = "class" ]; then  
16xterm -e java ${file%.*}  
17elif [ "${file##*.}" = "rpm" ]; then  
18xterm -e rpm -ivh $file  
19elif [ "${file##*.}" = "rb" ]; then  
20xterm -e ruby $file  
21elif [ "${file##*.}" = "py" ]; then  
22xterm -e python $file  
23elif [ "${file##*.}" = "jar" ]; then  
24xterm -e java -jar $file  
25fi  
26done  
27OLDIFS=$IFS  
28IFS=:  
29for path in $( find $direc -type d -printf "%p$IFS")  
30do  
31for file in `ls $path`  
32do  
33if [ "${file##*.}" = "sh" ]; then  
34xterm -e bash """"$path"/"$file""""  
35elif [ "${file##*.}" = "bin" ]; then  
36xterm -e """"$path"/"$file""""  
37elif [ "${file##*.}" = "run" ]; then  
38xterm -e """"$path"/"$file""""  
39elif [ "${file##*.}" = "bundle" ]; then  
40xterm -e """"$path"/"$file""""  
41elif [ "${file##*.}" = "pl" ]; then  
42xterm -e perl """"$path"/"$file""""  
43elif [ "${file##*.}" = "class" ]; then  
44xterm -e java """"$path"/"${file%.\*}""""  
45elif [ "${file##*.}" = "rpm" ]; then  
46xterm -e rpm -ivh """"$path"/"$file""""  
47elif [ "${file##*.}" = "rb" ]; then  
48xterm -e ruby """"$path"/"$file""""  
49elif [ "${file##*.}" = "py" ]; then  
50xterm -e python """"$path"/"$file""""  
51elif [ "${file##*.}" = "jar" ]; then  
52xterm -e java -jar """"$path"/"$file""""  
53fi  
54done  
55done  
56IFS=$OLDIFS  
57

18.复制文件
#!/bin/sh
cp %%1 %%2
19.复制一个目录下所有的文件到另一个目录


1
2
3
4
5
6
7
1#!/bin/bash  
2direc="%%1" $(pwd)  
3for file in "$direc/*"  
4do  
5cp "$file" "%%1"  
6done
7

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.移动一个目录下所有文件到另一个目录


1
2
3
4
5
6
7
8
9
10
1#!/bin/bash  
2direc="%%1" #$(pwd)  
3OLDIFS=$IFS  
4IFS=:  
5for file in "$(direc)/*"  
6do  
7mv "$file" "%%1"  
8done  
9IFS=$OLDIFS  
10

27.指定目录下搜索文件
#!/bin/sh
find -name "%%1"
28.打开文件对话框
#!/bin/sh
%%1="$(Xdialog –fselect '~/' 0 0 2>&1)"
29.文件分割


1
2
3
4
5
6
7
8
9
1#!/bin/sh  
2split -b 2k "%%1"  
3while read f1 f2 f3  
4do  
5    echo $f1 >> f1  
6    echo $f2 >> f2  
7    echo $f3 >> f3  
8done
9

1
2
3
4
5
6
7
8
9
10
11
12
1#!/bin/bash  
2  linenum=`wc   -l   httperr8007.log|   awk   '{print   $1}'`    
3  n1=1    
4  file=1    
5  while   [   $n1   -lt   $linenum   ]    
6  do    
7                  n2=`expr   $n1   +   999`
8                  sed   -n   "${n1},   ${n2}p"   httperr8007.log >   file_$file.log      
9                  n1=`expr   $n2   +   1`    
10                  file=`expr   $file   +   1`
11  done    
12

其中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 ……..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
1#!/bin/bash  
2if [ $\# -ne 2 ]; then  
3echo 'Usage: split file size(in bytes)'  
4exit  
5fi  
6file=$1  
7size=$2  
8if [ ! -f $file ]; then  
9echo "$file doesn't exist"  
10exit  
11fi  
12
13#TODO: test if $size is a valid integer  
14filesize=`/bin/ls -l $file | awk '{print $5}'`
15echo filesize: $filesize  
16let pieces=$filesize/$size  
17let remain=$filesize-$pieces\*$size  
18if [ $remain -gt 0 ]; then  
19let pieces=$pieces+1  
20fi  
21echo pieces: $pieces  
22i=0  
23while [ $i -lt $pieces ];  
24do  
25echo split: $file.$i:  
26dd if=$file of=$file.$i bs=$size count=1 skip=$i  
27let i=$i+1  
28done  
29echo "#!/bin/bash" > merge  
30echo "i=0" >> merge  
31echo "while [ $i -lt $pieces ];" >> merge  
32echo "do" >> merge  
33echo " echo merge: $file.$i" >> merge  
34echo " if [ ! -f $file.$i ]; then" >> merge  
35echo " echo merge: $file.$i missed" >> merge  
36echo " rm -f $file.merged" >> merge  
37echo " exit" >> merge  
38echo " fi" >> merge  
39echo " dd if=$file.$i of=$file.merged bs=$size count=1 seek=$i" >> merge  
40echo " let i=$i+1" >> merge  
41echo "done" >> merge  
42chmod u+x merge'  
43

30.文件合并


1
2
3
4
5
6
7
8
9
1#!/bin/sh  
2cp "%%1"+"%%2" "%%3"  
3exec 3<f1  
4exec 4<f2  
5while read f1 <&3 && read f2 <&4  
6do  
7    echo $f1 $f2 >> join.txt  
8done
9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
1#!/bin/bash  
2if [ $\# -ne 2 ]; then  
3echo 'Usage: split file size(in bytes)'  
4exit  
5fi  
6file=$1  
7size=$2  
8if [ ! -f $file ]; then  
9echo "$file doesn't exist"  
10exit  
11fi  
12#TODO: test if $size is a valid integer  
13filesize=`/bin/ls -l $file | awk '{print $5}'`
14echo filesize: $filesize  
15let pieces=$filesize/$size  
16let remain=$filesize-$pieces\*$size  
17if [ $remain -gt 0 ]; then  
18let pieces=$pieces+1  
19fi  
20echo pieces: $pieces  
21i=0  
22while [ $i -lt $pieces ];  
23do  
24echo split: $file.$i:  
25dd if=$file of=$file.$i bs=$size count=1 skip=$i  
26let i=$i+1  
27done  
28echo "#!/bin/bash" > merge  
29echo "i=0" >> merge  
30echo "while [ $i -lt $pieces ];" >> merge  
31echo "do" >> merge  
32echo " echo merge: $file.$i" >> merge  
33echo " if [ ! -f $file.$i ]; then" >> merge  
34echo " echo merge: $file.$i missed" >> merge  
35echo " rm -f $file.merged" >> merge  
36echo " exit" >> merge  
37echo " fi" >> merge  
38echo " dd if=$file.$i of=$file.merged bs=$size count=1 seek=$i" >> merge  
39echo " let i=$i+1" >> merge  
40echo "done" >> merge  
41chmod u+x merge'
42

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文件属性


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1#!/bin/bash  
2if [ "$%%3" = "" ];then  
3   sed -n "/[$%%2]/,/[.*]/{  
4   /^[.*]/d  
5   /^[ ]*$/d  
6   s/;.*$//  
7   p  
8   }" $1  
9elif [ "$%%4" = "" ];then  
10   sed -n "/[$%%2]/,/[.*]/{  
11   /^[.*]/d  
12   /^[ ]*$/d  
13   s/;.*$//  
14   s/^[ |        ]*$%%3[ | ]*=[ |   ]*(.*)[ |     ]*/\1/p  
15   }" $1  
16else  
17       if [ "$%%4" = "#" ];then  
18            sed "/[$%%2]/,/[.*]/{  
19            s/^[ |        ]*$%%3[ |    ]*=.*/ /  
20            }p" $1 > /tmp/sed$$  
21            mv /tmp/sed$$ $1  
22       else  
23            sed "/[$2]/,/[.*]/{  
24            s/^[ |        ]*$%%3[ |    ]*=.*/$%%3=$%%4/  
25            }p" $1 > /tmp/sed$$  
26            mv /tmp/sed$$ $%%1  
27       fi  
28fi  
29

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1#!/bin/bash  
2OLDIFS=$IFS  
3IFS=:  
4for path in $( find %%1 -type d -printf "%p$IFS")  
5do  
6for file in $path/*.c $path/*.cpp  
7do  
8if [[ ! "$file" =~ \*.[A-Za-z]+ ]]; then  
9#"$(path)/$(file)"  
10fi  
11done  
12done  
13IFS=$OLDIFS
14

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
1#!/bin/bash  
2cat <<'EOF'> combine.c  
3#include<stdio.h>  
4int main()  
5{  
6FILE *f1,*f2,*f3;  
7f1=fopen("a1.txt","r");  
8f2=fopen("a2.txt","r");  
9f3=fopen("a3.txt","w");  
10int a,b;  
11a=getw(f1);   /*从a1.txt和a2.txt中分别取最小的数a和b*/  
12b=getw(f2);  
13while(!feof(f1)&&!feof(f2))  /*两个文件都没结束时,执行循环、比较*/  
14{  
15if(a<=b)  
16{  
17putw(a,f3);  
18a=getw(f1);  
19}  
20else  
21{putw(b,f3);  
22b=getw(f2);  
23}  
24   }  
25if(feof(f1))  /*文件a1.txt结束时,把a2.txt中的数全部输入a3.txt*/  
26{putw(b,f3);  
27while((b=getw(f2))!=EOF)  
28putw(b,f3);  
29}  
30if(feof(f2))   /*同上*/  
31{  
32putw(a,f3);  
33while((a=getw(f1))!=EOF)  
34putw(a,f3);  
35}  
36fclose(f1);  
37fclose(f2);  
38fclose(f3);  
39printf("已完成!");  
40return 0;  
41}  
42EOF  
43gcc -o combine combine.c  
44if [ $? -eq 0 ]; then  
45./combine  
46else  
47echo 'Compile ERROR'  
48fi  
49

35.写入ini文件属性


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1#!/bin/bash  
2if [ "$%%3" = "" ];then  
3   sed -n "/\[$%%2\]/,/\[.\*\]/{  
4   /^\[.\*\]/d  
5   /^[ ]\*$/d  
6   s/;.\*$//  
7   p  
8   }" $1  
9elif [ "$%%4" = "" ];then  
10   sed -n "/\[$%%2\]/,/\[.\*\]/{  
11   /^\[.\*\]/d  
12   /^[ ]\*$/d  
13   s/;.\*$//  
14   s/^[ |        ]\*$%%3[ | ]\*=[ |   ]\*\(.\*\)[ |     ]\*/\1/p  
15   }" $1  
16else  
17       if [ "$%%4" = "\#" ];then  
18            sed "/\[$%%2\]/,/\[.\*\]/{  
19            s/^[ |        ]\*$%%3[ |    ]\*=.\*/ /  
20            }p" $1 > /tmp/sed$$  
21            mv /tmp/sed$$ $%%1  
22       else  
23            sed "/\[$%%2\]/,/\[.\*\]/{  
24            s/^[ |        ]\*$%%3[ |    ]\*=.\*/$%%3=$%%4/  
25            }p" $1 > /tmp/sed$$  
26            mv /tmp/sed$$ $%%1  
27       fi  
28fi
29

36.获得当前路径
#!/bin/sh
%%1=$(pwd)
37.读取XML数据库
如何通过shell命令行读取xml文件中某个属性所对应的值?
例如:


1
2
1<key>BuildVersion</key> <string>5</string>
2

我希望能够通过Unix shell命令对属性键的名称BuildVersion进行查询,返回的结果是5,如何实现呀?


1
2
3
1#!/bin/bash  
2grep BuildVersion|sed 's/.*<.*>\([^<].*\)<.*>.*/\1/'  
3

结果返回的是“BuildVersion”,而不是“5”,如果要查询BuildVersion自动返回数值5应当如何写?
应该没错的。试一下: echo "<key>BuildVersion</key> <string>5</string>"|grep BuildVersion|sed 's/.<.>([^<].)<.>.*/\1/'我在SL的终端里试,返回值是5
目前需要从xml文件提取数据,想做一个xmlparser.sh
xml 类似这样


1
2
3
4
1&lt;result&gt;  
2   &lt;shareinfo hostip=&quot;192.168.0.1&quot; sharename=&quot;abcd&quot; password=&quot;abc123&quot;&gt;&lt;/shareinfo&gt;  
3&lt;/result&gt;
4

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


1
2
3
4
5
6
7
1#!/bin/sh  
2if [ $\# -ne 2 ];then  
3   echo &quot;Usage: $0 &lt;xmlfile&gt; &lt;key&gt;&quot;  
4   exit 0  
5fi  
6grep $2 $1|awk &#x27;{print $2}&#x27;|grep -o &quot;[0-9.]\*&quot;
7


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

楼上这个有问题,如果我要得到的是


1
2
3
4
1&lt;result&gt;  
2   &lt;shareinfo hostip=&quot;192.168.0.1&quot; sharename=&quot;abcd&quot; password=&quot;abc123&quot;&gt;&lt;/shareinfo&gt;  
3&lt;/result&gt;  
4

中的sharename,那么,呵呵,就错了
我觉得应该先定位到第二个参数“$2”的位置,然后再提取“=”后面的内容
这里有个完整的实现:
Parse Simple XML Files using Bash – Extract Name Value Pairs and Attributes
http://www.humbug.in/2010/parse-simple-xml-files-using-bash-extract-name-value-pairs-and-attributes/
不过需要安装xmllint.
设计到对多个xml文件进行element的读取和列表。有人做过么?
举个例子,
多个xml文件里面都有


1
2
3
4
1&lt;article&gt;  
2        &lt;title&gt;xxx&lt;/titlel&gt;  
3&lt;/article&gt;
4

通过shell读取,然后合并到一起,再生成一个新的xml,但是其他元素不变。


1
2
3
4
5
6
7
1&lt;article&gt;  
2        &lt;title&gt;aaa&lt;/titlel&gt;  
3&lt;/article&gt;  
4&lt;article&gt;  
5        &lt;title&gt;bbb&lt;/titlel&gt;  
6&lt;/article&gt;  
7

如果格式异常简单,没有特例,那么可以用shell实现
如果有可能格式复杂,因为shell的命令所使用的正则表达式都不支持跨行匹配,所以用shell来解决这个问题就绕圈子了。
用perl来作这个工作最直接、简单。perl的XML:DOM模块是专门处理XML文件的。
偶倒是觉得,用PHP写Scripts也很方便,功能强大,而且,跨平台,


1
2
3
4
5
6
7
8
9
10
11
1#!/bin/sh  
2sed -n &#x27;/&lt;article&gt;/{  
3N;  
4/\n[[:space:]]*&lt;title&gt;/{  
5    N;  
6    /&lt;article&gt;.*&lt;\/article&gt;/p  
7    }  
8D;  
9n  
10}&#x27;  
11

这小段代码能把一个xml文件中,你要的东西拿出来.
你可以用for file in $*把这些信息都>>tmpfile中.
然后用sed 在指定文件的指定位置用r命令把tmpfile粘贴进来~~~~
大思路如此 我想有这个东西(只要能正确的跑出结果)后面就不难


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
1xmllint — command line XML tool  
2Synopsis  
3xmllint [[--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]  
4Introduction  
5The 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.  
6It is included in libxml2.  
7Options  
8--version  
9Display the version of libxml2 used.  
10--debug  
11Parse a file and output an annotated tree of the in-memory version of the document.  
12--shell  
13Run a navigating shell. Details on available commands in shell mode are below.  
14--debugent  
15Debug the entities defined in the document.  
16--copy  
17Test the internal copy implementation.  
18--recover  
19Output any parsable portions of an invalid document.  
20--noent  
21Substitute entity values for entity references. By default, xmllint leaves entity references in place.  
22--nocdata  
23Substitute CDATA section by equivalent text nodes.  
24--nsclean  
25Remove redundant namespace declarations.  
26--noout  
27Suppress output. By default, xmllint outputs the result tree.  
28--htmlout  
29Output 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.  
30--nowrap  
31Do not output HTML doc wrapper.  
32--valid  
33Determine 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.  
34--postvalid  
35Validate after parsing is completed.  
36--dtdvalid URL  
37Use the DTD specified by URL for validation.  
38--dtdvalidfpi FPI  
39Use the DTD specified by the Public Identifier FPI for validation, note that this will require a Catalog exporting that Public Identifier to work.  
40--timing  
41Output information about the time it takes xmllint to perform the various steps.  
42--output file  
43Define 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.  
44--repeat  
45Repeat 100 times, for timing or profiling.  
46--insert  
47Test for valid insertions.  
48--compress  
49Turn on gzip compression of output.  
50--html  
51Use the HTML parser.  
52--xmlout  
53Used 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.  
54--push  
55Use the push mode of the parser.  
56--memory  
57Parse from memory.  
58--maxmem nnbytes  
59Test 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.  
60--nowarning  
61Do not emit warnings from the parser and/or validator.  
62--noblanks  
63Drop ignorable blank spaces.  
64--format  
65Reformat and reindent the output. The $XMLLINT_INDENT environment variable controls the indentation (default value is two spaces &quot; &quot;).  
66--testIO  
67Test user input/output support.  
68--encode encoding  
69Output in the given encoding.  
70--catalogs  
71Use the catalogs from $SGML_CATALOG_FILES. Otherwise /etc/xml/catalog is used by default.  
72--nocatalogs  
73Do not use any catalogs.  
74--auto  
75Generate a small document for testing purposes.  
76--xinclude  
77Do XInclude processing.  
78--noxincludenode  
79Do XInclude processing but do not generate XInclude start and end nodes.  
80--loaddtd  
81Fetch external DTD.  
82--dtdattr  
83Fetch external DTD and populate the tree with inherited attributes.  
84--dropdtd  
85Remove DTD from output.  
86--stream  
87Use 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.  
88--walker  
89Test 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.  
90--chkregister  
91Turn on node registration. Useful for developers testing libxml2 node tracking code.  
92--pattern patternvalue  
93Used 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.  
94--relaxng schema  
95Use RelaxNG file named schema for validation.  
96--schema schema  
97Use a W3C XML Schema file named schema for validation.  
98--c14n  
99Use the W3C XML Canonicalisation (C14N) to serialize the result of parsing to stdout. It keeps comments in the result.  
100Shell  
101xmllint offers an interactive shell mode invoked with the --shell command. Available commands in shell mode include:  
102base  
103display XML base of the node  
104bye  
105leave shell  
106cat node  
107Display node if given or current node.  
108cd path  
109Change the current node to path (if given and unique) or root if no argument given.  
110dir path  
111Dumps information about the node (namespace, attributes, content).  
112du path  
113Show the structure of the subtree under path or the current node.  
114exit  
115Leave the shell.  
116help  
117Show this help.  
118free  
119Display memory usage.  
120load name  
121Load a new document with the given name.  
122ls path  
123List contents of path (if given) or the current directory.  
124pwd  
125Display the path to the current node.  
126quit  
127Leave the shell.  
128save name  
129Saves the current document to name if given or to the original name.  
130validate  
131Check the document for error.  
132write name  
133Write the current node to the given filename.  
134Catalogs  
135Catalog behavior can be changed by redirecting queries to the user&#x27;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.  
136Debugging Catalogs  
137Setting the environment variable XML_DEBUG_CATALOG using the command &quot;export XML_DEBUG_CATALOG=&quot; outputs debugging information related to catalog operations.  
138Error Return Codes  
139On the completion of execution, Xmllint returns the following error codes:  
1400  
141No error  
1421  
143Unclassified  
1442  
145Error in DTD  
1463  
147Validation error  
1484  
149Validation error  
1505  
151Error in schema compilation  
1526  
153Error writing output  
1547  
155Error in pattern (generated when [--pattern] option is used)  
1568  
157Error in Reader registration (generated when [--chkregister] option is used)  
1589  
159Out of memory error  
160Parse Simple XML Files using Bash – Extract Name Value Pairs and Attributes  
1612 Comments  
1621  
163Tweet  
164Pratik Sinha | July 31, 2010  
165I 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 &lt;abc arg1=&quot;hello&quot;&gt;xyz&lt;/abc&gt; 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  
166

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
1#!/bin/bash  
2xmlFile=$1  
3function pa***ML() {  
4  elemList=( $(cat $xmlFile | tr &#x27;\n&#x27; &#x27; &#x27; | XMLLINT_INDENT=&quot;&quot; xmllint --format - | /bin/grep -e &quot;&lt;/.*&gt;$&quot; | while read line; do \  
5    echo $line | sed -e &#x27;s/^.*&lt;\///&#x27; | cut -d &#x27;&gt;&#x27; -f 1; \  
6  done) )  
7  totalNoOfTags=${#elemList[@]}; ((totalNoOfTags--))  
8  suffix=$(echo ${elemList[$totalNoOfTags]} | tr -d &#x27;&lt;/&gt;&#x27;)  
9  suffix=&quot;${suffix}_&quot;  
10  for (( i = 0 ; i &lt; ${#elemList[@]} ; i++ )); do  
11    elem=${elemList[$i]}  
12    elemLine=$(cat $xmlFile | tr &#x27;\n&#x27; &#x27; &#x27; | XMLLINT_INDENT=&quot;&quot; xmllint --format - | /bin/grep &quot;&lt;/$elem&gt;&quot;)  
13    echo $elemLine | grep -e &quot;^&lt;/[^ ]*&gt;$&quot; 1&gt;/dev/null 2&gt;&amp;1  
14    if [ &quot;0&quot; = &quot;$?&quot; ]; then  
15      continue  
16    fi  
17    elemVal=$(echo $elemLine | tr &#x27;\011&#x27; &#x27;\040&#x27;| sed -e &#x27;s/^[ ]*//&#x27; -e &#x27;s/^&lt;.\*&gt;\([^&lt;].*\)&lt;.*&gt;$/\1/&#x27; | sed -e &#x27;s/^[ ]*//&#x27; | sed -e &#x27;s/[ ]*$//&#x27;)  
18    xmlElem=&quot;${suffix}$(echo $elem | sed &#x27;s/-/_/g&#x27;)&quot;  
19    eval ${xmlElem}=`echo -ne \&quot;&quot;${elemVal}&quot;\&quot;`
20    attrList=($(cat $xmlFile | tr &#x27;\n&#x27; &#x27; &#x27; | XMLLINT_INDENT=&quot;&quot; xmllint --format - | /bin/grep &quot;&lt;/$elem&gt;&quot; | tr &#x27;\011&#x27; &#x27;\040&#x27; | sed -e &#x27;s/^[ ]*//&#x27; | cut -d &#x27;&gt;&#x27; -f 1  | sed -e &#x27;s/^&lt;[^ ]*//&#x27; | tr &quot;&#x27;&quot; &#x27;&quot;&#x27; | tr &#x27;&quot;&#x27; &#x27;\n&#x27;  | tr &#x27;=&#x27; &#x27;\n&#x27; | sed -e &#x27;s/^[ ]*//&#x27; | sed &#x27;/^$/d&#x27; | tr &#x27;\011&#x27; &#x27;\040&#x27; | tr &#x27; &#x27; &#x27;&gt;&#x27;))  
21    for (( j = 0 ; j &lt; ${#attrList[@]} ; j++ )); do  
22      attr=${attrList[$j]}  
23      ((j++))  
24      attrVal=$(echo ${attrList[$j]} | tr &#x27;&gt;&#x27; &#x27; &#x27;)  
25      attrName=`echo -ne ${xmlElem}_${attr}`
26      eval ${attrName}=`echo -ne \&quot;&quot;${attrVal}&quot;\&quot;`
27    done  
28  done  
29}  
30
31par***ML  
32echo &quot;$status_xyz |  $status_abc |  $status_pqr&quot; #Variables for each  XML ELement  
33echo &quot;$status_xyz_arg1 |  $status_abc_arg2 |  $status_pqr_arg3 | $status_pqr_arg4&quot; #Variables for each XML Attribute  
34echo &quot;&quot;  
35#All the variables that were produced by the par***ML function  
36set | /bin/grep -e &quot;^$suffix&quot;  
37The XML File used for the above script example is:  
38&lt;?xml version=&quot;1.0&quot;?&gt;  
39&lt;status&gt;  
40  &lt;xyz arg1=&quot;1&quot;&gt; a &lt;/xyz&gt;  
41  &lt;abc arg2=&quot;2&quot;&gt; p &lt;/abc&gt;  
42  &lt;pqr arg3=&quot;3&quot; arg4=&quot;a phrase&quot;&gt; x &lt;/pqr&gt;  
43&lt;/status&gt;  
44The 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.  
45The output when the script is ran with the xml file as an argument is  
46@$ bash  par***ML.sh test.xml  
47a |  p |  x  
481 |  2 |  3 | a phrase  
49status_abc=p  
50status_abc_arg2=2  
51status_pqr=x  
52status_pqr_arg3=3  
53status_pqr_arg4=&#x27;a phrase&#x27;  
54status_xyz=a  
55status_xyz_arg1=1  
56This script won’t work for XML files like the one below with duplicate element names.  
57&lt;?xml version=&quot;1.0&quot;?&gt;  
58&lt;status&gt;  
59  &lt;test arg1=&quot;1&quot;&gt; a &lt;/test&gt;  
60  &lt;test arg2=&quot;2&quot;&gt; p &lt;/test&gt;  
61  &lt;test arg3=&quot;3&quot; arg4=&quot;a phrase&quot;&gt; x &lt;/test&gt;  
62&lt;/status&gt;  
63This 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 &lt;test arg1=&quot;1&quot;&gt;. It will only create the variables corresponding to &lt;test1 arg2=&quot;2&quot;&gt;abc&lt;/test1&gt;.  
64&lt;?xml version=&quot;1.0&quot;?&gt;  
65&lt;status&gt;  
66  &lt;test arg1=&quot;1&quot;&gt;  
67    &lt;test1 arg2=&quot;2&quot;&gt;abc&lt;/test1&gt;  
68  &lt;/test&gt;  
69&lt;/status&gt;  
70

38.写入XML数据库
#!/bin/bash
39.ZIP压缩文件
#!/bin/sh
zip -r "/%%1" "%%2"
40.ZIP解压缩
#!/bin/sh
unzip -x "/%%1" "%%2"
41.获得应用程序完整路径
#!/bin/bash
42.ZIP压缩文件夹
#!/bin/bash
43.递归删除目录下的文件


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1#!/bin/bash  
2rm -if &quot;%%1/*&quot;  
3OLDIFS=$IFS  
4IFS=:  
5for path in $( find %%1 -type d -printf &quot;%p$IFS&quot;)  
6do  
7for file in $path/*.c $path/*.cpp  
8do  
9if [[ ! &quot;$file&quot; =~ \*.[A-Za-z]+ ]]; then  
10\#&quot;$(path)/$(file)&quot;  
11fi  
12done  
13done  
14IFS=$OLDIFS  
15

44.IDEA加密算法
#!/bin/bash
45.RC6算法


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
1#!/bin/bash  
2cat &lt;&lt;&#x27;EOF&#x27;&gt; rc6.c  
3#include&lt;stdio.h&gt;  
4/* Timing data for RC6 (rc6.c)  
5128 bit key:  
6Key Setup:    1632 cycles  
7Encrypt:       270 cycles =    94.8 mbits/sec  
8Decrypt:       226 cycles =   113.3 mbits/sec  
9Mean:          248 cycles =   103.2 mbits/sec  
10192 bit key:  
11Key Setup:    1885 cycles  
12Encrypt:       267 cycles =    95.9 mbits/sec  
13Decrypt:       235 cycles =   108.9 mbits/sec  
14Mean:          251 cycles =   102.0 mbits/sec  
15256 bit key:  
16Key Setup:    1877 cycles  
17Encrypt:       270 cycles =    94.8 mbits/sec  
18Decrypt:       227 cycles =   112.8 mbits/sec  
19Mean:          249 cycles =   103.0 mbits/sec  
20*/  
21#include &quot;../std_defs.h&quot;  
22static char *alg_name[] = { &quot;rc6&quot;, &quot;rc6.c&quot;, &quot;rc6&quot; };  
23char **cipher_name()  
24{  
25    return alg_name;  
26}  
27#define f_rnd(i,a,b,c,d)                    \  
28        u = rotl(d * (d + d + 1), 5);       \  
29        t = rotl(b * (b + b + 1), 5);       \  
30        a = rotl(a ^ t, u) + l_key;      \  
31        c = rotl(c ^ u, t) + l_key[i + 1]  
32#define i_rnd(i,a,b,c,d)                    \  
33        u = rotl(d * (d + d + 1), 5);       \  
34        t = rotl(b * (b + b + 1), 5);       \  
35        c = rotr(c - l_key[i + 1], t) ^ u;  \  
36        a = rotr(a - l_key, u) ^ t  
37u4byte  l_key[44];  /* storage for the key schedule         */  
38/* initialise the key schedule from the user supplied key   */  
39u4byte *set_key(const u4byte in_key[], const u4byte key_len)  
40{   u4byte  i, j, k, a, b, l[8], t;  
41    l_key[0] = 0xb7e15163;  
42    for(k = 1; k &lt; 44; ++k)  
43         
44        l_key[k] = l_key[k - 1] + 0x9e3779b9;  
45    for(k = 0; k &lt; key_len / 32; ++k)  
46        l[k] = in_key[k];  
47    t = (key_len / 32) - 1; // t = (key_len / 32);  
48    a = b = i = j = 0;  
49    for(k = 0; k &lt; 132; ++k)  
50    {   a = rotl(l_key + a + b, 3); b += a;  
51        b = rotl(l[j] + b, b);  
52        l_key = a; l[j] = b;  
53        i = (i == 43 ? 0 : i + 1); // i = (i + 1) % 44;   
54        j = (j == t ? 0 : j + 1);  // j = (j + 1) % t;  
55    }  
56    return l_key;  
57};  
58/* encrypt a block of text  */  
59void encrypt(const u4byte in_blk[4], u4byte out_blk[4])  
60{   u4byte  a,b,c,d,t,u;  
61    a = in_blk[0]; b = in_blk[1] + l_key[0];  
62    c = in_blk[2]; d = in_blk[3] + l_key[1];  
63    f_rnd( 2,a,b,c,d); f_rnd( 4,b,c,d,a);  
64    f_rnd( 6,c,d,a,b); f_rnd( 8,d,a,b,c);  
65    f_rnd(10,a,b,c,d); f_rnd(12,b,c,d,a);  
66    f_rnd(14,c,d,a,b); f_rnd(16,d,a,b,c);  
67    f_rnd(18,a,b,c,d); f_rnd(20,b,c,d,a);  
68    f_rnd(22,c,d,a,b); f_rnd(24,d,a,b,c);  
69    f_rnd(26,a,b,c,d); f_rnd(28,b,c,d,a);  
70    f_rnd(30,c,d,a,b); f_rnd(32,d,a,b,c);  
71    f_rnd(34,a,b,c,d); f_rnd(36,b,c,d,a);  
72    f_rnd(38,c,d,a,b); f_rnd(40,d,a,b,c);  
73    out_blk[0] = a + l_key[42]; out_blk[1] = b;  
74    out_blk[2] = c + l_key[43]; out_blk[3] = d;  
75};  
76/* decrypt a block of text  */  
77void decrypt(const u4byte in_blk[4], u4byte out_blk[4])  
78{   u4byte  a,b,c,d,t,u;  
79    d = in_blk[3]; c = in_blk[2] - l_key[43];  
80    b = in_blk[1]; a = in_blk[0] - l_key[42];  
81    i_rnd(40,d,a,b,c); i_rnd(38,c,d,a,b);  
82    i_rnd(36,b,c,d,a); i_rnd(34,a,b,c,d);  
83    i_rnd(32,d,a,b,c); i_rnd(30,c,d,a,b);  
84    i_rnd(28,b,c,d,a); i_rnd(26,a,b,c,d);  
85    i_rnd(24,d,a,b,c); i_rnd(22,c,d,a,b);  
86    i_rnd(20,b,c,d,a); i_rnd(18,a,b,c,d);  
87    i_rnd(16,d,a,b,c); i_rnd(14,c,d,a,b);  
88    i_rnd(12,b,c,d,a); i_rnd(10,a,b,c,d);  
89    i_rnd( 8,d,a,b,c); i_rnd( 6,c,d,a,b);  
90    i_rnd( 4,b,c,d,a); i_rnd( 2,a,b,c,d);  
91    out_blk[3] = d - l_key[1]; out_blk[2] = c;  
92    out_blk[1] = b - l_key[0]; out_blk[0] = a;  
93};  
94int main()  
95{  
96return 0;  
97}  
98EOF  
99gcc -o rc6 rc6.c  
100if [ $? -eq 0 ]; then  
101./combine  
102else  
103echo &#x27;Compile ERROR&#x27;  
104fi  
105

46.Grep
#!/bin/bash
grep -qE %%1 %%2
47.直接创建多级目录
#!/bin/bash
mkdir -p %%1
48.批量重命名


1
2
3
4
1#!/bin/bash  
2find $PWD -type f -name &#x27;*\.cpp&#x27; |sed s/&#x27;\.cpp&#x27;//g|awk &#x27;{MV = &quot;mv&quot;};{C = &quot;\.c&quot;};{ CPP=&quot;\.cpp&quot;}; {print MV, $1 CPP , $1 C}&#x27;|sh  
3ls | awk -F &#x27;-&#x27; &#x27;{print &quot;mv &quot;$0&quot; &quot;$2}&#x27; \#去掉带&#x27;-&#x27;的前缀  
4

49.文本查找替换
#!/bin/bash
sed -e 's:%%2:%%3:g' %%1
#sed -e 's/%%2/%%3/g' %%1
50.文件关联
#!/bin/bash
51.批量转换编码从GB2312到Unicode


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1#!/bin/bash  
2scode=&quot;gbk&quot;  
3dcode=&quot;ucs2&quot;  
4for FILE in $(find $(pwd) -type f)  
5do  
6TMP_file=$(mktemp -p $(pwd))  
7if [ -f $FILE ]; then  
8Fright=$(stat -c %a $FILE)  
9Fuser=$(stat -c %U $FILE)  
10Fgrp=$(stat -c %G $FILE)  
11iconv -f $scode -t $dcode $FILE -o $TMP_file  
12mv $TMP_file $FILE  
13chmod $Fright $FILE  
14chown $Fuser.$Fgrp $FILE  
15fi  
16done  
17

52.设置JDK环境变量


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
1#!/bin/bash  
2find &quot;$PWD&quot; -type f \( -iname &#x27;*.bin&#x27; \) -print0 | xargs -0 chmod +x  
3find -type f \( -iname &#x27;*.bin&#x27; \) -print |  
4while read filename  
5do  
6    case &quot;$filename&quot; in  
7    *.bin)  
8        xterm -e &quot;$filename&quot; &amp;&amp; rm -if &quot;$filename&quot;  
9        ;;  
10    esac  
11done  
12OLDIFS=$IFS  
13IFS=$&#x27;\n&#x27;  
14for line in `cat ~/.bashrc`
15do  
16if [[ &quot;$line&quot; =~ .*export.* ]]; then  
17    if [[ &quot;$line&quot; =~ .*JAVA_HOME=.* ]]; then  
18      if [[ &quot;$line&quot; =~ =(\/([0-9a-zA-Z._]+))+ ]]; then  
19       javahome=$line  
20      fi  
21    fi  
22fi  
23if [[ &quot;$line&quot; =~ export\ PATH=\$PATH:\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin$ ]];then  
24    javapath=$line  
25fi  
26if [[ &quot;$line&quot; =~ export\ CLASSPATH=.:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib$ ]];then  
27    classpath=$line  
28fi  
29done  
30if [ ! -n &quot;$javahome&quot; ]; then  
31sed -i &#x27;$a export JAVA_HOME=&#x27;$(pwd)&#x27;/jdk1.6.0_25&#x27; ~/.bashrc  
32else  
33sed -i &#x27;s:&#x27;${javahome//\\/\\\\}&#x27;:export JAVA_HOME=&#x27;$(pwd)&#x27;/jdk1.6.0_32:g&#x27; ~/.bashrc  
34fi  
35if [ ! -n &quot;$javapath&quot; ]; then  
36sed -i &#x27;$a export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin&#x27; ~/.bashrc  
37fi  
38if [ ! -n &quot;$classpath&quot; ]; then  
39sed -i &#x27;$a export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib&#x27; ~/.bashrc  
40fi  
41IFS=$OLDIFS  
42\#!/bin/bash  
43shift  
44OLDIFS=$IFS  
45IFS=$&#x27;\n&#x27;  
46for line in `cat ~/TestBash.txt` \#~/.bashrc  
47do  
48  if [[ &quot;$line&quot; =~ .*export.* ]]; then  
49    if [[ &quot;$line&quot; =~ export\ CLASSPATH=.:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib$ ]]; then  
50      classpath=$line  
51    elif [[ &quot;$line&quot; =~ export\ PATH=\$PATH:\$CATALINA_HOME/bin$ ]]; then  
52      jbosspath=$line  
53fi  
54    if [[ &quot;$line&quot; =~ .*JAVA_HOME=.* ]]; then  
55      if [[ &quot;$line&quot; =~ =(\/([0-9a-zA-Z._]+))+ ]];then  
56       javahome=$line  
57      fi  
58    elif [[ &quot;$line&quot; =~ .*CATALINA_HOME=.* ]];then  
59      if [[ &quot;$line&quot; =~ =(\/([0-9a-zA-Z._]+))+ ]];then  
60       catalinahome=$line  
61      fi  
62    elif [[ &quot;$line&quot; =~ .*TOMCAT_HOME=.* ]];then  
63      if [[ &quot;$line&quot; =~ =(\/([0-9a-zA-Z._]+))+ ]];then  
64       tomcathome=$line  
65      fi  
66    elif [[ &quot;$line&quot; =~ .*CATALINA_BASE=.* ]];then  
67      if [[ &quot;$line&quot; =~ =(\/([0-9a-zA-Z._]+))+ ]];then  
68       catalinabase=$line  
69      fi  
70    elif [[ &quot;$line&quot; =~ .*JBOSS_HOME=.* ]];then  
71      if [[ &quot;$line&quot; =~ =(\/([0-9a-zA-Z._]+))+ ]];then  
72       jbosshome=$line  
73      fi  
74    fi  
75  elif [[ &quot;$line&quot; =~ ^PATH=\$PATH:\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin$ ]];then  
76    javapath=$line  
77  fi  
78  if [[ &quot;$line&quot; =~ export\ CLASSPATH=.:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib$ ]];then  
79    classpath=$line  
80  fi  
81  if [[ &quot;$line&quot; =~ export\ PATH=\$PATH:\$JBOSS_HOME/bin$ ]];then  
82    jbosspath=$line  
83  fi  
84done  
85if [ ! -n &quot;$javahome&quot; ]; then  
86sed -i &#x27;$a export JAVA_HOME=&#x27;$(pwd)&#x27;/jdk1.6.0_24&#x27; ~/TestBash.txt #~/.bashrc  
87else  
88sed -i &#x27;s:&#x27;${javahome//\\/\\\\}&#x27;:export JAVA_HOME=&#x27;$(pwd)&#x27;/jdk1.6.0_24:g&#x27; ~/TestBash.txt  
89fi  
90if [ ! -n &quot;$javapath&quot; ]; then  
91sed -i &#x27;$a PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin&#x27; ~/TestBash.txt #~/.bashrc  
92fi  
93if [ ! -n &quot;$classpath&quot; ]; then  
94sed -i &#x27;$a export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib&#x27; ~/TestBash.txt #~/.bashrc  
95fi  
96if [ ! -n &quot;$catalinahome&quot; ]; then  
97sed -i &#x27;$a export CATALINA_HOME=&#x27;$(pwd) ~/TestBash.txt #~/.bashrc  
98else  
99sed -i &#x27;s:&#x27;${catalinahome//\\/\\\\}&#x27;:export CATALINA_HOME=&#x27;$(pwd)&#x27;:g&#x27; ~/TestBash.txt  
100fi  
101if [ ! -n &quot;$tomcathome&quot; ]; then  
102sed -i &#x27;$a export TOMCAT_HOME=&#x27;$(pwd) ~/TestBash.txt #~/.bashrc  
103else  
104sed -i &#x27;s:&#x27;${tomcathome//\\/\\\\}&#x27;:export TOMCAT_HOME=&#x27;$(pwd)&#x27;:g&#x27; ~/TestBash.txt  
105fi  
106if [ ! -n &quot;$catalinabase&quot; ]; then  
107sed -i &#x27;$a export CATALINA_BASE=&#x27;$(pwd) ~/TestBash.txt #~/.bashrc  
108else  
109sed -i &#x27;s:&#x27;${catalinabase//\\/\\\\}&#x27;:export CATALINA_BASE=&#x27;$(pwd)&#x27;:g&#x27; ~/TestBash.txt  
110fi  
111if [ ! -n &quot;$jbosshome&quot; ]; then  
112sed -i &#x27;$a export JBOSS_HOME=&#x27;$(pwd) ~/TestBash.txt \#~/.bashrc  
113else  
114sed -i &#x27;s:&#x27;${jbosshome//\\/\\\\}&#x27;:export JBOSS_HOME=&#x27;$(pwd)&#x27;:g&#x27; ~/TestBash.txt  
115fi  
116if [ ! -n &quot;$jbosspath&quot; ]; then  
117sed -i &#x27;$a export PATH=$PATH:$CATALINA_HOME/bin&#x27; ~/TestBash.txt \#~/.bashrc  
118fi  
119IFS=$OLDIFS  
120

53.批量转换编码从Unicode到GB2312


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1#!/bin/bash  
2scode=&quot;ucs2&quot;  
3dcode=&quot;gbk&quot;  
4for FILE in $(find $(pwd) -type f)  
5do  
6TMP_file=$(mktemp -p $(pwd))  
7if [ -f $FILE ]; then  
8Fright=$(stat -c %a $FILE)  
9Fuser=$(stat -c %U $FILE)  
10Fgrp=$(stat -c %G $FILE)  
11iconv -f $scode -t $dcode $FILE -o $TMP_file  
12mv $TMP_file $FILE  
13chmod $Fright $FILE  
14chown $Fuser.$Fgrp $FILE  
15fi  
16done  
17

54.删除空文件夹
#!/bin/bash
rmdir -p %%1
55.GB2312文件转UTF-8格式
#!/bin/bash
iconv -f gbk -t utf8 %%1 -o %%2
56.UTF-8文件转GB2312格式
#!/bin/bash
iconv -f utf8 -t  gbk %%1 -o %%2
57.获取文件路径的父路径
#!/bin/bash
%%1=basename $PWD
58.Unicode文件转UTF-8格式
#!/bin/bash
iconv -f ucs2 -t  utf-8 %%1 -o %%2
59.CRC循环冗余校验


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
1#!/bin/bash  
2cat &lt;&lt;&#x27;EOF&#x27;&gt; crc.c  
3#include&lt;stdio.h&gt;  
4unsigned long int crc32_table[256];   
5unsigned long int ulPolynomial = 0x04c11db7;   
6unsigned long int Reflect(unsigned long int ref, char ch)   
7  {     unsigned long int value(0);   
8        // 交换bit0和bit7,bit1和bit6,类推   
9        for(int i = 1; i &lt; (ch + 1); i++)   
10         {            if(ref &amp; 1)   
11                      value |= 1 &lt;&lt; (ch - i);   
12                   ref &gt;&gt;= 1;      }   
13        return value;   
14}   
15init_crc32_table()   
16  {     unsigned long int crc,temp;   
17        // 256个值   
18        for(int i = 0; i &lt;= 0xFF; i++)   
19         {   temp=Reflect(i, 8);   
20               crc32_table[i]= temp&lt;&lt; 24;   
21                for (int j = 0; j &lt; 8; j++){   
22             unsigned long int t1,t2;   
23  unsigned long int flag=crc32_table[i]&amp;0x80000000;   
24                t1=(crc32_table[i] &lt;&lt; 1);   
25                if(flag==0)   
26                  t2=0;   
27                else   
28                  t2=ulPolynomial;   
29                crc32_table[i] =t1^t2 ;        }   
30               crc=crc32_table[i];   
31               crc32_table[i] = Reflect(crc32_table[i], 32);   
32        }  
33}  
34unsigned long GenerateCRC32(char xdata * DataBuf,unsigned long  len)   
35  {   
36        unsigned long oldcrc32;   
37        unsigned long crc32;   
38        unsigned long oldcrc;   
39        unsigned  int charcnt;   
40         char c,t;   
41        oldcrc32 = 0x00000000; //初值为0   
42     charcnt=0;   
43         while (len--) {   
44                 t= (oldcrc32 &gt;&gt; 24) &amp; 0xFF;   //要移出的字节的值   
45     oldcrc=crc_32_tab[t];         //根据移出的字节的值查表   
46                 c=DataBuf[charcnt];          //新移进来的字节值   
47                 oldcrc32= (oldcrc32 &lt;&lt; 8) | c;   //将新移进来的字节值添在寄存器末字节中   
48                 oldcrc32=oldcrc32^oldcrc;     //将寄存器与查出的值进行xor运算   
49                 charcnt++;   
50        }   
51         crc32=oldcrc32;   
52         return crc32;   
53} 
54

参数表可以先在PC机上算出来,也可在程序初始化时完成。下面是用于计算参数表的c语言子程序,在Visual C++ 6.0下编译通过。 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
1#include &lt;stdio.h&gt;   
2unsigned long int crc32_table[256];   
3unsigned long int ulPolynomial = 0x04c11db7;   
4unsigned long int Reflect(unsigned long int ref, char ch)   
5  {     unsigned long int value(0);   
6        // 交换bit0和bit7,bit1和bit6,类推   
7        for(int i = 1; i &lt; (ch + 1); i++)   
8         {            if(ref &amp; 1)   
9                      value |= 1 &lt;&lt; (ch - i);   
10                   ref &gt;&gt;= 1;      }   
11        return value;   
12}  
13int main()  
14{  
15     unsigned long int crc,temp;   
16        // 256个值   
17        for(int i = 0; i &lt;= 0xFF; i++)   
18         {  
19temp=Reflect(i, 8);  
20               crc32_table[i]= temp&lt;&lt; 24;   
21                for (int j = 0; j &lt; 8; j++){   
22             unsigned long int t1,t2;   
23  unsigned long int flag=crc32_table[i]&amp;0x80000000;  
24                t1=(crc32_table[i] &lt;&lt; 1);   
25                if(flag==0)   
26                  t2=0;   
27                else   
28                  t2=ulPolynomial;   
29                crc32_table[i] =t1^t2 ;         
30}   
31               crc=crc32_table[i];  
32               crc32_table[i] = Reflect(crc32_table[i], 32);  
33        }  
34return 0;  
35}  
36EOF  
37gcc -o crc crc.c  
38if [ $? -eq 0 ]; then  
39./combine  
40else  
41echo &#x27;Compile ERROR&#x27;  
42fi  
43

60.判断是否为空文件
#!/bin/bash
61.终止程序
#!/bin/sh
kill -KILL pidof %%1 -s
#killall %%1
62.定时关机
#!/bin/sh
shutdown -h %%1 & #23:00
#shutdown -h now
#halt
#/sbin/poweroff
#init 0
63.显示进程列表
#!/bin/sh
ps aux
#fuser -l
64.遍历文件夹列出文件大小
#!/bin/sh
du -sH "%%1/*"
65.GOST算法
#!/bin/bash
66.对目标压缩文件解压缩到指定文件夹
#!/bin/bash
67.保存文件时重名自动生成新文件
#!/bin/bash
68.打开网页
#!/bin/sh
lynx %%1
69.删除空文件夹整合操作
#!/bin/bash
70.获取磁盘所有分区
#!/bin/sh
df -k
71.激活一个程序或程序关联的文件
#!/bin/bash
72.MP3播放
#!/bin/sh
amp "%%1"
73.WAV播放
#!/bin/sh
amp "%%1"
74.写图像到剪切板
#!/bin/bash
75.从剪贴板复制图像到窗体
#!/bin/bash
76.删除文件夹下的所有文件且不删除文件夹下的文件夹
#!/bin/sh
rm -if "%%1/*"
77.XML遍历结点属性值
#!/bin/bash
78.Unicode文件转GB2312格式
#!/bin/sh
iconv -f ucs2 -t  gbk %%1 -o %%2
79.开源程序库Xercesc-C++代码工程中内联80.提取包含头文件列表
#!/bin/bash
81.GB2312文件转Unicode格式
#!/bin/sh
iconv -f gbk -t  ucs2 %%1 -o %%2
82.Java程序打包
#!/bin/bash
83.UTF-8文件转Unicode格式
#!/bin/bash
iconv -f utf8 -t  ucs2 %%1 -o %%2
84.创建PDF文档
#!/bin/bash
85.创建Word文档
#!/bin/bash
86.快速高效的文件加密
#!/bin/bash
87.从CSV文件构造XML文档
#!/bin/bash
88.从XML文档生成CSV文件
#!/bin/bash
89.模拟键盘输入字符串
#!/bin/bash
90.提取PDF文件中的文本
#!/bin/bash
91.操作内存映射文件
#!/bin/bash
91.1发送内存映射数据
#!/bin/bash
91.2接收内存映射数据
#!/bin/bash
92.重定向windows控制台程序的输出信息
#!/bin/bash
93.基数转序数
#!/bin/bash
94.数字月份转英文
#!/bin/bash
95.报表相关
#!/bin/bash
96.根据进程名获取进程ID
#!/bin/bash
pidof %%1 -s
96.BCP导入
#!/bin/bash
97.BCP导出
#!/bin/bash
98.计算文件MD5值
#!/bin/bash
md5sum "%%1"
99.计算获取文件夹中文件的MD5值
#!/bin/bash
100.复制一个目录下所有文件到一个文件夹中
#!/bin/bash
cp $(find "%%1" -name *.*) "%%2"
101.移动一个目录下所有文件到一个文件夹中
#!/bin/bash
mv $(find "%%1" -name *.*) "%%2"
102.文件RSA高级加密
十进制到十六进制
typeset -i16 BASE_16_NUM
BASE_16_NUM=%%1
echo $BASE_16_NUM
八进制到十六进制
#!/bin/bash
typeset -i16 BASE_16_NUM
BASE_16_NUM=8#%%1
echo $BASE_16_NUM
十进制到八进制
#!/bin/bash
printf %o %%1; echo
十进制到十六进制
#!/bin/bash
printf %x %%1; echo
103.计算文件大小
#!/bin/bash
wc "%%1"
104.计算文件夹的大小


1
2
3
4
5
6
1#!/sbin/ksh  
2dir=%%1  
3(cd $dir;pwd)  
4find $dir -type d -print | du | awk &#x27;{print $2, &quot;== (&quot;$1/2&quot;kb)&quot;}&#x27; |sort -f |  
5sed -e &quot;s,[^ /]*/([^ /]*) ==,|--1,&quot; -e&quot;s,[^ /]*/,| ,g&quot;
6

105.快速获得当前程序的驱动器、路径、文件名和扩展名
106.磁盘剩余空间计算
#!/bin/bash
df -k
107.获取当前程序进程ID
#!/bin/bash
pidof %%1 -s
108.全盘搜索文件
#!/bin/bash
#updatedb
#locate %%1
slocate %%1
109.获得当前登录的用户名
#!/bin/bash
whoami
110.获得所有用户名
#!/bin/bash
who
111.创建MySQL管理用户
#!/bin/bash
mysqladmin -u root password %%1
112.管理MySQL数据库服务器
#!/bin/bash
112.1.启动MySQL数据库服务器
mysqld -console
112.2.登录MySQL数据库服务器
112.2.1.登录本地MySQL数据库服务器
mysql -uroot -p%%1
112.2.2.登录远程MySQL数据库服务器
mysql -h %%1 -u %%2 -p%%3
112.3.关闭MySQL数据库服务器
mysqladmin -u root shutdown
#pkill -9 mysql
112.4.测试MySQL数据库服务器
mysqlshow || mysqlshow -u root mysql || mysqladmin version status || mysql test
113.MySQL执行查询


1
2
3
4
1#!/bin/sh  
2mysqladmin -u %%1 -p%%2 SELECT * INTO OUTFILE &#x27;./bestlovesky.xls&#x27; FROM bestlovesky WHERE 1 ORDER BY id DESC  LIMIT 0, 50;  
3mysql -u %%1 -p%%2 -e &quot;SELECT * INTO OUTFILE &#x27;./bestlovesky.xls&#x27; FROM bestlovesky WHERE 1 ORDER BY id DESC  LIMIT 0, 50;&quot;  
4

114.创建Oracle管理用户
#!/bin/sh
114.1.创建新用户
create user test identified by test default tablespace ts_test temporary
tablespace temp;
114.2.给用户角色特权
grant connect,resource to test;
115.登录Oracle数据库


1
2
3
4
5
6
7
1#!/bin/bash  
2sqlplusw  
3sqlplus /nolog  
4conn username/password@Oranet  
5conn system/systempwd@whfc  
6conn sys/syspwd@whfc as sysdba  
7

115.创建Oracle表空间
#!/bin/bash
conn system@whfc01
create tablespace ts_test datafile '/data2/oradata/ciis/ts_test01.dbf' size
116.添加Oracle数据文件
#!/bin/bash
alter tablespace ts_test add datafile '/data2/oradata/ciis/ts_test02.dbf' size
117.查看Oracle表空间大小
#!/bin/bash
desc DBA_DATA_FILES
118.查看Oracle剩余表空间大小
#!/bin/bash
desc DBA_FREE_SPACE
119.查看Oracle当前用户表名
#!/bin/bash
select * from tab;
120.Oracle创建索引
#!/bin/bash
CREATE INDEX idx_book_bookid ON book(bookname);
121.Oracle创建主键约束
#!/bin/bash
ALTER TABLE book ADD CONSTRAINT pk_book_bookid PRIMARY KEY (bookid);
122.Oracle显示表结构
#!/bin/bash
desc book
123.Oracle查看表的索引
#!/bin/bash
column index_name format a30
select table_name, index_name from user_indexes;
124.Oracle查看索引列
#!/bin/bash
select table_name, index_name, column_name, column_position from user_ind_columns;
125.Oracle查看数据段占空间大小
#!/bin/bash
desc user_segments
126.Oracle查看表占空间大小
#!/bin/bash
select segment_name,segment_type,bytes from user_segments where segment_type='TABLE';
127.安全删除USB
#!/bin/bash
rundll32.exe shell32.dll,Control_RunDLL hotplug.dll
128.打开SQL Server Management Studio
#!/bin/bash
sqlwb %%1.sql
129.MySQL数据库导出备份


1
2
3
4
5
1#!/bin/bash  
2mysqldump -u %%1 -p %%2 %%3&gt;%%4.sql  
3mysqldump --opt test &gt; mysql.test //将数据库test导出到mysql.test文件,后面是一个文本文件  
4mysqldump -u root -p123456 --databases dbname &gt; mysql.dbname //就是把数据库dbname导出到文件mysql.dbname中。  
5

130.MySQL数据库数据导入
mysql -u %%1 -p %%2 %%3<%%4.sql
mysqlimport -u root -p123456 < mysql.dbname
将文本数据导入数据库:
文本数据的字段之间用tab键隔开
use test
load data local infile "文件名" into table 表名;
eg: load data local infile "D:/mysql.txt" into table mytable;
导入.sql 文件命令
use database
source d:/mysql.sql;
131.MySQL数据库检查
mysqlcheck -o %%3 -u %%1 -p %%2
132.MySQL数据表文件修复
myisamchk -B -o %%1.myd
1,查看数据库状态 及启动停止
/etc/init.d/mysqld status
/etc/init.d/mysqld start
/etc/init.d/mysqld stop
2,给用户配置初始密码123456:
mysqladmin -u root -password 123456
3,修改root用户密码为 abc123
mysqladmin -u root -p123456 password abc123
4,如果想去掉密码:
mysqladmin -u root -pabc123 password ""
5,root连接数据库有密码和无密码:
mysql -u root(-uroot) -p
mysql
6,增加用户 test1 密码 abc,让它可以在任何主机上登录,并对所有数据库有查询,插入,修改,删除的权限:
格式: grant select on 数据库.* to 用户名@登录主机 identified by "密码"
grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";
8,增加一个用户test2,让它只可以在localhost上登录,并可以对数据库mydb进行查询,插入,修改,删除的操作,
这样用户即使使用知道test2的密码,他也无法从internet 上直接访问数据库,只能通过mysql主机上的web页面来访问。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";
grant select,insert,update,delete on mydb.* to test2@localhost identified by ""; 设置无密码
9,显示数据库列表:
show databases;
use mysql 打开库
show tables;
10,表的操作
describle 表名; 显示数据表的结构
create database 库名;
drop database 库名;
create table 表名(字段设定列表)
drop table 表名;
delete from 表名;清空表记录
select * from 表名; 显示表中的记录
insert into 表名 values(, ,)
alter table 表名 add column <字段名><字段选项>
133.检查端口占用
#!/bin/bash
netstat -ano
134.Linux下检查Apache是否安装
#!/bin/bash
rpm -qa | grep httpd
135.Linux下启动Apache服务
#!/bin/bash
service httpd start
136.Linux下停止Apache服务
#!/bin/bash
service httpd stop
137.Linux下重新启动Apache服务
#!/bin/bash
service httpd restart
138.Linux下自动加载Apache 服务
#!/bin/bash
chkconfig – level 3 httpd on
139.Linux下不自动加载Apache 服务
#!/bin/bash
chkconfig – level 3 httpd off
140.Linux下检查VSFTP是否安装
#!/bin/bash
rpm -qa | grep vsftpd
141.Linux下启动VSFTP服务
#!/bin/bash
service vsftpd start
142.Linux下停止VSFTP服务
#!/bin/bash
service vsftpd stop
143.Linux下重新启动VSFTP服务
#!/bin/bash
service vsftpd restart
144.Linux下检查VSFTP是否被启动
#!/bin/bash
pstree | grep vsftpd
145.Linux下检查Sendmail是否安装
#!/bin/bash
rpm -qa | grep sendmail
146.Linux下启动Sendmail服务
#!/bin/bash
service sendmail start
147.Linux下停止Sendmail服务
#!/bin/bash
service sendma stop
148.Linux下重新启动Sendmail服务
#!/bin/bash
service sendmail restart
149.Linux下自动加载Sendmail 服务
#!/bin/bash
chkconfig – level 3 sendmail on
150.Linux下不自动加载Sendmail 服务
#!/bin/bash
chkconfig – level 3 sendmail off
151.Linux下文本图形界面配置启动服务
#!/bin/bash
ntsysv
152.以数组的方式删除文件夹
153.GCC批量编译


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1#!/bin/bash  
2find -type f \( -iname &#x27;*.c&#x27; -o -iname &#x27;*.cpp&#x27; \) -print |  
3while read filename  
4do  
5    case &quot;$filename&quot; in  
6    *.c)  
7      gcc &quot;$filename&quot; -o &quot;$(dirname &quot;$filename&quot;)&quot;/&quot;$(basename &quot;$filename&quot; .c)&quot;  
8        ;;  
9    *.cpp)  
10        gcc &quot;$filename&quot; -o &quot;$(dirname &quot;$filename&quot;)&quot;/&quot;$(basename &quot;$filename&quot; .cpp)&quot;  
11        ;;  
12    esac  
13done  
14

154.批量赋予可执行权限


1
2
3
1#!/bin/bash  
2find &quot;$PWD&quot; -type f \( -iname &#x27;*.sh&#x27; -o  -iname &#x27;*.csh&#x27; -o  -iname &#x27;*.ksh&#x27; -o -iname &#x27;*.pl&#x27; -o -iname &#x27;\*.bin&#x27; -o -iname &#x27;\*.run&#x27; -o -iname &#x27;*.bundle&#x27; -o -iname &#x27;*.rb&#x27; -o -iname &#x27;*.py&#x27; \) -print0 | xargs -0 chmod +x  
3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1#!/bin/bash  
2for file in *.sh *.pl *.bin *.run *.bundle *.rb *.py  
3do  
4if [[ ! &quot;$file&quot; =~ \*.[A-Za-z]+ ]]; then  
5chmod +x &quot;$(file)&quot;  
6fi  
7done  
8OLDIFS=$IFS  
9IFS=:  
10for path in $( find $(pwd) -type d -printf &quot;%p$IFS&quot;)  
11do  
12for file in $path/*.sh $path/*.pl $path/*.bin $path/*.run $path/*.bundle $path/*.rb $path/*.py  
13do  
14if [[ ! &quot;$file&quot; =~ \*.[A-Za-z]+ ]]; then  
15chmod +x &quot;$(path)/$(file)&quot;  
16fi  
17done  
18done  
19IFS=$OLDIFS  
20

155.批量执行


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1#!/bin/bash  
2find -type f \( -iname &#x27;*.sh&#x27; -o  -iname &#x27;*.csh&#x27; -o  -iname &#x27;*.ksh&#x27; -o -iname &#x27;*.pl&#x27; -o -iname &#x27;*.bin&#x27; -o -iname &#x27;*.run&#x27; -o -iname &#x27;*.bundle&#x27; -o -iname &#x27;*.bin&#x27; -o -iname &#x27;*.class&#x27; -o -iname &#x27;*.rpm&#x27; -o -iname &#x27;*.rb&#x27; -o -iname &#x27;*.py&#x27; -o -iname &#x27;*.jar&#x27; \) -print |  
3while read filename  
4do  
5    case &quot;$filename&quot; in  
6    *.sh | *.csh | *.ksh)  
7if [ ! &quot;./&quot;&quot;$(basename $filename)&quot; = $0 ]; then  
8        xterm -e &quot;$filename&quot;  
9fi  
10        ;;  
11    *.pl)  
12        xterm -e perl &quot;$filename&quot;  
13        ;;  
14    *.bin | *.run | *.bundle)  
15        xterm -e &quot;$filename&quot;  
16        ;;  
17    *.class)  
18        xterm -e java &quot;$(dirname &quot;$filename&quot;)&quot;/&quot;$(basename &quot;$filename&quot; .class)&quot;  
19        ;;  
20    *.rpm)  
21        xterm -e rpm -ivh &quot;$filename&quot;  
22        ;;  
23    *.rb)  
24        xterm -e ruby &quot;$filename&quot;  
25        ;;  
26    *.py)  
27        xterm -e python &quot;$filename&quot;  
28        ;;  
29    *.jar)  
30        xterm -e java -jar &quot;$filename&quot;  
31        ;;  
32    esac  
33done
34

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1#!/bin/bash  
2find -maxdepth 1 -type f \( -iname &#x27;*.sh&#x27; -o -iname &#x27;*.pl&#x27; -o -iname &#x27;*.bin&#x27; -o -iname &#x27;*.run&#x27; -o -iname &#x27;*.bundle&#x27; -o -iname &#x27;*.bin&#x27; -o -iname &#x27;*.class&#x27; -o -iname &#x27;*.rpm&#x27; -o -iname &#x27;*.rb&#x27; -o -iname &#x27;*.py&#x27; -o -iname &#x27;*.jar&#x27; \) -print  
3while read file  
4do  
5    case &quot;${file##*.}&quot; in  
6        sh ) xterm -e &quot;&quot;&quot;&quot;$file&quot;&quot;&quot;&quot;;;  
7        pl ) xterm -e perl &quot;&quot;&quot;&quot;$file&quot;&quot;&quot;&quot;;;  
8        bin ) xterm -e &quot;&quot;&quot;&quot;$file&quot;&quot;&quot;&quot;;;  
9        run ) xterm -e &quot;&quot;&quot;&quot;$file&quot;&quot;&quot;&quot;;;  
10        bundle ) xterm -e &quot;&quot;&quot;&quot;$file&quot;&quot;&quot;&quot;;;  
11        class ) xterm -e java &quot;&quot;&quot;&quot;${file%.*}&quot;&quot;&quot;&quot;;;  
12        rpm ) xterm -e rpm -ivh &quot;&quot;&quot;&quot;$file&quot;&quot;&quot;&quot;;;  
13        rb ) xterm -e ruby &quot;&quot;&quot;&quot;$file&quot;&quot;&quot;&quot;;;  
14        py ) xterm -e python &quot;&quot;&quot;&quot;$file&quot;&quot;&quot;&quot;;;  
15        jar ) xterm -e java -jar &quot;&quot;&quot;&quot;$file&quot;&quot;&quot;&quot;;;  
16    esac  
17done  
18

156.获取操作系统版本
#!/bin/bash
uname -r
#uname -a
157.自身复制
#!/bin/bash
cp $0 "%%1"
158.GCC批量创建静态库


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1#!/bin/bash  
2find -type f \( -iname &#x27;*.c&#x27; -o -iname &#x27;*.cpp&#x27; \) -print |  
3while read filename  
4do  
5    case &quot;$filename&quot; in  
6    *.c)  
7      g++  -c -o &quot;$(dirname &quot;$filename&quot;)&quot;/&quot;$(basename &quot;$filename&quot; .c)&quot;.o&quot;&quot; &quot;$filename&quot;  
8        ;;  
9    *.cpp)  
10      g++  -c -o &quot;$(dirname &quot;$filename&quot;)&quot;/&quot;$(basename &quot;$filename&quot; .cpp)&quot;.o&quot;&quot; &quot;$filename&quot;  
11        ;;  
12    esac  
13done  
14OLDIFS=$IFS  
15IFS=:  
16for path in $( find $(pwd) -type d -printf &quot;%p$IFS&quot;)  
17do  
18ar ru $path&quot;.a&quot; $path&quot;/*.o&quot; &amp;&amp; ranlib $path&quot;.a&quot;  
19done  
20IFS=$OLDIFS  
21find &quot;$PWD&quot; -type f \( -iname &#x27;*.o&#x27; \) -print0 | xargs -0 rm -if  
22

159.Java批量打包EJB


1
2
3
4
5
6
7
8
9
10
11
1#!/bin/bash  
2find &quot;$PWD&quot; -type f \( -iname &#x27;*.java&#x27; \) -print0 | xargs -0 javac  
3OLDIFS=$IFS  
4IFS=:  
5for path in $( find $(pwd) -type d -printf &quot;%p$IFS&quot;)  
6do  
7jar -cvf &quot;$(path&quot;.jar&quot;)&quot; &quot;$(path&quot;/*.*&quot;)&quot; &amp;&amp; cp &quot;$(path&quot;.jar&quot;)&quot; &quot;$(JBOSS_HOME&quot;/server/default/deploy&quot;)&quot;  
8done  
9IFS=$OLDIFS  
10find &quot;$PWD&quot; -type f \( -iname &#x27;*.class&#x27; \) -print0 | xargs -0 rm -if  
11

160.获取环境变量
161.dd
#!/bin/bash
dd
162.显示只有小写字母的文件
#!/bin/bash
ls -1|awk '/^[[:lower:]].*/'
163.Zip压缩目录中的所有文件


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1#!/bin/bash  
2direc=&quot;%%1&quot; #$(pwd)  
3targetpath=&quot;%%2&quot;  
4OLDIFS=$IFS  
5IFS=:  
6for path in $( find $direc -type d -printf &quot;%p$IFS&quot;)  
7do  
8mkdir -p &quot;$targetpath/${path:${#direc}+1}&quot;  
9for file in $path/*  
10do  
11if [ -f $file ]; then  
12zip -j &quot;$targetpath/${path:${#direc}+1}/${file:${#path}+1}.zip&quot; &quot;$file&quot;  
13fi  
14done  
15done  
16IFS=$OLDIFS  
17

164.Zip解压缩目录中的所有文件


1
2
3
4
5
6
7
8
9
10
11
12
1#!/bin/bash  
2direc=&quot;%%1&quot; #$(pwd)  
3targetpath=&quot;%%2&quot;  
4OLDIFS=$IFS  
5IFS=:  
6for path in $( find $direc -type d -printf &quot;%p$IFS&quot;)  
7do  
8mkdir -p &quot;$targetpath/${path:${#direc}+1}&quot;  
9unzip -x &quot;$path/*.zip&quot; -d &quot;$targetpath/${path:${#direc}+1}&quot;  
10done  
11IFS=$OLDIFS  
12

165.分布式复制文件夹


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1#!/bin/bash  
2direc=&quot;%%1&quot; #$(pwd)  
3targetpath=&quot;%%2&quot;  
4OLDIFS=$IFS  
5IFS=:  
6for path in $( find $direc -type d -printf &quot;%p$IFS&quot;)  
7do  
8mkdir -p &quot;$targetpath/${path:${#direc}+1}&quot;  
9rm -if &quot;$targetpath/${path:${#direc}+1}/*.tmp&quot;  
10for file in $path/*  
11do  
12if [ -f $file ]; then  
13cp &quot;$file&quot; &quot;$targetpath/${path:${#direc}+1}/${file:${#path}+1}.tmp&quot;  
14mv &quot;$targetpath/${path:${#direc}+1}/${file:${#path}+1}.tmp&quot; &quot;$targetpath/${path:${#direc}+1}/${file:${#path}+1}&quot;  
15fi  
16done  
17done  
18IFS=$OLDIFS
19

166.注册反注册组件
#!/bin/bash
regsvr32 "%%1"
167.LZMA
#!/bin/bash
168.CAB压缩文件
#!/bin/bash
169.CAB解压缩文件
#!/bin/bash
170.锁定屏幕
#!/bin/sh
RUNDLL32.exe USER32,LockWorkStation
171.以其它用户的身份运行程序
#!/bin/bash
172.添加系统用户
#!/bin/sh
useradd "%%1"
173.删除系统用户
#!/bin/sh
userdel "%%1"
174.添加用户组
#!/bin/sh
groupadd -g 2000 "%%1"
175.删除用户组
#!/bin/sh
groupdel "%%1"
176.赋予管理员权限
#!/bin/bash
177.收回管理员权限
#!/bin/bash
178.遍历目录产生删除文件的脚本
#!/bin/bash
179.LZW压缩文件
#!/bin/bash
z
180.LZW解压缩文件
#!/bin/bash
z
181.递归赋予目录权限


1
2
3
4
5
6
7
8
9
10
1#!/bin/bash  
2direc=&quot;%%1&quot; #$(pwd)  
3OLDIFS=$IFS  
4IFS=:  
5for path in $( find $direc -type d -printf &quot;%p$IFS&quot;)  
6do  
7chown -R root.root &quot;$path&quot;  
8done  
9IFS=$OLDIFS  
10

182.卸载RPM包
#!/bin/sh
rpm -e  "%%1"
183.删除源文件中的注释
#!/bin/sh
184.设置目录下所有文件属性为可写
#!/bin/sh
185.统计目录下所有文件的总共行数
#!/bin/sh
cat * |wc
ls *|xargs wc -l
find ./ -name "*c" | xargs wc -l
186.删除自身
#!/bin/rm
exit 65
#rm $0
187.打开终端
#!/bin/bash -l
188.弹出光驱
#!/bin/sh
eject
189.收回光驱
#!/bin/sh
eject -t
190.磁盘总空间计算
191.解析CSV文件
192.按行保存文件为数组
193.MySQL执行SQL文件
mysqladmin -u %%1 -p%%2 < %%3.sql
mysql -u %%1 -p%%2 -e "SOURCE %%3.sql"

给TA打赏
共{{data.count}}人
人已打赏
安全运维

故障复盘的简洁框架-黄金三问

2021-9-30 19:18:23

安全运维

OpenSSH-8.7p1离线升级修复安全漏洞

2021-10-23 10:13:25

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索