Major = $ (awk "// $2 =/" $ module/"{print/$1}"/proc/devices) Correct Understanding.

Source: Internet
Author: User
Tags parse error

Major = $ (awk "// $2 =/" $ module/"{print/$1}"/proc/devices)

Correct understanding

Lead:
This statement comes from the script code for automatically creating a device file in the device section of o'reilly Linux Device Driver 3. This statement is used in all the ebook of o'reilly, the third edition of Linux Device Driver, on the Internet. At that time, this statement was put into the script, and there was an error in running the statement. It was wrong in the place where the double slashes were located.
Later I bought a genuine paper book to read it. I found that all the code in the book and the supporting source code here use a single slash, that is, there is only one backslash in $.
Major = $ (awk "/$2 =/" $ module/"{print/$1}"/proc/devices)


It runs correctly on my Linux. I am using the 2.6 kernel of Fedora 9 and the 2.4 kernel of Red Hat 9.

When beginners started reading ldd3, they were confused about the script for dynamically creating device files. After my actual operations are verified repeatedly, the main problems of this statement are explained as follows:


1. In this statement, awk uses the Play Number "" instead of the single quotation mark''

 

According to the awk rules and syntax, the entire awk statement is composed of two parts: the mode operation.
Pattern {action} # perform the operation if the pattern matches
Pattern # print if the pattern matches
{Action} # perform operations on each record
Similarly, an awk statement can be simplified
A-> B # B if
In this case, awk is no exception to any script. It uses delimiters such as:/(awk Regular Expression delimiters) and "" (string delimiters), which ultimately let the parsing program understand.
In bash or shell scripts, "" can be replaced with variables in content caused by double quotation marks, while content caused by single quotation marks with ''cannot be replaced with variables with special symbols. Therefore, you can choose whether to use single quotation marks or double quotation marks to define the content.


2. Multiple Use/backslash in this statement

 

Currently, we know that all scripts (Bash and shell) are escaped using/backslash (Escape in SQL) to ensure that special characters are considered as common characters.
Combined with statements and syntax, awk will eventually receive $1, $2,
Let's take a simple example. Let's make a script to print the device number of the misc device. There is no need for such a complex condition mode. A simple example is used to describe these arguments.
Print the device number named MISC in the/proc/devices file.
[Root @ localhost feiyinziiu] # Cat/proc/devices <br/> character devices: <br/> 1 mem <br/> 2 Pty <br/> 3 ttyp <br/> 4 TTYs <br/> 5 CUA <br/> 6 LP <br/> 7 VCs <br/> 10 MISC <br/> 13 input <br/> 29 FB <br/> 36 Netlink <br/> 128 PTM <br/> 129 PTM <br /> 130 PTM <br/> 131 PTM


This is part of my Linux devices file. Divide by awk field. Here, 1, 2, 3 .... It should be $1, and the following name should be $2. This is the result of typing commands in my shell.

[Root @ feiyinziiu fbin] # awk "/$2 =/" Misc/"{print/$1}"/proc/devices <br/> 10 <br/> [root @ feiyinziiu fbin] # awk '$2 = "Misc" {print $1}'/proc/devices <br/> 10

This is the correct result. Imagine if we do not use a backslash for $.
#! /Bin/sh <br/> Sf = $ (awk "$2 =/" Misc/"{print $1}"/proc/devices) <br/> echo $ SF
Run the following command:
[Root @ localhost feiyinziiu] #. /autoawk <br/> awk: cmd. line: 1 :== "Misc" {print} <br/> awk: cmd. line: 1: ^ Parse error </P> <p> [root @ localhost feiyinziiu] #

An error is reported here. Look at this awk: cmd. line: 1: = "Misc" {print}. Why is it empty before =, and empty after print? This should not happen, because what we use clearly is $2 and $1. In any case, it will not be empty.
Don't worry. Let's modify the script.

#! /Bin/sh <br/> Sf = $ (awk "$2 =/" Misc/"{print $0}"/proc/devices) <br/> echo $ SF

We only changed $1 to $0.

Run the following command:

[Root @ localhost feiyinziiu] #. /autoawk <br/> awk: cmd. line: 1: = "Misc" {print. /autoawk} <br/> awk: cmd. line: 1: ^ Parse error <br/> awk: cmd. line: 1: = "Misc" {print. /autoawk} <br/> awk: cmd. line: 1: ^ Parse error <br/> awk: cmd. line: 1: = "Misc" {print. /autoawk} <br/> awk: cmd. line: 1: ^ unterminated Regexp </P> <p> [root @ localhost feiyinziiu] #
Now let's take a look at the error.
Awk: cmd. Line: 1 :== "Misc" {print./autoawk}
./Autoawk appears after print
This is what we entered in the bash command line.

Now I think you should have thought about it. For Bash and shell scripts, $0, $1... And so on.
For example:
#./Autoawk a B c d
Then, $0 =./autoawk, $1 = A, $2 = B, $3 = C. This begins to understand that if we do not add a backslash, $0, $1, $2 will be shell. bash is used as its parameter. When the script runs, $0, $1, $2 will be replaced with the command line parameter. We wanted to receive the awk, but now it was replaced by Shell and bash. To prevent replacement of $1 and $2, we must add a backslash before $ to disable replacement, that is,/$1. Another way to disable such replacement is to use single quotation marks (''). The content in the single quotation marks ('') in the script is processed as is without any changes.
Both single quotes and double quotes can print the same result. The only difference is that double quotation marks need to be escaped from special characters, but single quotation marks are not required.
Awk "/$2 =" $ module "{print/$1}"/proc/devices # Here, the quotation marks of $ module are not particularly useful, because they are only strings referenced.
Awk '$2 = "$ module" {print $1}'/proc/devices
The two methods achieve the same effect.


3. $ Used a double backslash // $


[Root @ feiyinziiu fbin] # awk "/$2 =/" Misc/"{print/$1}"/proc/devices
10
[Root @ feiyinziiu fbin] # awk '$2 = "Misc" {print $1}'/proc/devices
10
The demos here are all directly typed in the command line. Shell or bash. The script file is finally read into the command interpreter. Here, the backslash is nothing more than retaining special characters. According to the normal situation,/$1 has already achieved the goal of avoiding the replacement of $1. For here // $1, I think this is due to a version issue or other reasons, and this type of double slash will prompt an error during running, so I 'd like to refer to this article as a SLR slash. As to why we used double backslash at the time, we only need to ask Wei Yongming.
You can refer to the source code of the linuxlinuxdevice driver in the driver source code ftp://ftp.ora.com/pub/examples/linux/drivers/. At this time I wrote this place, I again logged on to this server, found that there is still only the second version of the source code dd2-samples, there is no third version of the source code. There is a scull_load script under scull in ldd2-samples. The code for automatically creating a device node is as follows:
#! /Bin/sh <br/> module = "scull" <br/> device = "scull" <br/> mode = "664" <br/> # group: since distributions do it differently, look for wheel or use staff <br/> If grep '^ staff:'/etc/group>/dev/NULL; then <br/> group = "staff" <br/> else <br/> group = "Wheel" <br/> fi <br/> # invoke insmod with all arguments we got <br/> # And use a pathname, as newer modutils don't look in. by default <br/>/sbin/insmod-f. /$ module. o $ * | Exit 1 <br/> major = 'cat/proc/devices | awk "// $2 ==/" $ module/"{print/$1} "'<br/> # Remove stale nodes and replace them, then give GID and perms <br/> # usually the script is shorter, it's scull that has several devices in it. <br/> RM-F/dev/$ {Device} [0-3] <br/> mknod/dev/$ {Device} 0 C $ major 0 <br/> mknod/dev/$ {Device} 1 C $ major 1 <br/> mknod/dev/$ {Device} 2 C $ major 2 <br/> mknod/dev/$ {device} 3 C $ major 3 <br/> ln-SF $ {Device} 0/dev/$ {Device} <br/> chgrp $ group/dev/$ {Device} [0-3] <br/> chmod $ mode/dev/$ {Device} [0-3] <br/>

At the same time, the source code of my paper book is the following code:

#! /Bin/sh <br/> # $ ID: scull_load, V 1.4 2004/11/03 06:19:49 Rubini exp $ <br/> module = "scull" <br/> device = "scull" <br/> mode = "664" <br/> # Group: since distributions do it differently, look for wheel or use staff <br/> If grep-Q '^ staff:'/etc/group; then <br/> group = "staff" <br/> else <br/> group = "Wheel" <br/> fi <br/> # invoke insmod with all arguments we got <br/> # And use a pathname, as insmod doesn't look in. by default <br/>/sbin/insmod. /$ module. ko $ * | Exit 1 <br/> # retrieve major number <br/> major =$ (awk "/$2 =/" $ module/"{print/$ 1} "/proc/devices) <br/> # Remove stale nodes and replace them, then give GID and perms <br/> # usually the script is shorter, it's scull that has several devices in it. <br/> RM-F/dev/$ {Device} [0-3] <br/> mknod/dev/$ {Device} 0 C $ major 0 <br/> mknod/dev/$ {Device} 1 C $ major 1 <br/> mknod/dev/$ {Device} 2 C $ major 2 <br/> mknod/dev/$ {device} 3 C $ major 3 <br/> ln-SF $ {Device} 0/dev/$ {Device} <br/> chgrp $ group/dev/$ {Device} [0-3] <br/> chmod $ mode/dev/$ {Device} [0-3] <br/> RM-F/dev/$ {Device} pipe [0-3] <br/> mknod/dev/$ {Device} pipe0 C $ major 4 <br/> mknod/dev/$ {Device} pipe1 C $ major 5 <br /> mknod/dev/$ {Device} pipe2 C $ major 6 <br/> mknod/dev/$ {Device} pipe3 C $ major 7 <br/> ln-SF $ {Device} pipe0/dev/$ {Device} pipe <br/> chgrp $ group/dev/$ {Device} pipe [0-3] <br/> chmod $ mode/ dev/$ {Device} pipe [0-3] <br/> RM-F/dev/$ {Device} single <br/> mknod/dev/$ {Device} single C $ major 8 <br/> chgrp $ group/dev/$ {Device} single <br/> chmod $ mode/dev/$ {Device} single <br/> RM- f/dev/$ {Device} uid <br/> mknod/dev/$ {Device} uid C $ major 9 <br/> chgrp $ group/dev/$ {Device} UID <br/> chmod $ mode/dev/$ {Device} uid <br/> RM-F/dev/$ {Device} wuid <br/> mknod/dev/$ {Device} wuid C $ Major 10 <br/> chgrp $ group/dev/$ {Device} wuid <br/> chmod $ mode/dev/$ {Device} wuid <br /> RM-F/dev/$ {Device} priv <br/> mknod/dev/$ {Device} priv C $ Major 11 <br/> chgrp $ group/dev/ $ {Device} priv <br/> chmod $ mode/dev/$ {Device} priv

The Linux Device Driver officially announced two different backslash, which should indicate that it was a revision to the second version.

 

 

 

 

 

Copyright statement:
Reprinted Article please indicate the original source http://blog.csdn.net/feiyinzilgd/archive/2010/12/30/6108417.aspx
Contact tan Haiyan or go to tan Haiyan's personal homepage to leave a message.

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.