image
image

Go Back   macosx.com > Mac Help Forums > Unix & X11

Reply
 
Thread Tools
  #1  
Old February 18th, 2007, 02:21 PM
Registered User
 
Join Date: Feb 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
zberlin is on a distinguished road
error in script

Hey guys...

Maybe one of you can help me on this. I've created a script that closes a specified application

the script is titled close() and is the following:

close() { kill $(ps -xc | grep -i $* | awk '{print}') ;}

I saved this file in the ~/.bashrc file. The ~/.bash_profile calls the ~/.bashrc file with

source ~/.bashrc

when i start a new session in the bash terminal everything works fine when i activate this program.... however i get an erroneous output in stderr when i close an application such as Safari.app that is the following:

bash: kill: ??: no such pid
bash: kill: S: no such pid
bash: kill: 0:02.88: no such pid
bash: kill: Safari: no such pid


Can anyone explain this?

ZBerlin
Reply With Quote
  #2  
Old February 18th, 2007, 04:12 PM
Registered User
 
Join Date: Oct 2006
Posts: 172
Thanks: 0
Thanked 0 Times in 0 Posts
Flying Meat is on a distinguished road
Looks like you want to get the pid of the passed app name.
awk is returning the entire line.

I'm pretty sure you can tell awk to get just the first "word" of the returned line of text.

I'm not an awk user, so uh,.. man awk?
Reply With Quote
  #3  
Old February 18th, 2007, 04:17 PM
Registered User
 
Join Date: Oct 2006
Posts: 172
Thanks: 0
Thanked 0 Times in 0 Posts
Flying Meat is on a distinguished road
ah!

Change the {print} part to read {print $1}

man awk, indeed.

Troubleshooting:
I ran this:
ps -xc | grep -i textedit | awk '{print}' at the command line and saw this:
2134 ?? S 0:00.16 TextEdit

ergo, awk returns the full line of text.

Looking at the man page about awk, I found an example that was helpful:
EXAMPLES
length($0) > 72
Print lines longer than 72 characters.

{ print $2, $1 }
Print first two fields in opposite order. <<<< BINGO!

Changed the command to this:
ps -xc | grep -i textedit | awk '{print $1}' at the command line and saw this:
2134

Ran the command:
{ kill $(ps -xc | grep -i textedit | awk '{print $1}') ;} and the app quit, without error messages.

Last edited by Flying Meat; February 18th, 2007 at 07:53 PM. Reason: clarification (not in awk, but about awk)
Reply With Quote
  #4  
Old February 18th, 2007, 07:05 PM
Registered User
 
Join Date: Feb 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
zberlin is on a distinguished road
excellent.. it worked. I'm still very new in the Unix world. It will take me some time to learn the basic scripts for bash

thanx again
Reply With Quote
  #5  
Old February 18th, 2007, 07:52 PM
Registered User
 
Join Date: Oct 2006
Posts: 172
Thanks: 0
Thanked 0 Times in 0 Posts
Flying Meat is on a distinguished road
Well, you are ahead of me with awk (believe it or not, I've been scripting for a few years) and making great progress, as far as I can tell.

Just remember, the man command is your friend.

You may find that writing these routines to .bashrc is not an effective solution long term. Eventually you could wind up with dozens and dozens. That would slow down terminal session launches (potentially preventing terminal sessions if there are errors in the code).

Try creating single stand alone scripts. They are just text files with appropriate privileges applied.
I don't know if you already have this link as a resource, but this has been useful to me:
http://www.tldp.org/LDP/abs/html/

Last edited by Flying Meat; February 18th, 2007 at 08:17 PM. Reason: additional info.
Reply With Quote
  #6  
Old February 19th, 2007, 07:24 AM
Registered User
 
Join Date: Feb 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
zberlin is on a distinguished road
Point well taken. I'm just doing what the manual tells me to do. As an extra it asked the reader to create their own script using the previous lesson as a guide. Which caused my dilemma. An important thing for me to learn is understanding stderr outputs. Among other things. But I've only been using Unix for less than a week. I can only absorb so much.

I bookmarked that site... Hopefully in a week or so, i'll be ready for that reference guide.

What script editor do you recommend I use. I could use "nano" or textedit.app but I'm sure there are better ones out there

With respect to creating individual scripts, do you recommend i save them in a scripts folder in /[Users]/Documents or do you think i should save it in the /bin directory

Last edited by zberlin; February 19th, 2007 at 10:54 AM.
Reply With Quote
  #7  
Old February 19th, 2007, 11:33 AM
Registered User
 
Join Date: Mar 2005
Posts: 199
Thanks: 0
Thanked 1 Time in 1 Post
artov is on a distinguished road
You might like to check killall(1). It does not support regular expressions
like your script, but otherwise it is quite similar.
Reply With Quote
  #8  
Old February 19th, 2007, 12:43 PM
Registered User
 
Join Date: Oct 2006
Posts: 172
Thanks: 0
Thanked 0 Times in 0 Posts
Flying Meat is on a distinguished road
I generally save my scripts-in-development in my home folder. Once I know they are running as expected, then I move them to a more logical place.

I personally like the idea of /usr/local/sbin for my stuff. Others may provide guidance on this as well...

There have been many debates as to which text editor is the bestest of all time, bow in it's general direction, it roolz you.

Try out a few and keep an open mind.
I have never wanted to learn archane key combinations so I tend to go with GUI text editors myself. I do resort to nano (pretty simple) on occasion, but I do most of my editing in Nedit. It's open source, pre-compiled binary, is an X11 app, has tabbed multi-document editing, and syntax highlighting...

There are also great free tools like Text Wrangler (from the fine folk that brought you BBEdit (not free but extremely flexible), or Smultron.
www.nedit.org for Nedit of course
www.macupdate.com for Text Wrangler, Smultron...

Last edited by Flying Meat; February 19th, 2007 at 12:51 PM. Reason: added another editer name
Reply With Quote
Reply

Thread Tools

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -5. The time now is 09:12 AM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.