error in script

zberlin

Registered
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
 
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?
 
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.
 
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
 
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/
 
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
 
You might like to check killall(1). It does not support regular expressions
like your script, but otherwise it is quite similar.
 
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...
 
Back
Top