You are not logged in.

#1 2009-01-26 05:22:36

jiu
Member
From: Auckland, New Zealand
Registered: 2008-05-03
Posts: 31

newbie bash question

Hi,
I'm trying to make an alias for a function to kill whatever program is accessing my DVD drive, because sometimes vlc (or sthg else) will shut down in a dirty way and prevent me from ejecting the drive.

When I run:

lsof -t /dev/sr1|xargs kill -9

from the command line, it works.

But if I put:

alias fcd="lsof -t /dev/sr1|xargs kill -9"

in .bashrc and invoke fcd, I get an error.

Do you know what the problem might be?

Offline

#2 2009-01-26 05:31:58

kludge
Member
Registered: 2008-08-03
Posts: 294

Re: newbie bash question

Do you know what the problem might be?

no, not without seeing the error message.

btw, 'fuser' might be more directly applicable to the task at hand.


[23:00:16]    dr_kludge | i want to invent an olfactory human-computer interface, integrate it into the web standards, then produce my own forked browser.
[23:00:32]    dr_kludge | can you guess what i'd call it?
[23:01:16]    dr_kludge | nosilla.
[23:01:32]    dr_kludge | i really should be going to bed.  i'm giggling madly about that.

Offline

#3 2009-01-26 06:16:54

fumbles
Member
Registered: 2006-12-22
Posts: 246

Re: newbie bash question

As far as I can tell there is nothing wrong with your .bashrc. The only way I could see that it would come up with an error is that there was no process that was accessing your DVD drive. So the kill command would be given nothing to kill. Also remember with the lsof command that if the process is running as root a normal user cannot see that process. Otherwise it is because you haven't sourced the .bashrc file. The actual error would be nice to determine what the exact problem was.

Offline

#4 2009-01-26 08:01:13

jiu
Member
From: Auckland, New Zealand
Registered: 2008-05-03
Posts: 31

Re: newbie bash question

As I was trying to reproduce the problem so I could post the exact output, I noticed that while VLC was playing the DVD, lsof /dev/sr0 returned nothing. this seems weird (and I wasn't running vlc as root...). It could explain my problem because then there would be no process to kill.
@kludge: thanks, indeed it's more simple using: fuser -k /dev/sr0

While I'm at it, I tried earlier to make an alias to kill firefox using the long way (before I learnt about killall):
ps -e|grep firefox|awk {'print $1'}|xargs kill -9
And this works but if I make it an alias, there seems to be a problem with the text delimiter:
alias kfx="ps -e|grep firefox|awk {'print $1'}|xargs kill -9" does not work and I think it's because the ' and the " get mixed up. Is there a way to sort this out?

Offline

#5 2009-01-26 09:45:13

kludge
Member
Registered: 2008-08-03
Posts: 294

Re: newbie bash question

it's far too late/early for me to figure it out exactly, but curly braces get a special treatment by bash that i have still to understand completely.

definitely read the bash man page, but be forewarned that it's weighty stuff.  (btw, did you know you can search man pages by hitting '/', typing your search string--making sure to escape special characters, and hitting <enter>... you do now!)

there are also lots of good bash scripting tutorials out there.  i'd suggest searching for "curly brace expansion" and "quote expansion" with "bash".

good luck!


[23:00:16]    dr_kludge | i want to invent an olfactory human-computer interface, integrate it into the web standards, then produce my own forked browser.
[23:00:32]    dr_kludge | can you guess what i'd call it?
[23:01:16]    dr_kludge | nosilla.
[23:01:32]    dr_kludge | i really should be going to bed.  i'm giggling madly about that.

Offline

#6 2009-01-26 09:51:09

kludge
Member
Registered: 2008-08-03
Posts: 294

Re: newbie bash question

afterthought:  what you're trying to do with xargs, i've always accomplished with backticks.  they tell bash to expand to the results of the command list contained within them.  something like:

"kill -9 `ps -e|grep firefox|awk {'print $1'}`"

[23:00:16]    dr_kludge | i want to invent an olfactory human-computer interface, integrate it into the web standards, then produce my own forked browser.
[23:00:32]    dr_kludge | can you guess what i'd call it?
[23:01:16]    dr_kludge | nosilla.
[23:01:32]    dr_kludge | i really should be going to bed.  i'm giggling madly about that.

Offline

#7 2009-01-26 10:31:02

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: newbie bash question

afterafterthought: backticks are deprecated in favour of $() constructs:

"kill -9 $(ps -e|grep firefox|awk {'print $1'})"

Offline

#8 2009-01-27 03:26:20

jiu
Member
From: Auckland, New Zealand
Registered: 2008-05-03
Posts: 31

Re: newbie bash question

Well, thanks for all your replies.

@kludge: indeed the bash man page is such weighty stuff that it's discouraging. I understand their need to be complete, accurate and concise but it's like legal talk to me. I can't imagine why they don't give examples of the most used cases (OK, they sometimes do at the end of a page, but not enough). Yes, I knew about the search function.

Offline

#9 2009-01-27 07:48:04

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: newbie bash question

Try this instead.

Offline

#10 2009-01-27 09:12:37

fumbles
Member
Registered: 2006-12-22
Posts: 246

Re: newbie bash question

use pgrep or pkill instead of grep and awk.

Offline

#11 2009-01-27 13:11:09

andre.ramaciotti
Member
From: Brazil
Registered: 2007-04-06
Posts: 649

Re: newbie bash question

tomk wrote:

afterafterthought: backticks are deprecated in favour of $() constructs:

"kill -9 $(ps -e|grep firefox|awk {'print $1'})"

Aren't the single quotes in the wrong place? Shouldn't they be outside the curlybraces?

"kill -9 $(ps -e|grep firefox|awk '{print $1}')"

(lambda ())

Offline

Board footer

Powered by FluxBB