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



Reply
 
Thread Tools Rate Thread Display Modes
Old 10-08-2003, 11:26 AM   #1
cfoster
Prospect
 
Join Date: Sep 2003
Location: Toronto, ON, Canada
Posts: 18
Question How to embed a single quote (') in an alias command?

Hi all,

I'm working on a hint for the main site, but need to know how to embed a single quote when adding an alias command to one's .login file.

For example if I want to echo out the phrase "can't" every time I type the command "test" I might make an alias like so:
Code:
alias test 'echo can\\'t'
Unfortunately this doesn't work ("Unmatched ' ").

The "alias" command takes two parameters so the single or double quotes are necessary around the second parameter to keep it all together (in this simple example you can do without them but for anything complex they are necessary).

The real alias command I'm trying to get accepted is this (which will be posted to the main site as soon as I can figure out this little problem):
Code:
alias dureport 'du -Ps * | awk '{printf "%12d\t%s\n", $1, substr($0,index($0,$2),80)}' |sort -r'
(It's purpose is to summarize the disk usage of each item in the current directory, and sort it by size. Very useful for figuring out where you are wasting disk space. I recently found an innocuous looking folder with 5GB of video footage in it that I had forgotten about.)

As you can see, awk requires the single quote characters to do its thing but I can't get them to embed properly as a parameter of the alias command. But don't worry about that big ugly string: if someone knows how to fix the problem with that simple 'echo' command I think that should solve my issue with the longer string too.

Thanks,
Colin.

Last edited by cfoster; 10-08-2003 at 11:29 AM.
cfoster is offline   Reply With Quote
Old 10-08-2003, 11:41 AM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
1) To include a single-quote in the body of a csh or tcsh alias, use the 4 characters '\''

2) If you use the -k option to 'du', it gives the size in KB rather than in (512 byte) blocks. This gives sizes that are in more familiar units and directly comparable with what you see with 'ls -l'

3) I use 'du -skx * | sort -nr' to get the ordered list of disk space use that you are getting with your command
hayne is offline   Reply With Quote
Old 10-08-2003, 12:12 PM   #3
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
another blow against the csh family of shells; quoting nightmares.

simplify and use the tool's switches.

i find that:
Code:
$ du -xack /path/to/ 2>/dev/null | sort -nr | head -55
gives me what i'm looking for; heavy files/dirs.

and the head makes it fit in my terminal window, and the redirect errs to null keeps access errs quiet

as a bash function for the enlightened:
Code:
lhf ()  # list heavy files
{ 
    du -xack "$@" 2>/dev/null | sort -nr | head -55
}
mervTormel is offline   Reply With Quote
Old 10-08-2003, 12:53 PM   #4
cfoster
Prospect
 
Join Date: Sep 2003
Location: Toronto, ON, Canada
Posts: 18
I think I scoured every related man entry for a workaround option... except sort. Yes -n simplifies things, greatly, doesn't it?!

Good tip on escaping quote characters, just the same!

Thanks!
-Colin.
cfoster 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 08:30 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.