I start programming in c language, and I ask myself this question.
我开始用c语言编程,我问自己这个问题。
I have a file lol.c :
我有一个文件lol.c:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
printf ("argc\t= %d\n", argc);
for (int i=0; i < argc; i++)
printf ("argv[%i]\t= %s\n", i, argv[i]);
return 0;
}
So when I execute the file (after compilation) :
所以当我执行文件时(编译后):
./lol "abc" "def"
He returns so :
他回来了:
argc = 3
argv[0] = ./lol
argv[1] = abc
argv[2] = def
Now, I have a bash script lol.sh :
现在,我有一个bash脚本lol.sh:
#!/bin/bash
./lol "abc"
When I execute the file :
当我执行文件时:
bash lol.sh def
So we get :
所以我们得到:
argc = 2
argv[0] = ./lol
argv[1] = abc
How to retrieve the positional parameters passed to the bash script (from the file lol.c) ?
如何检索传递给bash脚本的位置参数(来自文件lol.c)?
My first solution is to pass the positional parameters to the lol command like this (lol.sh) :
我的第一个解决方案是将位置参数传递给lol命令,如下所示(lol.sh):
#!/bin/bash
./lol "abc" "$*"
He returns so :
他回来了:
argc = 3
argv[0] = ./lol
argv[1] = abc
argv[2] = def
I am not very satisfied with this solution, is there another way for that ?
我对这个解决方案不是很满意,还有另外一种方法吗?
Thanks
1 个解决方案
#1
0
While "$*"
works, it mashes all the arguments into one, with a space separating the original arguments. You could see this by running:
虽然“$ *”有效,但它将所有参数合并为一个,并用原始参数分隔空格。你可以通过运行看到这个:
bash lol.sh def ghi jkl
which would produce:
会产生:
argc = 3
argv[0] = ./lol
argv[1] = abc
argv[2] = def ghi jkl
and running bash lol.sh
would produce:
并运行bash lol.sh将产生:
argc = 3
argv[0] = ./lol
argv[1] = abc
argv[2] =
Using "$*"
is seldom the correct notation; you should normally use "$@"
which relays the arguments verbatim as separate arguments, and handles 'no arguments' correctly (whereas "$*"
creates an empty string as an argument).
使用“$ *”很少是正确的表示法;你通常应该使用“$ @”将参数逐字传递为单独的参数,并正确处理“无参数”(而“$ *”创建一个空字符串作为参数)。
This command should do what you want:
这个命令应该做你想要的:
./lol "abc" "$@"
Then the output from bash lol.sh def ghi jkl
would be:
那么bash lol.sh def ghi jkl的输出将是:
argc = 5
argv[0] = ./lol
argv[1] = abc
argv[2] = def
argv[3] = ghi
argv[4] = jkl
and the output from bash lol.sh
would be:
并且bash lol.sh的输出将是:
argc = 2
argv[0] = ./lol
argv[1] = abc
See also How to iterate over arguments in a bash script?)
另请参见如何在bash脚本中迭代参数?)
#1
0
While "$*"
works, it mashes all the arguments into one, with a space separating the original arguments. You could see this by running:
虽然“$ *”有效,但它将所有参数合并为一个,并用原始参数分隔空格。你可以通过运行看到这个:
bash lol.sh def ghi jkl
which would produce:
会产生:
argc = 3
argv[0] = ./lol
argv[1] = abc
argv[2] = def ghi jkl
and running bash lol.sh
would produce:
并运行bash lol.sh将产生:
argc = 3
argv[0] = ./lol
argv[1] = abc
argv[2] =
Using "$*"
is seldom the correct notation; you should normally use "$@"
which relays the arguments verbatim as separate arguments, and handles 'no arguments' correctly (whereas "$*"
creates an empty string as an argument).
使用“$ *”很少是正确的表示法;你通常应该使用“$ @”将参数逐字传递为单独的参数,并正确处理“无参数”(而“$ *”创建一个空字符串作为参数)。
This command should do what you want:
这个命令应该做你想要的:
./lol "abc" "$@"
Then the output from bash lol.sh def ghi jkl
would be:
那么bash lol.sh def ghi jkl的输出将是:
argc = 5
argv[0] = ./lol
argv[1] = abc
argv[2] = def
argv[3] = ghi
argv[4] = jkl
and the output from bash lol.sh
would be:
并且bash lol.sh的输出将是:
argc = 2
argv[0] = ./lol
argv[1] = abc
See also How to iterate over arguments in a bash script?)
另请参见如何在bash脚本中迭代参数?)