Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 在bash脚本中连接输入_Linux_Bash_Shell_Unix - Fatal编程技术网

Linux 在bash脚本中连接输入

Linux 在bash脚本中连接输入,linux,bash,shell,unix,Linux,Bash,Shell,Unix,我想连接传递给bash脚本的所有参数,除了标志 例如,如果脚本采用如下输入: ./myBashScript.sh -flag1 exampleString1 exampleString2 我希望结果是“exampleString1\u exampleString2” 我可以对预定义数量的输入(即2)执行此操作,但如何对任意数量的输入执行此操作?这是一个丑陋但简单的解决方案: echo $* | sed -e "s/ /_/g;s/[^_]*_//" 在这种情况下,“$*”似乎正是您想要的。它

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

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

./myBashScript.sh -flag1 exampleString1 exampleString2
我希望结果是“exampleString1\u exampleString2”


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

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

echo $* | sed -e "s/ /_/g;s/[^_]*_//"
在这种情况下,
“$*”
似乎正是您想要的。它很少是正确的选择,但在这里,它确实是正确的选择

或者,只需循环并连接:

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

$pad
变量确保您不会在
$The_rest

的开头出现错误的下划线,这是一段我真正引以为豪的代码(我认为这是非常shell风格的代码)

我已经解释了你的问题,以便删除所有以
-
开头的参数

如果只想删除从
-
开始的参数的开始序列:

#!/bin/sh

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

IFS=_ ; echo "$*"
如果只想删除第一个参数:

#!/bin/sh

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

还可以使用格式化字符串连接参数

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

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

哎呀

O.p.如何在每个参数之间获得所需的''.'值?您可以设置IFS,使其第一个字符包含
.
,以影响
$*
串联的行为。并且错误。你读过这个问题吗?@JoSo:是的,这属于它的(措辞拙劣的)规范。不管你如何解释规范,你的解决方案都是错误的。它甚至没有删除任何论点。哦,我明白了,你是对的,修正了。现在情况好多了,但根据任何解释,它仍然不可能完全正确。考虑第一个参数中的空格。抱歉是严格的,但是-1是坏shell样式(非常冗长和<代码> [[…] /代码>是不可移植的)。改为使用
test
,并查看我的解决方案以获得使用
${a:0:1}
@JoSo的替代方案为什么
[[]]
不可移植?它是bash,而不是POSIX sh(与
${a:0:1}
相同)。虽然有些人并不在意,因为bash几乎安装在任何系统上,但在这种情况下它确实没有任何用处(改用
test
alias
[…]
。@JoSo he明确表示他正在使用bash。我没有说它不起作用,只是风格(和可移植性)的问题。我们应该给出一些好的编码实践的例子,而你的不是。
#!/bin/sh

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

IFS=_ ; echo "$*"
#!/bin/sh

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

catted="$(paramCat "$@")"
echo ${catted/_/} 
# assuming flag is first arg and optional
flag=$1
[[ $1 = ${1#-} ]] && unset $flag || shift

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