Reputation: 75
1.my shell file is
[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
[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 ?
the $0 default file's name .
another ques. i want use the var $0 about awk but the another $variable in the shell file.what should i do ?
$ cat hi.sh
#!/bin/bash
strings="sdsf sdsda sdsadx"
num=9
echo \`awk '{print substr($0,0,$num)}' <<< $strings\`
Upvotes: 3
Views: 2327
Reputation: 8093
The problem with your script is use of double quotes in 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
. If you use single quotes, you can use $0
as awk
argument to display whole row.
Also echo
is not needed here, you can just use 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
Upvotes: 2