LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-30-2008, 01:50 PM   #1
coyttl
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Rep: Reputation: 0
Bash scripting/command - get the device currently on?


Odd question for everyone.

Doing a script in Bash, and would like to find out if there's a way (easy preferred, but not required!) to do this:
No matter what directory I'm in (/usr/john/yadda/yadda2/), I'd like to get the device that that directory is contained in.

For example, if my mount points are:
Code:
/dev/sda1     /
/dev/sda2     /usr
/dev/sdb      /mnt/cdrom
So if I ask for /etc, I get back /dev/sda1. Ask for /usr/john I get /dev/sda2. Ask for /mnt/cdrom/images/gold, I get /dev/sdb.

I know I can recursively compare directories until I find a match, but not 100% how to tackle that one, and before I started was wondering if there was another way.

Thanks in advance, all!
Mike.
 
Old 12-30-2008, 02:37 PM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
EDIT:- Sorry, misread the request!

Code:
df -k .|awk -F”[	 ]*” ‘!(/Mounted on/) {print $1}’

Last edited by Disillusionist; 12-30-2008 at 02:53 PM.
 
Old 12-30-2008, 02:42 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Hi,

I came up with this 'one-line', maybe not to elegant, but it seems to do what you asked:

df | awk '/^\// { print $6, $1 }' | sort | while read MPOINT DEVICE; do echo "`pwd`" | grep "$MPOINT" 1>/dev/null 2>&1 && echo "$DEVICE" ; done | tail -1

Here's the scripted format:
Code:
#!/bin/bash
df | \
awk '/^\// { print $6, $1 }' | \
sort | \
while read MPOINT DEVICE
do
  echo "`pwd`" | grep "$MPOINT" 1>/dev/null 2>&1 && echo "$DEVICE"
done | \
tail -1
Up to you to figure out how it works. Don't hesitate to ask if something is unclear.

Anyway, hopes this helps.
 
Old 12-30-2008, 02:44 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Or more simply
Code:
df /etc | awk 'END{print $1}'
 
Old 12-30-2008, 02:46 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Hi again,

I forgot all about df's file option.....

Expanding on the example given by Disillusionist: df . | awk '/^\// { print $1 }'
That's a lot better then my original reply!
 
Old 12-30-2008, 02:49 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Maybe I am a little drunk (just kiddin') but does the OP asked for the device name? In the df output it is the first field, not the sixth.

Last edited by colucix; 12-30-2008 at 02:50 PM.
 
Old 12-30-2008, 03:25 PM   #7
coyttl
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Original Poster
Rep: Reputation: 0
All - THANKS!

The last df command does exactly what I want. (The other examples probably do as well, but that one I tried. ) I knew of the DF command - just never knew it would accept parameters. (And because of that, I never thought to scan the man page!)

Thanks all, much appreciated.
Mike.
 
Old 12-30-2008, 03:44 PM   #8
coyttl
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Okay, I DO have a question. The command:
Code:
awk '/^\// { print $1 }'
I've never been strong with AWK - can someone explain the syntax there?

Thanks,
Mike.
 
Old 12-30-2008, 03:57 PM   #9
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
It means "for all the lines that match the regular expression, print the first (blank separated) field". The regular expression enclosed in slashes
Code:
/^\//
matches a single slash at the beginning of the line. The ^ special character means the beginning of the line; the \/ is an escaped slash. Need to escape otherwise it is interpreted as the closure of the embedding slashes (highlighted in red).

So, the output of df is something like
Code:
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda8             59795520  44720072  12037960  79% /
and the regexp matches only the second line, which begins with a slash. If you are interested in learning awk, I can suggest a must-read: the official GNU awk manual "Gawk: Effective AWK Programming".
 
Old 12-30-2008, 04:24 PM   #10
coyttl
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
It means "for all the lines that match the regular expression, print the first (blank separated) field". The regular expression enclosed in slashes
Code:
/^\//
matches a single slash at the beginning of the line. The ^ special character means the beginning of the line; the \/ is an escaped slash. Need to escape otherwise it is interpreted as the closure of the embedding slashes (highlighted in red).

So, the output of df is something like
Code:
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda8             59795520  44720072  12037960  79% /
and the regexp matches only the second line, which begins with a slash. If you are interested in learning awk, I can suggest a must-read: the official GNU awk manual "Gawk: Effective AWK Programming".

Ah! Okay, thanks. I figured it was something like a REGEX, didn't know about the 'surrounding-it-with-/s' tho.

Thanks.
Mike.
 
  


Reply

Tags
bash, device, mount


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Screen Command with Bash Scripting inthecrossfire Linux - Software 5 07-15-2008 01:18 PM
shell scripting/ bash/ a command with 3 parameters mayaabboud Programming 4 01-05-2008 10:22 AM
Bash scripting: redirecting input to a command hal8000b Programming 2 12-10-2007 07:29 AM
Wrong parsing of ls command in bash scripting itz2000 Programming 3 04-25-2007 10:23 AM
Bash command scripting otisthegbs Linux - General 6 07-06-2004 12:06 AM


All times are GMT -5. The time now is 08:07 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration