在串联bash脚本输入 [英] concatenate inputs in bash script
本文介绍了在串联bash脚本输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想串联传递给我的bash脚本除了标志的所有参数。
因此,例如,如果脚本的输入参数如下:
./ myBashScript.sh -flag1 exampleString1 exampleString2
我想要的结果是exampleString1_exampleString2
我可以输入一个predefined数(即2),但我怎么能做到这一点,用于输入任意号码?
做到这一点解决方案
功能concatenate_args
{
串=
为在$ @#遍历参数
做
如果[[$ {A:0:1}!= - ]]#忽略标志(第一个字符是 - )
然后
如果[[$字符串!=]]
然后
字符串+ =_#的Delimeter
科幻
字符串+ =$ A
科幻
DONE
回声$字符串
}#用途:
ARGS =$(concatenate_args$ @)
I would like to concatenate all the arguments passed to my bash script except the flag.
So for example, If the script takes inputs as follows:
./myBashScript.sh -flag1 exampleString1 exampleString2
I want the result to be "exampleString1_exampleString2"
I can do this for a predefined number of inputs (i.e. 2), but how can i do it for an arbitrary number of inputs?
解决方案
function concatenate_args
{
string=""
for a in "$@" # Loop over arguments
do
if [[ "${a:0:1}" != "-" ]] # Ignore flags (first character is -)
then
if [[ "$string" != "" ]]
then
string+="_" # Delimeter
fi
string+="$a"
fi
done
echo "$string"
}
# Usage:
args="$(concatenate_args "$@")"
这篇关于在串联bash脚本输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文