LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2015, 11:24 PM   #1
answerme
Member
 
Registered: Apr 2008
Posts: 97

Rep: Reputation: 24
Get Major number from shell script


Hi All
I have made sample test driver for linux. Now I want to do insmod & mknod using shell script.In my shell script I am able to insert the module (i.e. insmod), but not able to do mknod because I am not able to get major number value. I print the variable major but it is blank .
By doing cat /proc/devices major number exist. But in awk command I am doing something wrong which I am unable to find out. Down is the source for script .
Code:
#!/bin/sh
module="char-dev-reg"
device="char-dev-reg"
mode="664"


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

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

major=$(awk "\$2==\"$module\" {print \$1}" /proc/devices)
echo "major='$major'"          //HERE MAJOR VALUE IS BLANK
echo "module='$module'"

mknod /dev/${device} c ${major} 0
 
Old 05-19-2015, 06:23 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Not an awk expert, or even competant at it, however I know of no device named "char-dev-reg". Like for me the type of devices I make nodes for would be ttyUSB and in my /proc/devices file I have a line saying:
Code:
188 ttyUSB
so I would be searching for that exact string.

When your module is inserted, what line does show up in your /proc/devices file? Show that line and even given my lack of awk experience, someone else who does have good awk experience can probably help you with your setting of the major mode.
 
Old 05-19-2015, 06:28 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
// cannot be used as comment sign in shells, you should use # instead
 
Old 05-19-2015, 06:34 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by pan64 View Post
// cannot be used as comment sign in shells, you should use # instead
I think that's just a comment within their code block and not really in their shell. Instead just the point they're identifying where they've detected the variable is not the expected value.
 
Old 05-19-2015, 06:38 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
@answerme your awk works. I cut back the script to just "search" and changed the module to be ttyUSB which I have in my system:
Code:
#!/bin/sh
module="ttyUSB"
major=$(awk "\$2==\"$module\" {print \$1}" /proc/devices)
echo "major='$major'"
echo "module='$module'"
The result when I ran it showed:
Code:
major='188'
module='ttyUSB'
Which is a correct result.

I'd re-check the /proc/devices file after you insert that module.

Why are you doing the "rm -f /dev/${device}[0-3]"? Maybe comment out that line for now? How do you know at that point it would be a stale device?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I compare floating number in a shell script? abefroman Programming 34 10-12-2016 08:09 AM
Identify and explain the major number, minor number, and revision number in Linux... turbomen Linux - Newbie 1 11-16-2010 02:48 AM
how to find given number is integer or not in shell script kvmreddy Linux - Newbie 2 08-15-2009 08:05 AM
why there is a need for minor number and major number for monitor/keyboard in pc? tripathi Solaris / OpenSolaris 1 11-07-2003 09:36 AM
Shell Script assignment...need MAJOR help! slappydappy Programming 7 10-06-2003 02:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration