【问题标题】:cd ${1:-.} what does that mean [duplicate]cd ${1:-.} 这是什么意思[重复]
【发布时间】:2017-06-30 09:40:39
【问题描述】:
#====================script 5 -- ls reccurssive avec cd =======
#!/bin/bash
exec 2>/dev/null # redirige stderr pour toute la suite
# au cas ou le script est invoque sans argument $1
# n'existe pas, la commande suivante devient cd .
cd ${1:-.} # problem that i miss understood
for i in * ; do
if [ -d $i ] ; then
echo "$PWD/$i/ <-- repertoire"
$0 $i # le script s'invoque lui-même
else
echo $PWD/$i
fi
done

================================================ ====

谁能给我解释一下这张 cd ${1:-.} 是什么意思,如果有文章解释一下如何使用它

【问题讨论】:

  • 您的问题已包含法语答案。

标签: bash parameter-expansion


【解决方案1】:

${a:-b} 表示,如手册中所述,如果已定义,则使用 $a,否则仅使用 b

这里的想法是,如果脚本收到一个参数,$1 将被定义,脚本将cd 到那个目录。如果脚本没有收到参数,${1-.} 将扩展为提供的默认值.

由于. 表示当前目录而cd . 是空操作,这基本上意味着“cd$1 如果可用,否则只需继续执行脚本。”

【讨论】:

    猜你喜欢
    • 2020-03-03
    • 2015-05-27
    • 2019-04-24
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 2014-09-19
    • 2011-10-09
    • 2020-09-01
    相关资源
    最近更新 更多