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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
