Shell 遍历文件夹

例1

 1 #! /bin/bash
 2 read_dir(){
 3   a=0
 4   declare -a arr
 5   for file in `ls $1`
 6   do
 7     a=$a+1
 8     if [ -d $1"/"$file ]
 9     then
10       arr[$a]=$1"/"$file
11       echo $1"/"$file
12     fi
13   done
14   return $((arr))
15 }
16 
17 result=($(read_dir $1))
18 # echo "aaaa:${result[*]}"
19 echo ${#result[@]}
20 for element in ${result[@]}
21 do
22 #  echo "ffmpeg -f concat -i -safe 0 $element/files.txt -c copy $element.ts"
23   # 删除包含df00的行
24   echo "sed -i '/df00/d' $element/files.txt"
25   a='y'
26   if [[ $2 = $a ]]
27   then
28     sed -i '/df00/d' $element/files.txt
29   fi
30   # 删除首行
31   # echo "sed -i '1d' $element/files.txt"
32   # 删除1到10行
33   # echo "sed -i '1,10d' $element/files.txt"
34   echo "ffmpeg -f concat -i $element/files.txt -c copy $element.ts"
35   if [[ $2 = $a ]]
36   then
37     ffmpeg -f concat -i $element/files.txt -c copy $element.ts
38   fi
39   echo "rm -rf $element"
40   if [[ $2 = $a ]]
41   then
42     rm -rf $element
43   fi
44   echo ""
45   echo ""
46 
47 done

例2

 1 read_dir(){
 2   a=0
 3   declare -a arr
 4   for file in `ls $1`
 5   do
 6     a=$a+1
 7     if [ -f $1"/"$file ]
 8     then
 9       arr[$a]=$1"/"$file
10       echo $1"/"$file
11     fi
12   done
13   return $((arr))
14 }
15 
16 result=($(read_dir $1))
17 # echo "aaaa:${result[*]}"
18 if [[ $1 = '.' ]]
19 then
20     echo ${#result[@]}
21     for element in ${result[@]}
22     do
23         filename=${element%.*}
24         suffix=${element##*.}
25         
26         if [[ $suffix != 'ts' ]]
27         then
28             # echo '1'
29             continue
30         fi
31         echo $filename  $suffix
32         if [[ $2 = 'y' ]]
33         then
34             ffmpeg -i $element -c copy $filename.mp4
35             rm -rf $element
36         else
37             echo "ffmpeg -i $element -c copy $filename.mp4"
38             echo "rm -rf $element"
39         fi
40     
41     done
42 fi

 

 
posted @ 2023-08-27 19:59  看一百次夜空里的深蓝  阅读(528)  评论(0编辑  收藏  举报