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



Reply
 
Thread Tools Rate Thread Display Modes
Old 01-26-2002, 03:31 PM   #1
jmb
Triple-A Player
 
Join Date: Jan 2002
Posts: 92
tcsh and quotes

Hi,

I'm adding custom aliases to my .tcshrc file, and I have a question... I got a sample file from a friend, and some of the alias stings are set off with single quotes (') and some with double quotes (")... For example:

alias blah 'blah blah blah'

or

alias blab "blabbity blabbity"

He said there was a reason for using one over the other under different circumstances, but didn't know why... So, what's the answer? It does seem to make a difference in certain cases, but not in others. For example, my alias for opening the system prefs is as follows:

alias prefs "open -a /Applications/System\ Preferences.app"

I had previously tried it with single quotes, but I couldn't get it to 'source.' When I typed:

source .tcshrc

It gave me:

Unmatched '.

But when I comment it out or use double quotes, all is fine. Other alises for starting apps don't behave this way. Why?

Thanks,

--jmb
jmb is offline   Reply With Quote
Old 01-26-2002, 06:20 PM   #2
Novajo
Triple-A Player
 
Join Date: Jan 2002
Location: Toronto, Canada
Posts: 185
Single quote: no interpolation; double quotes interpolation

With double quotes, variables are interpolated (replaced with their values). With single quotes, they are not.

If you define

alias test1 "echo $youwin$"

This will fail telling you that $youwin is not a variable because it is enclosed in double quotes. It might be what you want to say though: you might really want to define echo $youwin$. You have to define it this way:

alias test1 'echo $youwin$'

so that anything prefixed by a dollars $ is not mistaken for a variable.
Novajo is offline   Reply With Quote
Old 01-29-2002, 03:08 PM   #3
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
navajo, thanks for the great explanation.

so

single quotes are always a literal string of characters, passed thru the shell processing unaltered.

&

double quotes are a literal string (?) that the shell will scan and interpolate shelly things?

there's more to it than that, i believe, because there are other shell quoting chars, but that's a starter.
mervTormel is offline   Reply With Quote
Old 10-25-2002, 12:43 AM   #4
thatch
All Star
 
Join Date: Jan 2002
Posts: 534
Question Reviving this old thread for a moment...

I'm looking for a little clarification on special quoting circumstances, specifically this alias I would like to create although the single quote doesn't work and neither does double quote or back tics:

alias frsrc 'find . -type f | sed 's/^/"/;s/$/\/rsrc"/' | xargs ls -l'

This is in a tcsh shell with Jag 10.2.1 and will find resource fork files in the cwd. Does anyone know if there is another way to get it working without changing the guts of the line too much?

Muchly appreciated in advance.
thatch is offline   Reply With Quote
Old 10-25-2002, 06:43 AM   #5
ericw13
Major Leaguer
 
Join Date: Oct 2002
Posts: 268
Re: Reviving this old thread for a moment...

Quote:
Originally posted by thatch
I'm looking for a little clarification on special quoting circumstances, specifically this alias I would like to create although the single quote doesn't work and neither does double quote or back tics:

alias frsrc 'find . -type f | sed 's/^/"/;s/$/\/rsrc"/' | xargs ls -l'

This is in a tcsh shell with Jag 10.2.1 and will find resource fork files in the cwd. Does anyone know if there is another way to get it working without changing the guts of the line too much?

Muchly appreciated in advance.

The problem is the quotes for sed. As soon as you hit the ' after sed, you've closed the quote that started just before find, leaving

find . -type f | sed

not an overly useful command.

Any time you need to use literal ' (and possibly the double quotes ") inside a quoted alias, backslash escape them (' -> \', " -> \")...

alias frsrc 'find . -type f | sed \'s/^/\"/;s/$/\/rsrc\"/\' | xargs ls -l'

Again, you may or may not need to escape the double quotes so try it both ways.

HTH
Eric
ericw13 is offline   Reply With Quote
Old 10-25-2002, 06:45 AM   #6
ericw13
Major Leaguer
 
Join Date: Oct 2002
Posts: 268
Sheesh... what is with this board and disappearing backslashes?!?! Let's try this again (and learn to use preview more often )

alias frsrc 'find . -type f | sed \'s/^/\"/;s/$/\/rsrc\"/\' | xargs ls -l'

Again, you may or may not need to escape the double quotes...

Eric
ericw13 is offline   Reply With Quote
Old 10-26-2002, 01:54 AM   #7
thatch
All Star
 
Join Date: Jan 2002
Posts: 534
ericw13, thanks for the reply. What you said makes perfect sense and I had tried the escapes to no avail both ways. I'm still getting the illegal variable name message from the shell. Can you suggest anything else? TIA.

Just to be clearer, here are what I've tried...


alias frsrc 'find . -type f | sed \'s/^/"/;s/$/\/rsrc"/\' | xargs ls -l'

alias frsrc 'find . -type f | sed \'s/^/\"/;s/$/\/rsrc\"/\' | xargs ls -l'


alias frsrc "find . -type f | sed 's/^/\"/;s/$/\/rsrc\"/' | xargs ls -l"

alias frsrc "find . -type f | sed \'s/^/\"/;s/$/\/rsrc\"/\' | xargs ls -l"


Last edited by thatch; 10-27-2002 at 01:45 AM.
thatch is offline   Reply With Quote
Old 10-28-2002, 07:31 AM   #8
ericw13
Major Leaguer
 
Join Date: Oct 2002
Posts: 268
I'm not much up on sed (preferring to use perl for pattern matching...), but this is what I used to create the alias (this is bash, so slightly different syntax for setting aliases):

alias frsrc='find . -type f | sed "s/^/\"/;s/$/\/rsrc\"/" | xargs ls -l'

Not entirely sure what the sed part actually does though. From your original post, you said you want to limit the search to cwd. If so, you should add -maxdepth 0 to the find command. This will prevent find from traversing down the directory structure.

Note the differences: the sed commands are delimited with ", not '. The literal " inside sed are backslash escaped.

HTH
Eric
ericw13 is offline   Reply With Quote
Old 10-28-2002, 03:12 PM   #9
thatch
All Star
 
Join Date: Jan 2002
Posts: 534
ericw13, thanks again for your reply. I'm chalking this one up to the wonkiness of tcsh. It appears that other shells work fine as you have shown.

Quote:
Originally posted by ericw13
Not entirely sure what the sed part actually does though. From your original post, you said you want to limit the search to cwd....

Actually, my original post just stated that the line finds resource files in the cwd, which it does do quite nicely without the aliasing in tcsh. The sed line just filters down any filename beginning in anything and ending in rsrc and pipes it on to xargs.
thatch is offline   Reply With Quote
Old 10-28-2002, 04:07 PM   #10
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
another ding against the C-shell family...

% man csh
...
After the input line is aliased and parsed, and before each command is
executed, variable substitution is performed keyed by `$' characters.
This expansion can be prevented by preceding the `$' with a `\\' except
within `"'s where it always occurs, and within `''s where it never
occurs. Strings quoted by ``' are interpreted later (see Command
substitution below) so `$' substitution does not occur there until later,
if at all. A `$' is passed unchanged if followed by a blank, tab, or
end-of-line.
...


from UNIX Power Tools:
...

you can't quote things reasonably well in csh.

dollar signs ($) cannot be escaped in double quotes in csh. ugh

% set foo = "this is a \$dollar quoted and this is $HOME not quoted"
dollar: Undefined variable.

...

thatch, i suggest you craft that find into a sh script.

for filtering out the chaff, i've had luck with this incantation...
Code:
$ find . -type f | sed 's/^/"/;s/$/\/rsrc"/' | xargs ls -l | \
    awk '{if ($5 > 0 ) print $0 }'

Last edited by mervTormel; 10-28-2002 at 04:09 PM.
mervTormel is offline   Reply With Quote
Old 10-29-2002, 12:09 AM   #11
thatch
All Star
 
Join Date: Jan 2002
Posts: 534
mT, thanks. The sh script in ~/bin with the name frsrc works excellent. And the addition of the awk line leaves out all the zero byte files. Perfect!

thatch is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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:29 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, 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.