Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of this shell script line with awk

Tags:

bash

shell

awk

I read this line of script in book [linux device drivers]. What does it do?

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

as in context:

#!/bin/sh
module="scull"   
device="scull"  
mode="664"

# invoke insmod with all arguments we got  
# and use a pathname, as newer modutils don't look in . by default

/sbin/insmod ./$module.ko $* || exit 1


# remove stale nodes  
rm -f /dev/${device}[0-3]   

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

mknod /dev/${device}0 c $major 0
....
like image 261
user1559625 Avatar asked Dec 19 '12 11:12

user1559625


1 Answers

A better way to write this would be :

major=$(awk -v mod=$module '$2==mod{print $1}' /proc/devices)
like image 130
Guru Avatar answered Oct 22 '22 08:10

Guru