LinuxQuestions.org
Help answer threads with 0 replies.
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 03-20-2010, 08:59 AM   #1
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Rep: Reputation: 16
how to modify the output of df command


this is the output of df command on my system
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda8 18073924 4911628 12244184 29% /
udev 642140 308 641832 1% /dev
none 642140 1820 640320 1% /dev/shm
none 642140 332 641808 1% /var/run
none 642140 0 642140 0% /var/lock
none 642140 0 642140 0% /lib/init/rw
/dev/sda6 39070048 37855272 1214776 97% /media/New Volume
/dev/sda5 1959898 1330254 629644 68% /media/New Volume_
/dev/sdb1 15752188 12568456 3183732 80% /media/ANURUPDT
/dev/sr0 8128832 8128832 0 100% /media/cdrom0


i just need to get /dev/sr0 .. is that possible using grep or cut
because i tried doing that
 
Old 03-20-2010, 09:03 AM   #2
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,448
Blog Entries: 4

Rep: Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505Reputation: 1505
Hi,

You really should get some background information or references;

Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Bash Reference Manual
Advanced Bash-Scripting Guide
Linux Newbie Admin Guide
LinuxSelfHelp
Getting Started with Linux

These links and others can be found at 'Slackware-Links' .
More than just SlackwareŽ links!
 
Old 03-20-2010, 09:45 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Hi anurupr

You say you have tried grep. If you show us what you have tried we can tell you which bit is in error?
Just to confirm, you basically only want to return the row(s) containing /dev/sr0?

Apart from all the other references by onebuck, I would simply suggest man grep
 
Old 03-20-2010, 09:50 AM   #4
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Original Poster
Rep: Reputation: 16
im not getting the right output when using cut and grep commands

i used grep and cut to get a string from the df command output
this is the shell script that i used to do it but its never exiting the while loop
Code:
#!/bin/bash

c=`df |grep "/dev/sr0"|cut -f 1-2 -d' '`



 while [[ "$c" != "/dev/sr0" ]] 
 do
 c=`df |grep "/dev/sr0"|cut -f 1-2 -d' '`
 done
can anyone tell me what is wrong ? because when i execute the " df |grep "/dev/sr0"|cut -f 1-2 -d' '" i get /dev/sr0 which is the same as the string i used for comparison .. but it never becomes true
 
Old 03-20-2010, 10:05 AM   #5
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Original Poster
Rep: Reputation: 16
i dont want to return the entire row i jus need to return "/dev/sr0"
 
Old 03-20-2010, 10:12 AM   #6
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,197

Rep: Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642
this works for me:
Code:
[liveuser@localhost ~]$ while [ $c != "/dev/sr0" ] ;  do c=`df |grep "/dev/sr0"|cut -f 1-2 -d' '`; echo c = $c; sleep 1; done
[liveuser@localhost ~]$ 
[liveuser@localhost ~]$ while [ $c = "/dev/sr0" ] ;  do c=`df |grep "/dev/sr0"|cut -f 1-2 -d' '`; echo c = $c; sleep 1; done
c = /dev/sr0
c = /dev/sr0
c = /dev/sr0
c = /dev/sr0
^C
dont know why it works without double brackets and no quotes around $c ?
 
Old 03-20-2010, 10:14 AM   #7
bremarv
LQ Newbie
 
Registered: Mar 2010
Posts: 1

Rep: Reputation: 0
do you mean getting the first column, i.e. list of all your partitions? if so you might have some luck with sed.

if not, it might help to know what youre going to use it for, as I am a bit confused about what info you want to get from the df command.
 
Old 03-20-2010, 10:29 AM   #8
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Original Poster
Rep: Reputation: 16
weird
 
Old 03-20-2010, 11:47 AM   #9
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Original Poster
Rep: Reputation: 16
what im trying to do is detect if a cd is inserted or not . when a cd is inserted and the df command is used /dev/sr0 (or /dev/scd0) appears in the output but is absent when a cd is not inserted . so i basically exploit this to check if a cd is available or not.
 
Old 03-20-2010, 12:52 PM   #10
nonamenobody
Member
 
Registered: Oct 2002
Posts: 138

Rep: Reputation: 22
Quote:
Originally Posted by onebuck View Post
Hi,

You really should get some background information or references;

Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Bash Reference Manual
Advanced Bash-Scripting Guide
Linux Newbie Admin Guide
LinuxSelfHelp
Getting Started with Linux

These links and others can be found at 'Slackware-Links' .
More than just SlackwareŽ links!
If you follow one_buck's advice, you will save yourself a lot of time. You may even be able to find some of the documents in your own native language (which I am guessing is not English).

I will, however, try to answer your question. Using "cut -f 1 -d ' '" will give you a the first row. However because the rows are not separated with a a single character separator, getting the second row (if you wanted it) would be tricky. You might want to use:
Code:
grep '/dev/sr0' /proc/mounts | cut -f 1 -d ' '
rather than
Code:
df | grep '/dev/sr0' | cut -f 1 -d ' '
I notice you have asked lots of questions but you don't seem to have said what is that you are trying to accomplish. We know you want to execute a command when a CD is mounted - but for what purpose?
Also it isn't clear if you want to execute the command once per mount or the whole time the cd is mounted.

Do you want to have a program autorun when you put the CD in your computer? If so why not just run the program manually?

Do you want to have a program autorun when you put the CD in another computer? You will have to install the autorun program you are trying to make, which kind of defeats the purpose.
 
Old 03-20-2010, 05:18 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Anurupr,

I've closed one of your duplicated threads, and merged two others.
I'd recommend that you read our rules again and discontinue this
bad practice - stick with one thread, and run with it if the problem
belongs in the same scope...


Cheers,
Tink
 
Old 03-20-2010, 06:16 PM   #12
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,240

Rep: Reputation: 196Reputation: 196
These commands:

Code:
c=`df | grep "/dev/sr0" | cut -f 1-2 -d ' '`
echo \"$c\"
give as an output:

Code:
"/dev/sr0 "
which isn't equal to:

Code:
"/dev/sr0"
in your while condition.

These commands:

Code:
c=`df | grep "/dev/sr0" | cut -f 1 -d ' '`
echo \"$c\"
give the output you need:

Code:
"/dev/sr0"
In short: cut -f 1-2 -d ' ' adds unwanted trailing tab.
 
Old 03-20-2010, 07:27 PM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Code:
df|awk '$1 ~ /sr0/{print $1}'
 
Old 03-21-2010, 03:30 AM   #14
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Hi anurupr

It seems we are all on a bit of a goose chase here it would appear to me.
From reading this thread (and others you have posted) your only requirement
is to check that df is returning /dev/sr0 and not to actually get this information in a variable.

In this case you simply need the following:

Code:
while [[ ! $(df | grep -q /dev/sr0) ]]
do
<your code here>
done
 
Old 03-21-2010, 02:21 PM   #15
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,197

Rep: Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642
fyi, i put this code in my xbmc /etc/rc.local. maybe this mite help:
Code:
while [ 1 ]
do
 if [ -z "`ifconfig | grep wlan0"` ]
 then
  sudo /etc/init.d/networking restart
  sshfs user@server:/media /stuff
 fi
 sleep 10
done &
 
  


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
Command syntax to modify multiple files sg_sonic Linux - Newbie 5 09-27-2009 03:28 PM
How to redirect the output of exec command output in perl? sudhap85 Linux - Newbie 3 08-28-2009 04:35 PM
Grep on output of command returns all output traigo Linux - Newbie 2 02-24-2009 06:15 PM
vi help, how to modify previous command ufmale Linux - Newbie 1 09-05-2008 01:20 PM
How do I parse the output of a command that output to standard out? gauol Programming 9 01-17-2008 02:54 PM


All times are GMT -5. The time now is 12:02 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration