Linux 在 bash 脚本中连接输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9354847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:40:25  来源:igfitidea点击:

concatenate inputs in bash script

linuxbashshellunix

提问by zpesk

I would like to concatenate all the arguments passed to my bash script except the flag.

我想连接传递给我的 bash 脚本的所有参数,除了标志。

So for example, If the script takes inputs as follows:

例如,如果脚本采用如下输入:

./myBashScript.sh -flag1 exampleString1 exampleString2

I want the result to be "exampleString1_exampleString2"

我希望结果是“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?

我可以为预定义数量的输入(即 2)执行此操作,但是如何为任意数量的输入执行此操作?

回答by mvds

This is an ugly but simple solution:

这是一个丑陋但简单的解决方案:

echo $* | sed -e "s/ /_/g;s/[^_]*_//"

回答by Tyilo

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 "$@")"

回答by Jonathan Leffler

flag=""
shift
oldIFS="$IFS"
IFS="_"
the_rest="$*"
IFS="$oldIFS"

In this context, "$*"is exactly what you're looking for, it seems. It is seldom the correct choice, but here's a case where it really is the correct choice.

在这种情况下"$*",这似乎正是您要寻找的。这很少是正确的选择,但这里有一个案例,它确实是正确的选择。

Alternatively, simply loop and concatenate:

或者,只需循环并连接:

flag=""
shift
the_rest=""
pad=""
for arg in "$@"
do
    the_rest="${the_rest}${pad}${arg}"
    pad="_"
done

The $padvariable ensures that you don't end up with a stray underscore at the start of $the_rest.

$pad变量确保您不会在$the_rest.

回答by Jo So

Here's a piece of code that I'm actually proud of (it is very shell-style I think)

这是一段我引以为豪的代码(我认为它是非常 shell 风格的)

#!/bin/sh

firsttime=yes
for i in "$@" 
do
    test "$firsttime" && set -- && unset firsttime
    test "${i%%-*}" && set -- "$@" "$i"
done

IFS=_ ; echo "$*"

I've interpreted your question so as to remove allarguments beginning with -

我已经解释了你的问题,以删除所有-

If you only want to remove the beginning sequence of arguments beginnnig with -:

如果您只想删除参数 beginnnig 的开头序列-

#!/bin/sh

while ! test "${1%%-*}"
do
    shift
done

IFS=_ ; echo "$*"

If you simply want to remove the first argument:

如果您只想删除第一个参数:

#!/bin/sh

shift
IFS=_ ; printf %s\n "$*"

回答by user unknown

#!/bin/bash
paramCat () {
for s in "$@" 
do
    case $s in 
        -*)
            ;;
        *) 
            echo -n _${s}
            ;; 
    esac
done
}

catted="$(paramCat "$@")"
echo ${catted/_/} 

回答by nickl-

You can also use formatted strings to concatenate args.

您还可以使用格式化字符串来连接 args。

# assuming flag is first arg and optional
flag=
[[  = ${1#-} ]] && unset $flag || shift

concat=$(printf '%s_' ${@})
echo ${concat%_} # to remove the trailing _

nJoy!

快乐!