Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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/6/multithreading/4.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
String 将除第一个参数以外的所有参数添加到字符串_String_Bash_Sh_Args - Fatal编程技术网

String 将除第一个参数以外的所有参数添加到字符串

String 将除第一个参数以外的所有参数添加到字符串,string,bash,sh,args,String,Bash,Sh,Args,试图将所有参数解析为一个字符串,但我的代码只给出了它找不到的错误i: test: line 3: =: command not found test: line 7: [i: command not found test: line 7: [i: command not found test: line 7: [i: command not found test: line 7: [i: command not found test: line 7: [i: command not found

试图将所有参数解析为一个字符串,但我的代码只给出了它找不到的错误
i

test: line 3: =: command not found
test: line 7: [i: command not found
test: line 7: [i: command not found
test: line 7: [i: command not found
test: line 7: [i: command not found
test: line 7: [i: command not found
暗号

#!/bin/sh

$msg=""

for i in $@
do
if [i -gt 1]; then
    msg="$msg $i"
fi
done
编辑:thx为所有的帮助,让它工作。如果有人感兴趣,我的最终解决方案是:

#!/bin/sh

args=""
for i in "${@:2}"
do
    args="$args $i"
done
[
只是(或多或少地)测试的别名,所以您应该像使用常规命令一样使用它。我想说的是,您需要在
[
之前和之后使用空格,如下所示:

if [ $i -gt 1 ]; then
另外,您在
if
子句中忘记了
$
之前的
$

[
只是(或多或少)测试的别名,所以您应该像常规命令一样使用它。我想说的是,您需要在
[
之前和之后使用空格,如下所示:

if [ $i -gt 1 ]; then
您还忘记了
if
子句中
i
之前的
$

显示您的特定错误消息是因为:

  • 赋值给变量不是像在
    $msg=“”
    中那样使用
    $
    字符,而是应该使用
    msg=“”
    ;和

  • [
    实际上是一个命令,它应该用空格与其他单词隔开,这样shell就不会认为您试图执行某个虚构的
    [i
    命令

但是,您还有几个其他问题。第一个问题是,
i
的值需要使用
$i
来获取,而不仅仅是
i
。单独使用
i
会导致以下错误:

-bash: [: i: integer expression expected
因为
i
本身不是数值

其次,
i
$i
都不是可以与1进行比较的索引,因此
$i-gt 1
表达式将不起作用。单词
$i
将扩展到参数的值,而不是它的索引


但是,如果您真的想处理参数列表中除第一个元素以外的所有元素,
bash
有一些非常类似于C的结构,这将使您的生活更加轻松:

for ((i = 2; i <= $#; i++)) ; do   # From two to argc inclusive.
    echo Processing ${!i}
done
要构造包含这些参数的字符串,可以使用以下内容:

msg="$2"                           # Second arg (or blank if none).
for ((i = 3; i <= $#; i++)) ; do   # Append all other args.
    msg="$msg ${!i}"
done
尽管在这种情况下,有一种更简单的方法根本不涉及任何(显式)循环:

msg="${@:2}"
显示您的特定错误消息是因为:

  • 赋值给变量不是像在
    $msg=“”
    中那样使用
    $
    字符,而是应该使用
    msg=“”
    ;和

  • [
    实际上是一个命令,它应该用空格与其他单词隔开,这样shell就不会认为您试图执行某个虚构的
    [i
    命令

但是,您还有几个其他问题。第一个问题是,
i
的值需要使用
$i
来获取,而不仅仅是
i
。单独使用
i
会导致以下错误:

-bash: [: i: integer expression expected
因为
i
本身不是数值

其次,
i
$i
都不是可以与1进行比较的索引,因此
$i-gt 1
表达式将不起作用。单词
$i
将扩展到参数的值,而不是它的索引


但是,如果您真的想处理参数列表中除第一个元素以外的所有元素,
bash
有一些非常类似于C的结构,这将使您的生活更加轻松:

for ((i = 2; i <= $#; i++)) ; do   # From two to argc inclusive.
    echo Processing ${!i}
done
要构造包含这些参数的字符串,可以使用以下内容:

msg="$2"                           # Second arg (or blank if none).
for ((i = 3; i <= $#; i++)) ; do   # Append all other args.
    msg="$msg ${!i}"
done
尽管在这种情况下,有一种更简单的方法根本不涉及任何(显式)循环:

msg="${@:2}"

对于特定的输出,实际上不需要循环(假设单个字符串实际上是正确的输出):

但是,如果您计划稍后对参数进行迭代,则平面字符串是错误的方法。您应该使用数组:

args=( "${@:2}" )

对于特定的输出,实际上不需要循环(假设单个字符串实际上是正确的输出):

但是,如果您计划稍后对参数进行迭代,则平面字符串是错误的方法。您应该使用数组:

args=( "${@:2}" )

在使用
[]
时应该真正引用变量,或者只使用
[[]]
就像bash一样。看起来您试图使用$i作为命令参数的索引。这不起作用。$i是实际参数本身。使用
shift
放弃第一个参数,将所有其他参数移到左侧一个位置。然后只需执行`msg=“$@”我想OP也不知道如何分配变量。关于
$msg=”“
:)不,我在5分钟内用sh进行了少量编码:)现在开始工作了,谢谢anyway@Prototype从bash手册页面:可以通过以下形式的语句为变量赋值:
name=[value]
。所以赋值变量应该不带美元。;)在使用
[]
时应该真正引用变量,或者只使用
[[]]
就像bash一样。看起来您试图使用$i作为命令参数的索引。这不起作用。$i是实际参数本身。使用
shift
放弃第一个参数,将所有其他参数移到左侧一个位置。然后只需执行`msg=“$@”我想OP也不知道如何分配变量。关于
$msg=”“
:)不,我在5分钟内用sh进行了少量编码:)现在开始工作了,谢谢anyway@Prototype从bash手册页面:可以通过以下形式的语句为变量赋值:
name=[value]
。因此赋值变量应不带美元。;)第一个错误(
test:line 3:=:command not found
)源于
msg
:将第3行替换为
msg=”“
(没有美元,这是shell,不是Perl或PHP)。第一个错误(
test:line 3:=:command not found
)源于
msg
:将第3行替换为
msg=“”
(没有美元,这是