代码之家  ›  专栏  ›  技术社区  ›  l0b0

Bash“合并”功能

  •  3
  • l0b0  · 技术社区  · 14 年前

    if [ -z "$foo" ]
    then
        source "$foo/subdir/yo.sh"
    else
        source "$bar/yo.sh"
    fi
    

    (注意,只有在使用foo时才会出现subdir)。目前最简单的解决方案是:

    source "${foo:-$bar}${foo:+/subdir}/yo.sh"
    

    换句话说,取foo或bar,然后附加 /subdir

    3 回复  |  直到 12 年前
        1
  •  2
  •   sorpigal    14 年前

    试试这个:

    $((printf $foo 2>/dev/null && printf '/subdir') || printf $bar)/yo.sh
    

    不过有点复杂。

    因为这个原因 printf 需要打印,但其他可以替换为 echo -n

        2
  •  0
  •   Daenyth    14 年前
    if [[ -d $foo ]]; then
        dir="$foo/subdir"
    else
        dir="$bar"
    fi
    source "$dir/yo.sh"
    
        3
  •  -1
  •   Alberto Zaccagni    14 年前

    尝试

    amithere=$foo   
    if [ ! -z "$amithere" ]   
    then    
        amithere=$bar    
    fi    
    source "$amithere/yo.sh"