shell脚本内循环执行时报出let:not found错误

shell脚本内循环执行时报出let:not found错误

//20220322
写在前面:今天编写脚本统计文件夹下文件数和文件夹数时,执行过程中报出let:not found错误,后搜集资料解决,遂在此记录一下

问题所在:

  • Ubuntu默认使用的shell解析是dash,dash只有bash一部分的功能

解决方法:

  1. 安装bash:这一条我没有尝试,因为不是管理员,网上有很多教程,可以试试
  2. 使用bash代替sh执行脚本文件

另附上循环遍历目录下所有文件并计数脚本代码_基本逻辑是:根据传入的父文件夹名进行判断,然后判断目录下文件属性,如果是文件夹则自动拼接进入下一次循环

#!/bin/bash

let "filenum=0"
let "dirnum=0"

fun_directory(){
        # let "filenum=0"
        # let "dirnum=0"
        for i in `ls -a $1`
        do
                # echo $i
                if [ -d $1"/"$i ]
                then
                        let dirnum+=1
                        if [[ $i != '.' && $i != '..' ]]
                        then
                                fun_directory $1"/"$i
                        fi
                else
                        let filenum+=1
                fi
        done

        echo "The num of this directory is $dirnum"
        echo "the num of files is $filenum"
}

echo "The num of this directory is $dirnum"
echo "the num of files is $filenum"

# 调用函数
fun_directory wikiart

希望对后来者有所帮助
以上
/抱拳

posted @ 2022-03-22 10:09  醉生梦死_0423  阅读(278)  评论(0编辑  收藏  举报