Performance bash-提高此代码性能的最佳方法是什么

Performance bash-提高此代码性能的最佳方法是什么,performance,bash,refactoring,Performance,Bash,Refactoring,我现在已经让我的脚本工作了,我使用了cdwn,只要当前工作目录中没有文件,只有一个目录,它就是cd。否则它将打破一个无限循环。我为这个脚本创建了几个通用函数,但将用于其他脚本,我使用了几个find命令和一个egrep。请就如何提高性能提出建议,这主要是一个复习和学习bash内容的练习,但我想利用它 以下函数用于检查wd中是否存在任何文件 function checkForFilesInCurrentDir(){ # check if there are files in the curr

我现在已经让我的脚本工作了,我使用了cdwn,只要当前工作目录中没有文件,只有一个目录,它就是cd。否则它将打破一个无限循环。我为这个脚本创建了几个通用函数,但将用于其他脚本,我使用了几个find命令和一个egrep。请就如何提高性能提出建议,这主要是一个复习和学习bash内容的练习,但我想利用它

以下函数用于检查wd中是否存在任何文件

function checkForFilesInCurrentDir(){
    # check if there are files in the current directory
    doFilesExist=`find . -maxdepth 1 -type f`
    if [ -z "$doFilesExist" ]; then
    #   echo "no files"
        return 0 
    else
    #   echo "some files"
        return 1
    fi
}
function numOfDirsInWD(){
    # check number of dirs in working directory and return that num
    checkForDirs=`find . -mindepth 1 -maxdepth 1 -type d`
    numOfDirs=$(echo "$checkForDirs" | egrep -c "./")
    return $numOfDirs
}
下面的函数检查dir,然后在单独的行上打印字符串,然后使用egrep计算出现该模式的行数,有效地计算wd中的目录数

function checkForFilesInCurrentDir(){
    # check if there are files in the current directory
    doFilesExist=`find . -maxdepth 1 -type f`
    if [ -z "$doFilesExist" ]; then
    #   echo "no files"
        return 0 
    else
    #   echo "some files"
        return 1
    fi
}
function numOfDirsInWD(){
    # check number of dirs in working directory and return that num
    checkForDirs=`find . -mindepth 1 -maxdepth 1 -type d`
    numOfDirs=$(echo "$checkForDirs" | egrep -c "./")
    return $numOfDirs
}
下面的函数是脚本本身。它打开一个无限循环,检查文件,如果检测到它们,它将中断,然后检查wd中有多少个dir,如果有多个或少个dir,它将中断循环。否则,它会对唯一目录执行ls和cd。然后在下一个目录上重复。上升

function cdwn (){
    while :
    do
        # check for files
        checkForFilesInCurrentDir
        filesExist="$?"
        if [[ filesExist -eq 1 ]]; then
            echo "files exist"
            break
        fi

        # check that only one dir
        numOfDirsInWD
        numOfDirs="$?"
        if [[ numOfDirs -eq 1 ]]; then
            # cd to it
            echo "only one dir"
            name=`ls`
            cd "$name"
        else
            # break out of the loop
            echo "there is not a sole dir"
            break
        fi
    done
}

在我现在的烂电脑上,下载大约十个空目录大约需要18秒。。。主要的延误似乎发生在一开始。我会在早上检查回复,被这些东西迷住了,现在是早上6点。。。非常感谢您的帮助。

首先,获取如下文件的数量:

function numOfDirsInWD(){
    # check number of dirs in working directory and return that num
    checkForDirs=`find . -mindepth 1 -maxdepth 1 -type d`
    return ${checkForDirs[#]}
}
这将是相当快和容易

其次,你忘了一个美元符号。将您的替换为以下行:

if [[ $numOfDirs -eq 1 ]]; then
如果您遇到更多问题,请发布

您还可以使用以下代码立即更改目录:

cd $(ls)
也就是说,如果只有一个dir


至于为什么会陷入无限循环:如果dir中有多个文件,则继续执行。检查文件数是否为
ge 1
,并相应断开。所以更改这一行:
if[[fileexist-eq 1];然后
如果[[fileexist-ge 1]];然后

在旁注中,你可以使用反勾号来使用内联代码:`,为此,请将你的代码包含在所述反勾号中。我不明白,你是什么意思?没什么,我以为你试图在文本中嵌入代码,但这是另一个人解决的格式问题,所以不用担心。哦,我的错!再次感谢:)我不懂第一点,checkForDirs是内置命令吗?我把你放在那里的代码输入我的git bash终端,但它不起作用。啊,我的错!我添加了美元符号,它的速度更快,但现在当它到达末尾时,它返回<后面是一大堆看起来像笑脸的特殊字符..不,它不是一个命令,它是一个变量,您可以将数组的大小指定给它
checkForDirs
。您应该通过这些行来更改脚本中的代码行。@user122072这是Windows上的吗?在我的Linux系统上,下降到10个dir需要0.06秒。@thatotherguy哈哈,一个有效的问题,可能应该从这个开始。哦,我的糟糕!我已遵照您的cd建议,只使用了cd“$(ls”这就解决了dirs使用空格的问题,这一点很重要,因为我在windows上。在两个位置修复了丢失的$后,现在速度非常快。我还没有使用你的dirs数组的数量,但我现在对脚本很满意!感谢你的帮助!现在和之前!现在我没有理由不睡觉了。快7点了…谢谢谢谢你的帮助!我会为这个问题打分并投票表决