Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make awk use Shell variable under Linux

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how to let awk use Shell variables under Linux, the content is very detailed, interested friends can refer to, hope to be helpful to you.

How to make awk use Shell variable under Linux

When we write shell scripts, we usually include other Mini Program or commands in the script, such as the awk action. For awk, we need to find ways to pass some values from shell to the awk operation. So how do you get awk to use Shell variables? Next, the Linux training of Brother Company will introduce to you:

There are two possible ways for awk to use the shell variable:

1. Use Shell references

Let's use an example to demonstrate how to use a shell reference instead of a shell variable in an awk command. In this example, we want to search for a user name in the file / etc/passwd, filter and output the user's account information.

Therefore, we can write a test.sh script with the following contents:

#! / bin/bash

# read user name

Read-p "Please enter user name:" username

# search for a user name in / etc/passwd, and then output details on the screen

Cat / etc/passwd | awk "/ $username/" {print $0}'

Then, save the file and exit.

Description of the awk command in the above test.sh script:

Cat / etc/passwd | awk "/ $username/" {print $0}'

"/ $username/": this shell reference is used to replace the value of the shell variable username in the awk command. The value of username is the pattern to search for in the file / etc/passwd.

Notice that the double quotation marks are outside the awk script'{print $0}'.

Next, add executable permissions to the script and run it, as follows:

$chmod + x test.sh

$. / text.sh

After running the script, it will prompt you to enter a user name, and then you enter a valid user name and enter enter. You will see detailed user account information from the / etc/passwd file, as shown in the following figure:

Shell script to find the user name in the Password file

Shell script to find the user name in the Password file

two。 Using awk for variable assignment

Compared with the method described above, this method is more simple and better. Considering the example above, we can run a simple command to accomplish the same task. In this method, we use the-v option to assign the value of a shell variable to an awk variable.

First, create a shell variable username, and then give it a name that we want to search in the / etc/passwd file.

Username= "aaronkilik"

Then enter the following command and enter:

# cat / etc/passwd | awk-v name= "$username"'$0 ~ name {print $0}'

Use awk to find the user name in the Password file

Use awk to find the user name in the Password file

Description of the above command:

One of the-v:awk options to declare a variable

Username: is the shell variable

Name: is the awk variable

Let's take a closer look at $0 ~ name in the awk script'$0 ~ name {print $0}'. Remember, when we introduced the awk comparison operator in Section 4 of the awk series, value ~ pattern is one of the comparison operators, which means that if value matches pattern, true is returned.

The output ($0) passed to awk by the cat command matches the pattern (aaronkilik), which is the name we searched for in / etc/passwd. Finally, the comparison operation returns true. Next, a line containing the user's account information is output on the screen.

Www.lampbrother.net has introduced an important part of the awk feature that helps us use shell variables in awk commands. Many times, you will write small awk programs or commands in shell scripts, so you need a clear understanding of how to use shell variables in awk commands.

On how to let awk use Shell variables under Linux to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report