[英]how to use awk's $0 variables in the shell file
1.my shell file is 1.我的shell文件是
[root@node3 script]# cat hi.sh
#!/bin/bash
strings="sdsf sdsda sdsadx"
echo `awk "{print substr($0,0,9)}"<<<$strings`
2.exe my shell file 2.exe我的shell文件
[root@node3 script]# sh hi.sh
awk: {print substr(hi.sh,0,9)}
awk: ^ syntax error
awk: 致命错误: 0 是 substr 的无效参数个数
so how to use the awk's $0 in shell file ? 那么如何在外壳文件中使用awk的$ 0呢?
the $0 default file's name . $ 0默认文件的名称。
another ques. 另一个问题。 i want use the var $0 about awk but the another $variable in the shell file.what should i do ?
我想使用有关awk的$ 0变量,但要在shell文件中使用另一个$变量。我该怎么办?
$ cat hi.sh
#!/bin/bash
strings="sdsf sdsda sdsadx"
num=9
echo \`awk '{print substr($0,0,$num)}' <<< $strings\`
The problem with your script is use of double quotes in awk. 脚本的问题是在awk中使用了双引号。 Replace them with single quotes.
用单引号替换它们。
When you use double quotes, the $0
inside it is treated as first argument to the script, which in this case is script name hi.sh
. 当使用双引号时,其中的
$0
将被视为脚本的第一个参数,在本例中为脚本名称hi.sh
If you use single quotes, you can use $0
as awk
argument to display whole row. 如果使用单引号,则可以将
$0
用作awk
参数来显示整行。
Also echo
is not needed here, you can just use awk '{print substr($0,0,9)}' <<< $strings
同样,这里不需要
echo
,您可以使用awk '{print substr($0,0,9)}' <<< $strings
$ cat hi.sh
#!/bin/bash
strings="sdsf sdsda sdsadx"
#echo `awk '{print substr($0,0,9)}' <<< $strings` --echo not needed
awk '{print substr($0,0,9)}' <<< $strings
$
$ sh hi.sh
sdsf sdsd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.