Re: Shell-wrap a file in 2 variable strings
Re: Shell-wrap a file in 2 variable strings
- Subject: Re: Shell-wrap a file in 2 variable strings
- From: Axel Luttgens <email@hidden>
- Date: Sun, 01 Feb 2004 21:41:56 +0100
(sorry Gnarlodious, AGAIN forgot to change "To:" field :-( )
Gnarlodious wrote:
>
Entity Chris Janton spoke thus:
>
>
> echo "TAG-OPEN" `cat somefile` "TAG-CLOSE" > some-file
>
>
Thanks, Chris, the trick, it seems, is to wrap the command in slanty
>
quotes
>
(wonder why?):
>
>
do shell script "echo " & quoted form of startHTML & " " & "`cat " &
>
flopPOSIX & "`" & " " & quoted form of endHTML & ">>" & iPathPOSIX
>
>
Or am I doing this the hard way?
No no... ;-)
You could just slightly simplify the above as:
do shell script "echo " & quoted form of startHTML & " `cat " &
flopPOSIX & "` " & quoted form of endHTML & ">>" & iPathPOSIX
>
This command works:
>
"<html>" `cat flip.txt` "</html>" > testFile.txt
>
>
note that it uses slanty quotes, "`" (ASCII 96).
>
>
But these simply wrap the command and filepath in the tags:
>
echo "<html>" cat flip.txt "</html>" > testFile.txt
>
echo "<html>" 'cat flip.txt' "</html>" > testFile.txt
>
like this:
>
<html> cat flip.txt </html>
>
>
So it's possible to say that Terminal ignores straight quotes (ASCII 39).
As you sure know, the Terminal has nothing to do here; it is really a
shell matter.
So, type this into a Terminal window:
ls -al
--> a listing of current directory [1]
and compare the result to the one obtained with:
echo ls -al
--> ls -al [2]
This tell us two things.
First, when the shell is expecting a command, it takes the first "word"
("ls" in [1], "echo" in "2") as a command name that will be applied to
the remaining data on the line ("-ls" in [1], "ls -al" in [2]).
Second, a command name passed as a argument to another command will
generally not provoke the execution of that command (ie. "ls" passed as
the first argument to "echo" is just treated as a string to be echoed).
Note that the "generally" is very important, as some commands may be
conceived so as to take one or more arguments as command names to be
executed...
But back to [2].
"echo" is the command name, "ls" and "-al" are two arguments passed to
that command.
As "ls" and "-al" don't contain special characters, the evaluation of
those arguments is straightforward: the evaluated arguments are just the
original strings (is "ls" and "-al").
Let's take an other example of evaluation:
myvar=ls; echo $myvar -al
--> ls -al [3]
The first command ("myvar=ls") creates a shell variable named "myvar"
and set it to "ls".
The second command encouters a shell special character ("$") which means
here "evaluate me by taking the value of the corresponding variable".
As a result, echo is again executed with arguments "ls" and "-al".
Another example of evaluation:
myvar=ls; echo "$myvar -al"
--> ls -al [4]
The difference is very subtle; the double quotes now tell the shell to
evaluate whatever is within those quotes, but to pass the result as a
single argument to the echo command.
And another one:
myvar=ls; echo '$myvar -al'
--> $myvar -al [5]
The single quotes tell the shell to take their contents (almost) as is,
and to pass that one as a single argument to the echo command.
Yet another one:
myvar=ls; echo `$myvar -al`
--> kind of current directory listing
The slanty quotes tell the shell to evaluate their contents, then to
take the evaluation's result as a command to be executed, and finally to
pass the standard output of that execution as an argument to the echo
command.
As a side note: the correct shell terminology is "substitution", not
"evaluation".
Must now be clear why ;-)
>
>
Guess I just don't grasp the concept, unless it's only a shortcut to
>
escape
>
Applescript characters..
No, as said above, the whole matter happens at the shell level.
And yes, its is rather subtle; I don't remember who wrote about shell
scripting as a kind of self-torture...
The hard, but possibly the best way: man sh.
Very complete, but EACH word matters.
And as a preparation,
http://rhols66.adsl.netsonic.fi/era/unix/shell.html
which has been written by the guy who must know: Steve Bourne himself!
Far more readable than the man pages, but yet without any
over-simplification (just a bit historic, but still sooo pertinent).
HTH,
Axel
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.