本例是一个遍历加密寒武纪模型的代码,主要注意几个点:
1、递归 参考:https://www.jb51.net/article/142325.htm
2、字符串相等
3、字符串取段
#加密root目录下的所有寒武纪模型,秘钥=1234567890
bash encode.sh root 1234567890
#! /bin/bash
function read_dir(){
for file in `ls $1` #注意此处这是两个反引号,表示运行系统命令
do
if [ -d $1"/"$file ] #注意此处之间一定要加上空格,否则会报错
then
read_dir $1"/"$file $2
else
cam=".cambricon"
now=${file:0-10:10}
#echo $now
if [ "$now"x = "$cam"x ]
then
echo "find cambricon file: "$1"/"$file #在此处处理文件即可
./encode $1"/"$file $2
passkeylen=${#s2}
newfile=${file:0:${#file}}"-encode"
tempfilename=$file"-"$2
echo "tempfilename= "$tempfilename
#sleep 5
mv $1"/"$tempfilename $1"/"$newfile
fi
fi
done
}
#读取第一个参数
read_dir $1 $2