LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-10-2012, 01:01 AM   #1
Fracker
Member
 
Registered: Mar 2009
Posts: 90

Rep: Reputation: 0
Get Partition list one Liner Command


Hi guyz,

I have this command which normally serves my purpose, but I am sure there is better way to do it..

Code:
cat /proc/mounts | grep -v none | grep -v rootfs | grep -v proc | grep -v sys | grep -v /dev/root | awk '{print $2}' | grep -v /dev | grep -v nfs > tmpfil && while read i; do echo "Partition = $i"; done < tmpfil && rm -f tmpfil
I am doing some operation on all partitions (other then root and default ones).. here is what I cooked up, but don't look nice. Any help in doing this in more professional manner!

Last edited by Fracker; 07-10-2012 at 03:37 AM. Reason: Change Quote to Code
 
Old 07-10-2012, 01:41 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
First of all, please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


You have a Useless Use Of Cat and Grep, as well as unnecessary multiple instances of grep, even if you did need it (check out the -e option). The temp file and loop aren't necessary either.

A single awk command (it's a full text processing language that can completely replace grep and sed) with proper regex matching and formatted print statement can do it all. This command exactly replicates the above:

Code:
awk '( $0 !~ /none|rootfs|proc|sys|\/dev\/root/ && $2 !~ /\/dev|nfs/ ) { print "Partition = " $2 }' /proc/mounts
There may be cleaner ways to filter out the lines you want. All I did was re-write your posted code for awk.

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/

Here are a few regular expressions tutorials:
http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html
http://www.regular-expressions.info/

Last edited by David the H.; 07-10-2012 at 01:43 AM.
 
2 members found this post helpful.
Old 07-10-2012, 03:39 AM   #3
Fracker
Member
 
Registered: Mar 2009
Posts: 90

Original Poster
Rep: Reputation: 0
Thanks, yeah that works great for text parsing. and also remove grep and cat mess.

I need loop or atleast a shell where I can play with normal shell commands. Awk don't have many commands. That's why I use while loop for this task.
 
Old 07-10-2012, 03:51 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,018

Rep: Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199
Quote:
I need loop or atleast a shell where I can play with normal shell commands. Awk don't have many commands. That's why I use while loop for this task.
Please try and explain further so we might assist? Awk is a very full featured command / language and I would be surprised if what you need to accomplish cannot be handled.
 
Old 07-10-2012, 04:08 AM   #5
Fracker
Member
 
Registered: Mar 2009
Posts: 90

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Please try and explain further so we might assist? Awk is a very full featured command / language and I would be surprised if what you need to accomplish cannot be handled.
I need to assign single value to a variable and after that call a function, which will do some tasks based on this variable.

say

/tmp ---> xyz

func_somethingtodowithpartition

/var ---> xyz

func_somethingtodowithpartition

.
.
.
.

untill all partitions are done
 
Old 07-10-2012, 06:46 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,251

Rep: Reputation: 4158Reputation: 4158Reputation: 4158Reputation: 4158Reputation: 4158Reputation: 4158Reputation: 4158Reputation: 4158Reputation: 4158Reputation: 4158Reputation: 4158
I'll let you debate that one with the esteemed doyens above.

As an aside, what does any of this to do (directly) with partitions ... ?
 
Old 07-10-2012, 08:25 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
If you want to run arbitrary commands on the files, then you can still feed the raw output into a loop. For maximum safety regarding possible unusual filename characters, use a null separator between them ( use printf and "\0" in awk ).

How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
http://mywiki.wooledge.org/BashFAQ/001
How can I find and deal with file names containing newlines, spaces or both?
http://mywiki.wooledge.org/BashFAQ/020

Assuming bash, it's common to use a process substitution for this, instead of a pipe.
Code:
while IFS='' read -r -d '' mnt ; do

	echo "Partition = $mnt"
	mountlist+=( "$mnt" )

done < <( awk '( $0 !~ /none|rootfs|proc|sys|\/dev\/root/ && $2 !~ /\/dev|nfs/ ) { printf "%s\0", $2 }' /proc/mounts )
The second example command above adds the names to an array, so that they remain available for later in the script, if needed.

Last edited by David the H.; 07-10-2012 at 08:27 AM. Reason: small changes
 
2 members found this post helpful.
Old 07-10-2012, 09:48 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,018

Rep: Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199
The other alternative is to call the action required within awk and use it to process the partitions.
Code:
awk '( !/none|rootfs|proc|sys|\/dev\/root/ && $2 !~ /\/dev|nfs/ ) { print | "ls " $2 }' /proc/mounts
Obviously you can change ls for your action.
 
1 members found this post helpful.
Old 07-10-2012, 11:09 PM   #9
Fracker
Member
 
Registered: Mar 2009
Posts: 90

Original Poster
Rep: Reputation: 0
Thanks guyz for the great help, now I can compact the code as per requirements.
 
  


Reply


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
[SOLVED] Partial list with ls-l in bash script run in cron but full list run from command line redgshost Linux - General 29 01-16-2011 12:14 PM
[SOLVED] awk nested if, command liner patolfo Programming 37 05-14-2010 12:19 AM
Is there a single command to list all hardware installed (command line)? davee Linux - Hardware 6 02-28-2009 07:19 PM
Need one liner copy command madhi Linux - Software 2 07-31-2008 01:32 AM
Command to run another command against a list of files psweetma Linux - General 3 11-09-2005 05:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:23 AM.

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