我正在研究使用${parameter:-word}vs 的参数扩展${parameter:=word}。
${parameter:-word}如果参数未设置或为空,则替换单词的扩展。
${parameter:=word}如果参数未设置或为空,则将单词的扩展分配给参数。然后替换参数的值。
当然,关键的区别在于每个描述的第一句,替换与分配。
但实际上,我不知道当某事是时substituted与当某事是时会发生什么assigned。
最后,什么时候使用其中一种比较合适?
我在 bash 中有以下代码,我想知道我是否可以摆脱\+并简单地使用+
find "$fdir" \
-type f -name "*.org" -o -name "*.texi" \
-exec head -n "$n" {} \+ \
| grep --color -e ^ -e '^==>.*'
Run Code Online (Sandbox Code Playgroud) 我正在使用find和grep命令。
当多个选项由“或”与-o标志和分组括号的使用连接时,以及何时不使用分组括号时,感到非常困惑。
使用时find,分组括号似乎是必要的
find $fdir ( -name *.texi -o -name *.org )
Run Code Online (Sandbox Code Playgroud)
使用时grep,不使用分组括号
grep --include "*.texi" --exclude "*.org"
Run Code Online (Sandbox Code Playgroud) 当我有两个参数时,我正在运行以下代码
if (( $# == 2 )); then
: ${fdir:="${@:-1}"}
pfm -w2 "" "unspecified -d option"
echo "use last argument as substitute"
printf '%s\n\n' "fdir: ${@:-1}"
echo "\$1: $1 \$2: $2"
Run Code Online (Sandbox Code Playgroud)
这是结果
pregion --dyn "John" ./01cuneus
pregion --dyn John ./01cuneus
unspecified -d option
use last argument as substitute
fdir: John
./01cuneus
$1: John $2: ./01cuneus
Run Code Online (Sandbox Code Playgroud) 我有一个nmser在 bash 脚本中命名的数组,我想从中创建一个字符串并将其存储在变量中flsel。
flsel=echo "${nmser[*]}"
Run Code Online (Sandbox Code Playgroud) 我find通过构造一个isufx包含文件名后缀的数组来调用。
因此,我有
echo "isufx: ${isufx[*]}"
Run Code Online (Sandbox Code Playgroud)
这导致
-name *.texi -o -name *.org -o
Run Code Online (Sandbox Code Playgroud)
最后,我删除了数组中的最后一个元素 (-o),这样我就可以将它与find.
find "$fdir" "${isufx[@]}"
Run Code Online (Sandbox Code Playgroud)
对于数组索引不是从 0 开始的情况,我询问使用什么技术来删除更健壮的最后一个元素。
我在语句中使用正则表达式和通配符if。
例子:
[[ "$exitfn" =~ ^[yY]*$ ]] && return
[[ "$exitfn" == "[yY]*" ]] && return
[[ "$exitfn" == "*$fs*" ]] && return
[[ "$exitfn" == *$fs* ]] && return
Run Code Online (Sandbox Code Playgroud)
但真的很想知道如果我引用*$fs*"或不引用会带来什么困难*$fs*。
还想知道如何使用=~against ==。