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

Shell commands used in awk-a brief note

Dec 15, 2016 am 10:45 AM

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!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24