LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to modify the output of df command (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-modify-the-output-of-df-command-796668/)

anurupr 03-20-2010 07:59 AM

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

onebuck 03-20-2010 08:03 AM

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!

grail 03-20-2010 08:45 AM

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

anurupr 03-20-2010 08:50 AM

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

anurupr 03-20-2010 09:05 AM

i dont want to return the entire row i jus need to return "/dev/sr0"

schneidz 03-20-2010 09:12 AM

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 ?

bremarv 03-20-2010 09:14 AM

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.

anurupr 03-20-2010 09:29 AM

weird :(

anurupr 03-20-2010 10:47 AM

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.

nonamenobody 03-20-2010 11:52 AM

Quote:

Originally Posted by onebuck (Post 3905482)
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.

Tinkster 03-20-2010 04:18 PM

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

w1k0 03-20-2010 05:16 PM

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.

Tinkster 03-20-2010 06:27 PM

Code:

df|awk '$1 ~ /sr0/{print $1}'

grail 03-21-2010 02:30 AM

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


schneidz 03-21-2010 01:21 PM

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 &



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