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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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