I tried this but it does not seem to work. Please help thanks
我试过这个,但似乎没有用。请帮忙谢谢
TEST_STRING= test
echo Starting awk command
awk -v testString=$TEST_STRING'
BEGIN {
}
{
print testString
}
END {}
' file2
1 个解决方案
#1
2
There are two problems here: You aren't actually assigning to TEST_STRING
, and you're passing the program code in the same argument as the variable value. Both of these are caused by whitespace and quoting being in the wrong places.
这里有两个问题:您实际上并没有分配给TEST_STRING,而是在与变量值相同的参数中传递程序代码。这两个都是由空格和引用位于错误的地方引起的。
TEST_STRING= test
...does not assign a value to TEST_STRING
. Instead, it runs the command test
, with an environment variable named TEST_STRING
set to an empty value.
...不为TEST_STRING赋值。相反,它运行命令test,将名为TEST_STRING的环境变量设置为空值。
Perhaps instead you want
也许你反而想要
TEST_STRING=test
or
要么
TEST_STRING=' test'
...if the whitespace is intentional.
......如果空白是故意的。
Second, passing a variable to awk with -v
, the right-hand side should be double-quoted, and there must be unquoted whitespace between that value and the program to be passed to awk (or other values). That is to say:
其次,使用-v将变量传递给awk,右侧应该是双引号,并且该值与要传递给awk(或其他值)的程序之间必须有不带引号的空格。也就是说:
awk -v testString=$TEST_STRING' BEGIN
...will, if TEST_STRING
contains no whitespace, pass the BEGIN
as part of the value of testString
, not as a separate argument!
...如果TEST_STRING不包含空格,则将BEGIN作为testString值的一部分传递,而不是作为单独的参数传递!
awk -v testString="$TEST_STRING" 'BEGIN
...on, the other hand, ensures that the value of TEST_STRING
is passed as part of the same argument as testString=
, even if it contains whitespace -- and ensures that the BEGIN
is passed as part of a separate argument.
另一方面,... on确保TEST_STRING的值作为testString =的相同参数的一部分传递,即使它包含空格 - 并确保BEGIN作为单独参数的一部分传递。
#1
2
There are two problems here: You aren't actually assigning to TEST_STRING
, and you're passing the program code in the same argument as the variable value. Both of these are caused by whitespace and quoting being in the wrong places.
这里有两个问题:您实际上并没有分配给TEST_STRING,而是在与变量值相同的参数中传递程序代码。这两个都是由空格和引用位于错误的地方引起的。
TEST_STRING= test
...does not assign a value to TEST_STRING
. Instead, it runs the command test
, with an environment variable named TEST_STRING
set to an empty value.
...不为TEST_STRING赋值。相反,它运行命令test,将名为TEST_STRING的环境变量设置为空值。
Perhaps instead you want
也许你反而想要
TEST_STRING=test
or
要么
TEST_STRING=' test'
...if the whitespace is intentional.
......如果空白是故意的。
Second, passing a variable to awk with -v
, the right-hand side should be double-quoted, and there must be unquoted whitespace between that value and the program to be passed to awk (or other values). That is to say:
其次,使用-v将变量传递给awk,右侧应该是双引号,并且该值与要传递给awk(或其他值)的程序之间必须有不带引号的空格。也就是说:
awk -v testString=$TEST_STRING' BEGIN
...will, if TEST_STRING
contains no whitespace, pass the BEGIN
as part of the value of testString
, not as a separate argument!
...如果TEST_STRING不包含空格,则将BEGIN作为testString值的一部分传递,而不是作为单独的参数传递!
awk -v testString="$TEST_STRING" 'BEGIN
...on, the other hand, ensures that the value of TEST_STRING
is passed as part of the same argument as testString=
, even if it contains whitespace -- and ensures that the BEGIN
is passed as part of a separate argument.
另一方面,... on确保TEST_STRING的值作为testString =的相同参数的一部分传递,即使它包含空格 - 并确保BEGIN作为单独参数的一部分传递。