Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rate Thread Display Modes
Old 01-27-2004, 10:33 AM   #1
sweyhrich
Prospect
 
Join Date: Mar 2002
Location: Omaha, NE
Posts: 44
Complex alias in bash

I was online with my dialup connection and found things running really slowly. I decided it was time to do a restart, and iTunes announced to me that someone was connected to my shared iTunes library! At home on my Airport network I've just left this on, for the sake of others in the house who want to connect from time to time, but I'd never come across who sniffed my iTunes library over the dialup before.

Anyway, in perusing the hints, I found one that lets me see who is connected (I'd like to know next time), and this is the code:

Code:
netstat -f inet -W | awk '$4 ~ /3689/ {print "iTunes connection from: "$5}' \
 | sed -e 's/\.[^.]*$//g'
However, in trying to make this into an alias for bash, I found that

Code:
alias itunesshare="netstat -f inet -W | awk '$4 ~ /3689/ {print "iTunes connection from: "$5}' \
  | sed -e 's/\.[^.]*$//g'"
doesn't work because part of the line I am trying to make into an alias already contains the double quote, as well as the single quote. (Even if I leave out the "\" to do it on two lines). Is there a way to make this work?

(There was a downloadable script on that hint page, but it was from May 2003, and the link is no longer valid).
__________________
Steven Weyhrich
Apple II History
http://apple2history.org
sweyhrich is offline   Reply With Quote
Old 01-27-2004, 02:03 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
3 possibilities

1) escape the embedded single quotes with backslashes: \'

2) make this into a bash "function" instead of an "alias". Aliases are intended only for relatively simple things.

3) make it a bash script by saving the command (not the alias) to a file, making the first line
#!/bin/sh
and making the fiel executable.
hayne is online now   Reply With Quote
Old 01-27-2004, 02:59 PM   #3
sweyhrich
Prospect
 
Join Date: Mar 2002
Location: Omaha, NE
Posts: 44
Re: 3 possibilities

Quote:
Originally posted by hayne
3) make it a bash script by saving the command (not the alias) to a file, making the first line
#!/bin/sh
and making the fill executable.

So I put that line in my first message in a file by itself, with the first line of the file being the "#!/bin/sh", and the second line the command.

Where do I put such a file, and how do I make it executable?
__________________
Steven Weyhrich
Apple II History
http://apple2history.org
sweyhrich is offline   Reply With Quote
Old 01-27-2004, 03:12 PM   #4
zed
Major Leaguer
 
Join Date: Sep 2002
Location: Earth
Posts: 381
Where to put the file ???

Well most UNIX people create a dir in their home folders called: bin

mkdir bin

then edit your path to include this directory..or just call the file directly

now to make it executable:

chmod is ya friend here

chmod 755 <filename>

Will do the trick

so now to run the file

~/bin/<filename>

the ~ is replaced automatically with ya home folder path...

Cheers,
--Zed
zed is offline   Reply With Quote
Old 01-27-2004, 05:58 PM   #5
gatorparrots
Major Leaguer
 
Join Date: Dec 2002
Posts: 441
Re: bash script

Quote:
Originally posted by hayne
3) make it a bash script by saving the command (not the alias) to a file, making the first line
#!/bin/sh

To be a bash script, shouldn't the first line be
#!/bin/bash
(I know bash is used to provide /bin/sh under Panther, but you don't want to impose Bourne shell limitations on yourself unnecessarily...)
gatorparrots is offline   Reply With Quote
Old 01-27-2004, 06:10 PM   #6
sweyhrich
Prospect
 
Join Date: Mar 2002
Location: Omaha, NE
Posts: 44
Quote:
Originally posted by zed
Where to put the file ??? (and comments following)

Thanks, that worked.

Quote:
Originally posted by gatorparrots
To be a bash script, shouldn't the first line be
#!/bin/bash

I notice it works either way. Why is either necessary, and why would one be preferred?
__________________
Steven Weyhrich
Apple II History
http://apple2history.org
sweyhrich is offline   Reply With Quote
Old 01-27-2004, 06:54 PM   #7
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,334
the `#!` operator tells the operating system to execute the following code with the specified program. `/bin/sh` is the path to the original Bourne shell interpreter. `/bin/bash` is the path to the Bourne Again SHell. If you write a perl program, you set it to `/usr/bin/perl` or wherever perl is on your computer.

Your script is simple enough that it will run under sh, bash, tcsh or almost any other shell. More complex scripts are not so flexible.
acme.mail.order is offline   Reply With Quote
Old 01-27-2004, 07:03 PM   #8
sweyhrich
Prospect
 
Join Date: Mar 2002
Location: Omaha, NE
Posts: 44
Thanks for the explanation. As always, the answer and associated comments have been educational!
__________________
Steven Weyhrich
Apple II History
http://apple2history.org
sweyhrich is offline   Reply With Quote
Reply


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 On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 02:14 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.