Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果所有文件的大小都小于某个值,Bash函数将继续运行_Bash - Fatal编程技术网

如果所有文件的大小都小于某个值,Bash函数将继续运行

如果所有文件的大小都小于某个值,Bash函数将继续运行,bash,Bash,我很少接触Bash脚本。我试图实现一个bash函数,该函数仅在前面的命令成功时运行某些命令。特别是在这里,如果所有文件大小都小于某个大小,我想运行该函数 MWE 函数myfunc(){ files=`find.-type f-size+1M-print0 | xargs-0 du-h | sort-rh` echo“大于1MB${Files}的文件” 如果len(files)>0,则 echo“至少有一个文件较大,正在停止该函数” 其他的 echo“所有文件的大小都小于X,请运行其他命令” 呼应

我很少接触Bash脚本。我试图实现一个bash函数,该函数仅在前面的命令成功时运行某些命令。特别是在这里,如果所有文件大小都小于某个大小,我想运行该函数

MWE
函数myfunc(){
files=`find.-type f-size+1M-print0 | xargs-0 du-h | sort-rh`
echo“大于1MB${Files}的文件”
如果len(files)>0,则
echo“至少有一个文件较大,正在停止该函数”
其他的
echo“所有文件的大小都小于X,请运行其他命令”
呼应“成功”
fi
}

如何使函数工作?

如果要计算变量中的项数,该变量应为数组

在这种情况下,存储一个包含原始文件列表的数组,并通过
du
sort
运行该数组更有意义,仅用于输出目的:

#!/usr/bin/env bash
case $BASH_VERSION in ''|[0-2].*) echo "ERROR: bash 3.x needed" >&2; exit 1;; esac

myfunc() {
    # bash 3.0-compatible; readarray requires bash 4
    local file; local -a files=( )
    while IFS= read -r -d '' file; do
      files+=( "$file" )
    done < <(find . -type f -size +1M -print0)

    echo "${#files[@]} found larger than 1MB" >&2

    if (( ${#files[@]} > 0 )); then
        # actually print a listing of our larger files
        printf '%s\0' "${files[@]}" | xargs -0 du -h | sort -rh >&2
        echo "At least one file has larger size, stopping the function" >&2
    else
        echo "All files are less than size X, run other commands" >&2
        echo "success"
    fi
}

myfunc "$@"  # actually start our function
#/usr/bin/env bash
“|[0-2].*)echo中的$BASH_版本错误:需要BASH 3.x”>&2;出口1;;以撒
myfunc(){
#bash 3.0兼容;readarray需要bash 4
本地文件;本地-a文件=()
当IFS=read-r-d“”文件时,执行以下操作
文件+=(“$file”)
完成<&2
如果(${#files[@]}>0),则
#实际上,打印一个较大文件的列表
printf“%s\0”${files[@]}“| xargs-0 du-h | sort-rh>&2
echo“至少有一个文件较大,正在停止函数”>&2
其他的
echo“所有文件的大小都小于X,请运行其他命令”>&2
呼应“成功”
fi
}
myfunc“$@”#实际启动我们的函数

注意,为用户使用而编写的输出,而不是为其他软件读取的输出,应该在stderr上;因此,
&2
s.

如果要计算变量中的项数,该变量应该是一个数组

在这种情况下,存储一个包含原始文件列表的数组,并通过
du
sort
运行该数组更有意义,仅用于输出目的:

#!/usr/bin/env bash
case $BASH_VERSION in ''|[0-2].*) echo "ERROR: bash 3.x needed" >&2; exit 1;; esac

myfunc() {
    # bash 3.0-compatible; readarray requires bash 4
    local file; local -a files=( )
    while IFS= read -r -d '' file; do
      files+=( "$file" )
    done < <(find . -type f -size +1M -print0)

    echo "${#files[@]} found larger than 1MB" >&2

    if (( ${#files[@]} > 0 )); then
        # actually print a listing of our larger files
        printf '%s\0' "${files[@]}" | xargs -0 du -h | sort -rh >&2
        echo "At least one file has larger size, stopping the function" >&2
    else
        echo "All files are less than size X, run other commands" >&2
        echo "success"
    fi
}

myfunc "$@"  # actually start our function
#/usr/bin/env bash
“|[0-2].*)echo中的$BASH_版本错误:需要BASH 3.x”>&2;出口1;;以撒
myfunc(){
#bash 3.0兼容;readarray需要bash 4
本地文件;本地-a文件=()
当IFS=read-r-d“”文件时,执行以下操作
文件+=(“$file”)
完成<&2
如果(${#files[@]}>0),则
#实际上,打印一个较大文件的列表
printf“%s\0”${files[@]}“| xargs-0 du-h | sort-rh>&2
echo“至少有一个文件较大,正在停止函数”>&2
其他的
echo“所有文件的大小都小于X,请运行其他命令”>&2
呼应“成功”
fi
}
myfunc“$@”#实际启动我们的函数

注意,为用户使用而编写的输出,而不是为其他软件读取的输出,应该在stderr上;因此,
&2
s.

请注意,
文件=$(…任何东西…)
通常有缺陷。请参阅。
function
关键字是从遗留ksh借用的语法-POSIX sh标准化语法只是将函数定义为
myfunc(){
。请注意
files=$(…任何东西…)
通常是错误的。请参阅。
function
关键字是从遗留ksh借用的语法-POSIX sh标准化语法只是将函数定义为
myfunc(){
。请参阅
错误:bash 4.x needed
的意思正好是它所说的。什么是
echo“$bash\u VERSION”
说你有吗?它是否适用于bash
GNU bash,版本3.2.57(1)-发行版(x86_64-apple-darwin19)
Ahh。好的,更新后可以使用苹果公司提供的bash的古老版本。(如果你用Nix、Macports、Homebrew等安装bash,你会得到一个更新得多的版本——它在5.x上,但苹果拒绝提供任何东西,因为在4.0中切换到GPLv3许可)。我正试图编写一个简单的git pull and push函数,但如果某些文件大小更大,并且我正在使用内置bash 3.2的Macos中工作,它就会失败。
错误:bash 4.x needed
的意思正是它所说的。
echo“$bash_VERSION”
说你有什么?是否适用于bash
GNU bash,版本3.2.57(1)-发行版(x86_64-apple-darwin19)
Ahh.好的,更新后可以使用苹果提供的bash的古老版本。(如果你用Nix、Macports、Homebrew等安装bash,你会得到一个更新得多的版本——它在5.x上,但苹果拒绝提供任何东西,因为在4.0中切换到GPLv3许可).我正在尝试编写一个简单的git pull and push函数,但如果某些文件大小更大,并且我正在使用内置bash 3.2的Macos中工作,则会失败。