Home > php教程 > PHP开发 > Shell commands used in awk-a brief note

Shell commands used in awk-a brief note

高洛峰
Release: 2016-12-15 10:45:56
Original
1264 people have browsed it

There are two methods for shell commands used in awk:
One. Use system()
2. Use print cmd | "/bin/bash"
http://www.gnu.org/software/gawk/manual/gawk.html#I_002fO-Functions

One. Using system()
In the awk program, we can use the system() function to call the shell command

For example: awk 'BEGIN{system("echo abc")}' file

echo abc will be used as the "command line" , executed by the shell, so we will get the following results:



root@ubuntu:~# awk 'BEGIN{system("echo abc")}'
abc
root@ubuntu:~#


root@ ubuntu:~# awk 'BEGIN{v1="echo";v2="abc";system(v1" "v2)}'
abc
root@ubuntu:~#


root@ubuntu:~# awk 'BEGIN {v1="echo";v2="abc";system(v1 v2)}'
/bin/sh: echoabc: command not found
root@ubuntu:~#


root@ubuntu:~# awk 'BEGIN {v1=echo;v2=abc;system(v1" "v2)}'
root@ubuntu:~#

From the above example, let's briefly analyze how awk calls system:
If system() brackets If the parameters inside are not enclosed in double quotes, awk thinks it is a variable, and it will replace them with constants from awk's variables, and then pass them back to the shell

If the parameters in the system() brackets are added If there are double quotes, then awk will directly pass the content in the quotes back to the shell as the shell's "command line"



2. Use print cmd | "/bin/bash"
root@ubuntu:~# awk 'BEGIN{print "echo","abc"| "/bin/bash"}'
abc
root@ubuntu:~#


root@ubuntu:~# awk 'BEGIN{print "echo","abc",";","echo","123"| "/bin/bash"}'
abc
123
root@ubuntu:~#


Three. Summary
Whether you use system() or print cmd | "/bin/bash"
awk opens a new shell and sends the corresponding cmdline parameters back to the shell, so pay attention to the current shell variables and the newly opened shell variables

1.1
root@ubuntu:~# abc=12345567890
root@ubuntu:~# awk 'BEGIN{system("echo $abc")}'

root@ubuntu:~#


1.2
root@ubuntu:~ # export abc=12345567890
root@ubuntu:~# awk 'BEGIN{system("echo $abc")}'
12345567890
root@ubuntu:~#

2.1
root@ubuntu:~# abc=1234567890
root @ubuntu:~# awk 'BEGIN{print "echo","$abc"| "/bin/bash"}'

root@ubuntu:~#


2.2
root@ubuntu:~# export abc=1234567890
root@ubuntu:~# awk 'BEGIN{print "echo","$abc"| "/bin/bash"}'
1234567890
root@ubuntu:~#


In the above example, if there is no export, those variables They only exist in the current shell variables, so they cannot be echoed.
The exported ones are all environment variables, so when awk calls a new shell, it can be echoed


More Used in awk Shell commands - a brief note. For related articles, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template