简体   繁体   English

bash 中的回声表达

[英]Echo expression in bash

I have been trying to print maximal depth of directory but I can't figure out how to print the results in the format "Maximal depth: xxx"我一直在尝试打印目录的最大深度,但我不知道如何以“最大深度:xxx”格式打印结果

    dir_depth() {
      cd "$1"
      maxdepth=0
      for d in */.; do
        [ -d "$d" ] || continue
        depth=`dir_depth "$d"`
        maxdepth=$(($depth > $maxdepth ? $depth : $maxdepth))
      done
      echo $((1 + $maxdepth)) #this line is problem
    }

dir_depth "$@"

If I try to do it like如果我尝试这样做

    foo=$((1 + $maxdepth))
    echo "Maximal depth: " $foo

then I get an error然后我得到一个错误

Max depth: 1: expression recursion level exceeded (error token is "depth: 1")

You have all the parts, just need to assemble them correctly;您拥有所有零件,只需要正确组装它们;

echo "Maximal depth:  $((1 + maxdepth))"

For example:例如:

x=5
echo "Max $((1+x))"

Results in Max 6 being printed.结果打印了Max 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM