Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.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/2/shell/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
Bash 在shell脚本中生成用户输入循环变量_Bash_Shell - Fatal编程技术网

Bash 在shell脚本中生成用户输入循环变量

Bash 在shell脚本中生成用户输入循环变量,bash,shell,Bash,Shell,我有以下脚本,其运行方式如下: >> bash test.sh < 0 0 但我得到了以下错误: test.sh: line 14: [: {sni..snf}: integer expression expected test.sh: line 19: [: {sni..snf}: integer expression expected 如何生成循环变量整数?谢谢。你可以用这样的东西 for (( i=$sni ; i<=$snf ; i++ )) 还可以将变量作

我有以下脚本,其运行方式如下:

>> bash test.sh < 0 0
但我得到了以下错误:

test.sh: line 14: [: {sni..snf}: integer expression expected
test.sh: line 19: [: {sni..snf}: integer expression expected

如何生成循环变量整数?谢谢。

你可以用这样的东西

for (( i=$sni ; i<=$snf ; i++ ))
还可以将变量作为命令参数传递给shell脚本

bash test.sh 1 2

变量将包含在变量
$1
$2

中,您可以使用类似的内容

for (( i=$sni ; i<=$snf ; i++ ))
#!/bin/sh
if [[ ! $2 ]]
then
  echo test.sh SNI SNF
  exit
fi

echo "Run between $1 and $2"
seq $1 $2 | while read i
do
  # do stuff
done
还可以将变量作为命令参数传递给shell脚本

bash test.sh 1 2

变量将包含在变量
$1
$2

OK中,用于汇总各种输入,以防您想继续使用
的语法

#!/bin/sh
if [[ ! $2 ]]
then
  echo test.sh SNI SNF
  exit
fi

echo "Run between $1 and $2"
seq $1 $2 | while read i
do
  # do stuff
done
read $sni
这是间接的。您没有读入变量
${sni}
,而是读入名为
${sni}
的变量,例如:

$ foo=bar
$ read $foo <<< "quux"
$ echo "${foo}"
bar
$ echo "${bar}"
quux
相反。关于

for i in {sni..snf}
。。。这不起作用,因为这里没有将变量视为变量。 如果使用
ksh
,则可以

for i in {${sni}..${snf}}; do
    ...
done
但是
bash
不是那么聪明,在这种情况下,您需要使用它

for i in $(seq ${sni} ${snf}); do
    ...
done
所以整个事情看起来应该更像:

#!/bin/sh

read sni
read snf

echo "Run between '${sni}' and '${snf}'"
for i in $(seq ${sni} ${snf}); do
        echo "i=$i"
done
例如:

$ printf "1\n4\n" | ./t.sh
Run between '1' and '4'
i=1
i=2
i=3
i=4

好的,总结各种输入,如果您想继续使用
for
语法

read $sni
这是间接的。您没有读入变量
${sni}
,而是读入名为
${sni}
的变量,例如:

$ foo=bar
$ read $foo <<< "quux"
$ echo "${foo}"
bar
$ echo "${bar}"
quux
相反。关于

for i in {sni..snf}
。。。这不起作用,因为这里没有将变量视为变量。 如果使用
ksh
,则可以

for i in {${sni}..${snf}}; do
    ...
done
但是
bash
不是那么聪明,在这种情况下,您需要使用它

for i in $(seq ${sni} ${snf}); do
    ...
done
所以整个事情看起来应该更像:

#!/bin/sh

read sni
read snf

echo "Run between '${sni}' and '${snf}'"
for i in $(seq ${sni} ${snf}); do
        echo "i=$i"
done
例如:

$ printf "1\n4\n" | ./t.sh
Run between '1' and '4'
i=1
i=2
i=3
i=4

另外,您应该使用
读取sni
,因为您不想在那里使用变量插值。@jpaugh ad 1)遗憾的是,您不能。如果你想在这里使用变量,你必须使用
seq
(无论如何都要使用
bash
)。哎哟!哇!你说得对。@jpaugh我们可以假装你建议使用
ksh
,这比
bash
更聪明,而且效果很好:-)哈哈。不幸的是,我没有那么聪明。:-)另外,您应该使用
读取sni
,因为您不想在那里使用变量插值。@jpaugh ad 1)遗憾的是,您不能。如果你想在这里使用变量,你必须使用
seq
(无论如何都要使用
bash
)。哎哟!哇!你说得对。@jpaugh我们可以假装你建议使用
ksh
,这比
bash
更聪明,而且效果很好:-)哈哈。不幸的是,我没有那么聪明。:-)