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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-10-2012, 01:01 AM
|
#1
|
Member
Registered: Mar 2009
Posts: 90
Rep:
|
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
|
|
|
07-10-2012, 03:39 AM
|
#3
|
Member
Registered: Mar 2009
Posts: 90
Original Poster
Rep:
|
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.
|
|
|
07-10-2012, 03:51 AM
|
#4
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,018
|
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.
|
|
|
07-10-2012, 04:08 AM
|
#5
|
Member
Registered: Mar 2009
Posts: 90
Original Poster
Rep:
|
Quote:
Originally Posted by grail
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
|
|
|
07-10-2012, 06:46 AM
|
#6
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,251
|
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 ... ?
|
|
|
07-10-2012, 08:25 AM
|
#7
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
|
07-10-2012, 09:48 AM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,018
|
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.
|
07-10-2012, 11:09 PM
|
#9
|
Member
Registered: Mar 2009
Posts: 90
Original Poster
Rep:
|
Thanks guyz for the great help, now I can compact the code as per requirements.
|
|
|
All times are GMT -5. The time now is 01:23 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|