阅读背景:

如果有超过9个参数,如何访问函数的参数?

来源:互联网 

With first 9 arguments being referred from

With first 9 arguments being referred from $1-$9, $10 gets interpreted as $1 followed by a 0. How do I account for this and access arguments to functions greater than 10?

前9个参数从$ 1- $ 9引用,$ 10被解释为$ 1后跟0.我如何解释这个并访问大于10的函数的参数?

Thanks.

谢谢。

3 个解决方案

#1


26  

Use :

使用 :

#!/bin/bash
echo ${10}

To test the difference with $10, code in foo.sh :

要测试10美元的差异,foo.sh中的代码:

#!/bin/bash
echo $10
echo ${10}

Then :

然后 :

$ ./foo.sh first 2 3 4 5 6 7 8 9 10
first0
10

the same thing is true if you have :

如果您有以下情况,情况也是如此:

foobar=42
foo=FOO
echo $foobar # echoes 42
echo ${foo}bar # echoes FOObar

Use {} when you want to remove ambiguities ...

当您想要消除含糊不清时使用{}

my2c

MY2C

#2


4  

If you are using bash, then you can use ${10}.

如果您使用的是bash,则可以使用$ {10}。

${...} syntax seems to be POSIX-compliant in this particular case, but it might be preferable to use the command shift like that :

在这种特殊情况下,$ {...}语法似乎与POSIX兼容,但最好使用命令shift,如下所示:

while [ "$*" != "" ]; do
  echo "Arg: $1"
  shift
done

EDIT: I noticed I didn't explain what shift does. It just shift the arguments of the script (or function). Example:

编辑:我注意到我没有解释什么转变。它只是移动脚本(或函数)的参数。例:

> cat script.sh
echo "$1"
shift
echo "$1"

> ./script.sh "first arg" "second arg"
first arg
second arg

In case it can help, here is an example with getopt/shift :

如果它可以提供帮助,这里有一个getopt / shift示例:

while getopts a:bc OPT; do
 case "$OPT" in
  'a')
   ADD=1
   ADD_OPT="$OPTARG"
   ;;
  'b')
   BULK=1
   ;;
  'c')
   CHECK=1
   ;;
 esac
done
shift $( expr $OPTIND - 1 )
FILE="$1"

#3


2  

In general, to be safe that the whole of a given string is used for the variable name when Bash is interpreting the code, you need to enclose it in braces: ${10}

通常,为了确保在Bash解释代码时将整个给定字符串用于变量名称,您需要将其括在大括号中:$ {10}


-, gets interpreted as

With first 9 arguments being referred from $1-$9, $10 gets interpreted as $1 followed by a 0. How do I account for this and access arguments to functions greater than 10?

前9个参数从$ 1- $ 9引用,$ 10被解释为$ 1后跟0.我如何解释这个并访问大于10的函数的参数?

Thanks.

谢谢。

3 个解决方案

#1


26  

Use :

使用 :

#!/bin/bash
echo ${10}

To test the difference with $10, code in foo.sh :

要测试10美元的差异,foo.sh中的代码:

#!/bin/bash
echo $10
echo ${10}

Then :

然后 :

$ ./foo.sh first 2 3 4 5 6 7 8 9 10
first0
10

the same thing is true if you have :

如果您有以下情况,情况也是如此:

foobar=42
foo=FOO
echo $foobar # echoes 42
echo ${foo}bar # echoes FOObar

Use {} when you want to remove ambiguities ...

当您想要消除含糊不清时使用{}

my2c

MY2C

#2


4  

If you are using bash, then you can use ${10}.

如果您使用的是bash,则可以使用$ {10}。

${...} syntax seems to be POSIX-compliant in this particular case, but it might be preferable to use the command shift like that :

在这种特殊情况下,$ {...}语法似乎与POSIX兼容,但最好使用命令shift,如下所示:

while [ "$*" != "" ]; do
  echo "Arg: $1"
  shift
done

EDIT: I noticed I didn't explain what shift does. It just shift the arguments of the script (or function). Example:

编辑:我注意到我没有解释什么转变。它只是移动脚本(或函数)的参数。例:

> cat script.sh
echo "$1"
shift
echo "$1"

> ./script.sh "first arg" "second arg"
first arg
second arg

In case it can help, here is an example with getopt/shift :

如果它可以提供帮助,这里有一个getopt / shift示例:

while getopts a:bc OPT; do
 case "$OPT" in
  'a')
   ADD=1
   ADD_OPT="$OPTARG"
   ;;
  'b')
   BULK=1
   ;;
  'c')
   CHECK=1
   ;;
 esac
done
shift $( expr $OPTIND - 1 )
FILE="$1"

#3


2  

In general, to be safe that the whole of a given string is used for the variable name when Bash is interpreting the code, you need to enclose it in braces: ${10}

通常,为了确保在Bash解释代码时将整个给定字符串用于变量名称,您需要将其括在大括号中:$ {10}


followed by a 0. How do I account for this and access arguments to functions greater than 10? With first 9 arguments being referred from

With first 9 arguments being referred from $1-$9, $10 gets interpreted as $1 followed by a 0. How do I account for this and access arguments to functions greater than 10?

前9个参数从$ 1- $ 9引用,$ 10被解释为$ 1后跟0.我如何解释这个并访问大于10的函数的参数?

Thanks.

谢谢。

3 个解决方案

#1


26  

Use :

使用 :

#!/bin/bash
echo ${10}

To test the difference with $10, code in foo.sh :

要测试10美元的差异,foo.sh中的代码:

#!/bin/bash
echo $10
echo ${10}

Then :

然后 :

$ ./foo.sh first 2 3 4 5 6 7 8 9 10
first0
10

the same thing is true if you have :

如果您有以下情况,情况也是如此:

foobar=42
foo=FOO
echo $foobar # echoes 42
echo ${foo}bar # echoes FOObar

Use {} when you want to remove ambiguities ...

当您想要消除含糊不清时使用{}

my2c

MY2C

#2


4  

If you are using bash, then you can use ${10}.

如果您使用的是bash,则可以使用$ {10}。

${...} syntax seems to be POSIX-compliant in this particular case, but it might be preferable to use the command shift like that :

在这种特殊情况下,$ {...}语法似乎与POSIX兼容,但最好使用命令shift,如下所示:

while [ "$*" != "" ]; do
  echo "Arg: $1"
  shift
done

EDIT: I noticed I didn't explain what shift does. It just shift the arguments of the script (or function). Example:

编辑:我注意到我没有解释什么转变。它只是移动脚本(或函数)的参数。例:

> cat script.sh
echo "$1"
shift
echo "$1"

> ./script.sh "first arg" "second arg"
first arg
second arg

In case it can help, here is an example with getopt/shift :

如果它可以提供帮助,这里有一个getopt / shift示例:

while getopts a:bc OPT; do
 case "$OPT" in
  'a')
   ADD=1
   ADD_OPT="$OPTARG"
   ;;
  'b')
   BULK=1
   ;;
  'c')
   CHECK=1
   ;;
 esac
done
shift $( expr $OPTIND - 1 )
FILE="$1"

#3


2  

In general, to be safe that the whole of a given string is used for the variable name when Bash is interpreting the code, you need to enclose it in braces: ${10}

通常,为了确保在Bash解释代码时将整个给定字符串用于变量名称,您需要将其括在大括号中:$ {10}


-$




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: