将bash中第一个N以外的剩余参数串联在一起 [英] Concatenating remaining arguments beyond the first N in bash

查看:80
本文介绍了将bash中第一个N以外的剩余参数串联在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前不必编写任何bash脚本.这是我需要做的.

I did not have to write any bash script before. Here is what I need to do.

我的脚本将使用一组字符串参数运行. st的数量将大于8.我将必须连接字符串9和之后的字符串,并从中组合一个字符串.这样...

My script will be run with a set of string arguments. Number of stings will be more than 8. I will have to concatenate strings 9 and onward and make a single string from those. Like this...

myscript s1 s2 s3 s4 s5 s6 s7 s8 s9 s10 ....(完全未知)

myscript s1 s2 s3 s4 s5 s6 s7 s8 s9 s10....(total unknown)

在脚本中,我需要这样做...

in the script, I need to do this...

新字符串= s9 + s10 + ...

new string = s9 + s10 + ...

我正在尝试类似的操作...(通过网络搜索).

I am trying something like this...(from web search).

 array="${@}"
 tLen=${#array[@]}
 # use for loop  to read string beyond 9
 for (( i=8; i<${tLen}; i++ ));
 do
   echo ${array[$i]}  --> just to show string beyond 9
 done

不起作用.如果i = 0,它会打印出来.这是我的意见.

Not working. It prints out if i=0. Here is my input.

./tastest 1 2 3 4 5 6 7 8 A B C

./tastest 1 2 3 4 5 6 7 8 A B C

我希望打印A BC.最后,我将不得不做ABC.

I am expecting A B C to be printed. Finally I will have to make ABC.

任何人都可以帮忙吗?

推荐答案

它应该比问题中的循环简单得多:

It should be a lot simpler than the looping in the question:

shift 8
echo "$*"

丢失参数1-8;将所有其他参数打印为单个字符串,并用单个空格分隔参数(以及保留的参数中的空格).

Lose arguments 1-8; print all the other arguments as a single string with a single space separating arguments (and spaces within arguments preserved).

或者,如果您需要一个变量,则:

Or, if you need it in a variable, then:

nine_onwards="$*"

或者,如果您不能在主shell进程中丢弃前8个参数,则:

Or if you can't throw away the first 8 arguments in the main shell process:

nine_onwards="$(shift 8; echo "$*")"

您可以检查是否至少有9个参数,如果没有,则在抱怨.或者,您也可以接受一个空字符串-没有错误.

You can check that there are at least 9 arguments, of course, complaining if there aren't. Or you can accept an empty string instead — with no error.

如果参数必须没有空格连接在一起(如对该问题的修正),那么您就必须弄混$IFS:

And if the arguments must be concatenated with no space (as in the amendment to the question), then you have to juggle with $IFS:

nine_onwards="$(shift 8; IFS=""; echo "$*")"


如果我正确地从该答案下面解释了注释,那么您想将前8个参数保存在8个单独的简单(非数组)变量中,然后将9个参数再保存在另一个简单变量中,且之间没有空格参数值.


If I'm interpreting the comments from below this answer correctly, then you want to save the first 8 arguments in 8 separate simple (non-array) variables, and then arguments 9 onwards in another simple variable with no spaces between the argument values.

这是微不足道的:

var1="$1"
var2="$2"
var3="$3"
var4="$4"
var5="$5"
var6="$6"
var7="$7"
var8="$8"
var9="$(shift 8; IFS=""; echo "$*")"

名称不必与名称紧密相关.您可以使用:

The names don't have to be as closely related as those are. You could use:

teflon="$1"
absinthe="$2"
astronomy="$3"
lobster="$4"
darkest_peru="$5"
mp="$6"
culinary="$7"
dogma="$8"
concatenation="$(shift 8; IFS=""; echo "$*")"

您也不必按此顺序进行操作;任何序列(排列)都可以很好地完成工作.

You don't have to do them in that order, either; any sequence (permutation) will do nicely.

也请注意,在问题中,您有:

Note, too, that in the question, you have:

array="${@}"

尽管有名称,它会创建一个包含参数的简单变量.要创建数组,必须使用这样的括号,其中的空格是可选的:

Despite the name, that creates a simple variable containing the arguments. To create an array, you must use parentheses like this, where the spaces are optional:

array=( "$@" )

这篇关于将bash中第一个N以外的剩余参数串联在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆