Pass parameters to the script (1). Pass parameters to the script

Source: Internet
Author: User

Pass parameters to the script (1). Pass parameters to the script

In awk, the most confusing thing is to pass parameters to the script. The parameter assigns the value to a variable, which can be accessed in the awk script. This variable can be set on the command line and placed behind the script before the file name.

Awk 'script' var = value inputfile

Each item must be interpreted as a single parameter. Therefore, spaces are not allowed on both sides of the equal sign. You can also use this method to pass multiple parameters. For example, if you want to define variables high and low in the command line, you can use the following code to call awk:

$ Awk-f scriptfile high = 100 low = 60 datafile

In the script, these two variables can be accessed as any awk variable. If you want to write the script into the implementation of a shell script, you can pass shell command line parameters in the form of numerical values (shell provides command line parameter variables by location: $1 indicates the first parameter, $2 indicates the second parameter, and so on ).

Awk-f scriptfile "high = $1" "low = $2" datafile

If the shell script is named awket, you can call it as follows:

$ Awket 100 60

"100" corresponds to $1, and its value is assigned to the variable high. In addition, the output results of environment variables or commands can also be passed as variable values. There are two examples:

Awk '{...}' directory = $ cwd file1...
Awk '{...}' directory = 'pwd' file1...

"$ Cwd" returns the value of the variable cwd, that is, the current working path (only csh ). In the second example, run the pwd command using backquotes and assign the result to the variable directory (which is very convenient ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.