Shell脚本样例

Shell脚本样例:

  • 文件夹排序:对 当前目录/当前目录+子目录 按照文件名排序
  • 非递归阶乘:非递归的阶乘问题(考虑到$?返回值无法返回大数字)
  • 文件解压:支持zip、tar、tar.gz、tar.bz2压缩文件类型的解压
  • 获得某个目录下前N个最大的文件:获得指定或默认目录下的前N个文件或者所有文件

一、文件夹排序脚本

1.1对当前目录

对当前目录所有的文件夹名称及其文件名称进行排序的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#/bin/bash
#对文件路径的本目录的所有文件的文件名进行排序

path=$1
files=$(ls $path)

for filename in $files #循环遍历当前文件夹内的所有文件名及其文件夹名
do
   echo $filename>>sort.txt #将输出的文件名重定向到test.txt
done

echo "排序之后:"
sort test.txt #对文件排序并输出
rm test.txt #删除文件

1.2对当前目录及其子目录

对当前目录及其子目录所有的文件夹名称及其文件名称进行排序的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
#对文件路径本目录及其所有子目录文件名排序

function ergodic(){
   for filename in $(ls $1) #循环遍历当前文件夹内的所有文件名及其文件夹名
   do
       if [ -d $1"/"$filename ] #判断是否存在子目录
       then
          ergodic $1"/"$filename #对函数ergodic进行递归调用
       else
           echo $filename>>test.txt #将输出的文件名重定向到test.txt
       fi
   done
}
ergodic $1 #函数开始执行的位置,$1为输入的第一个参数
echo "排序之后:"
sort test.txt #对文件排序并输出
rm test.txt #删除文件

二、非递归阶乘

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
#!/bin/bash
# 计算阶乘
# 由于$?无法返回大数字,使用全局变量存储返回的值

let result=0
let localreturn=0

funFactorial(){
  let num1=1
   if [ $1 -gt $num1 ]; then
      let numSub=$1-1
      funFactorial $numSub
      let result=$localreturn*$1
      let localreturn=$result
   else
      let result=1
      let localreturn=1;
   fi
}
if [ -n "$1" ]; then
  funFactorial $1
   echo result:$result
else
   echo "usage: factorial.sh [n]"
   echo "calculates a number's factorial"
fi

三、文件解压

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
#!/bin/bash

# usage: self_compression.sh [--list] or [source compressd file] [destination path]
# self compression accroding to file name suffix

# 不存在参数
if [ "" = "$1" ]; then
   echo "usage: self_compression.sh [--list] or [source compressd file] [destination path]"
   echo "self compression accroding to file name suffix"
else
   # 有参数 --list
   if [ "--list" = "$1" ]; then
       echo "Support file types:zip tar tar.gz tar.bz2"
   # 选择文件类型&解压缩文件
   else
       # get type
       file=$1
       type=${file##*.}
       # Choose type & unzip
       if [ "$type" = "zip" ]; then
          unzip $1 -d $2
       elif [ "$type" = "tar" ]; then
          tar -xf $1 -C $2
       elif [ "$type" = "gz" ]; then
          tar -xzvf $1 -C $2
       elif [ "$type" = "bz2" ]; then
          tar -xjvf $1 -C $2
       else
           echo "$type Not Suport!!"
       fi
   fi
fi

四、获得某个目录下前N个最大的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
# echo "usage:file_size_get.sh [-n N] [-d DIR]"
# echo "show top N largest files/directories"
# 获得指定或默认目录下的前N个文件或者所有文件

# 存在参数
if [ -n "$1" -o -n "$3" ];then
   # 有两个参数
   if [ -n "$1" -a -n "$3" ];then
      du -ak $4 | sort -nr | head -$2 > test.txt
   # 只有参数-n
   elif [ "-n" = "$1" ];then
      du -ak | sort -nr | head -$2 > test.txt
   # 只有参数-d
   else
      du -ak $2 | sort -nr > test.txt
   fi
# 不存在参数
else
  du -ah | sort -nr > test.txt
fi
cat -n test.txt
rm test.txt