VAR = $ {N:-m} Bash中使用 [英] VAR=${n:-m} usage in Bash
问题描述
我想写一个遗传算法基于一张贴在这里bash下的http://father-natures.blogspot.mx/2013/04/implementing-genetic-algorithm-in-bash.html.我事先脚本相当不熟练,我没有得到什么VAR = $ {N:-m}表示。我的猜测是,这样的话:
POOL_SIZE = $ {1:-6}
请$ 1 = -6,但是当我检查$ 1中是空的,当我检查$ POOLSIZE我得到6。
利伯塔德@ engrane4:〜$回声POOL_SIZE
6
这对我来说是相当混乱。如果我想的变量是6,我会写:
POOL_SIZE = 6
你能告诉我我在想什么(还有什么是该做的分配)?
感谢您,
它规定的情况下,默认的 $ 1
是空的。
$ {参数:-word}
如果参数没有设置或者为空,word的扩展将被替换。
否则,参数的值是取代
块引用>示例
$回声$ {A: - 你好}
你好
$ a =测试
$回声$ {一个: - 你好}
测试基于您的评论
谢谢,@fedorqui。最初的变量是POOL_SIZE = $ {1:-6},
REPRO_CHANCE = $ {2:-30},BEST_FITS = $ {3} -70。现在我想知道如果
$ {POOL_SIZE:-6} $ {REPRO_CHANCE:-30}和$ {BEST_FITS:-70}将是
同样的,为什么是记数需要
块引用>如果您有
POOL_SIZE = $ {1:-6}
REPRO_CHANCE = $ {2:-30}
BEST_FITS = $ {3} -70这是因为
POOL_SIZE
,REPRO_CHANCE
和BEST_FITS
应该包含$ 1
,$ 2
和$ 3
。任何$ N
表示第n
参数,例如从脚本。所以,如果你有以下脚本:$猫
#!/斌/庆典POOL_SIZE = $ {1:-6}
REPRO_CHANCE = $ {2:-30}
BEST_FITS = $ {3} -70回声POOL_SIZE = $ POOL_SIZE
回声REPRO_CHANCE = $ REPRO_CHANCE
回声BEST_FITS = $ BEST_FITS然后与不同量的参数执行会产生:
$ ./a
POOL_SIZE = 6
REPRO_CHANCE = 30
BEST_FITS = 70$ ./a 2 2 2
POOL_SIZE = 2
REPRO_CHANCE = 2
BEST_FITS = 2$ ./a 24 2
POOL_SIZE = 24
REPRO_CHANCE = 2
BEST_FITS = 70我希望它明确。
还要注意的是
$ {VAR:-value}
和$ {VAR值}
是不一样的: $之间的区别是什么{VAR:-word}?和$ {VAR字}I want to write a Genetic Algorithm for bash based in the one posted here: http://father-natures.blogspot.mx/2013/04/implementing-genetic-algorithm-in-bash.html. I am quite inexpert in advance scripting and I don't get what VAR=${n:-m} stands for. My guess was that things like:
POOL_SIZE=${1:-6}
Make $1=-6, however when I check $1 it is empty and when I check $POOLSIZE I get 6.
libertad@engrane4:~$ echo "POOL_SIZE" 6
This is quite confusing for me. If I wanted the variable to be 6 I would write:
POOL_SIZE=6
Could you tell me what am I missing (what else is this assignation doing)?
Thank you,
解决方案It sets a default in case
$1
is empty.From 3.5.3 Shell Parameter Expansion in the Bash Reference Manual:
${parameter:-word}
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
Example
$ echo ${a:-"hello"} hello $ a="test" $ echo ${a:-"hello"} test
Based on your comment
Thanks, @fedorqui. The original variables were POOL_SIZE=${1:-6}, REPRO_CHANCE=${2:-30}, BEST_FITS=${3:-70}. Now I am wondering if ${POOL_SIZE:-6}, ${REPRO_CHANCE:-30} and ${BEST_FITS:-70} would be the same and why is the numeration needed
If you have
POOL_SIZE=${1:-6} REPRO_CHANCE=${2:-30} BEST_FITS=${3:-70}
it is because
POOL_SIZE
,REPRO_CHANCE
andBEST_FITS
are supposed to contain the value of$1
,$2
and$3
. Any$n
means thenth
parameter, for example from a script. So if you have the following script:$ cat a #!/bin/bash POOL_SIZE=${1:-6} REPRO_CHANCE=${2:-30} BEST_FITS=${3:-70} echo "POOL_SIZE=$POOL_SIZE" echo "REPRO_CHANCE=$REPRO_CHANCE" echo "BEST_FITS=$BEST_FITS"
Then its execution with different amount of parameters would yield:
$ ./a POOL_SIZE=6 REPRO_CHANCE=30 BEST_FITS=70 $ ./a 2 2 2 POOL_SIZE=2 REPRO_CHANCE=2 BEST_FITS=2 $ ./a 24 2 POOL_SIZE=24 REPRO_CHANCE=2 BEST_FITS=70
I hope it makes it clear.
Note also that
${var:-value}
and${var-value}
are not the same: What is the difference between ${var:-word} and ${var-word}?.这篇关于VAR = $ {N:-m} Bash中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!