Re: Big bug in shell scripting in Applescript
Re: Big bug in shell scripting in Applescript
- Subject: Re: Big bug in shell scripting in Applescript
- From: "Mark J. Reed" <email@hidden>
- Date: Thu, 04 Aug 2011 22:56:47 -0400
The
echo command is the shell's print command; a "Hello, world" program in shell looks like this:
echo Hello, World # "Hello, World"
The pipe symbol "|" takes the output of the left command and feeds it to the right command, so you can make
echo work with
sed like this:
echo Hello, World | sed s/o// # "Hell, World"
Which works fine for one-liners. The problem with this from the standpoint of more significant shell programs is that the command on the right of a pipe is executed in a different shell environment, so it can't have any side effects inside the shell.
For instance, the
read command takes a variable name, reads a line of input, and stores the input line in the named variable (creating it if it doesn't exist). But reading at the end of a pipe does no good because the shell environment in which the variable is set is discarded immediately:
echo Hello, World | read myLineecho $myLine # "" - var is not set
The traditional solution is what's called a "here-document", which looks like this:
echo $myLine # "Hello, World"
That's a bit clunky for one-liners, though, so bash added a single-line version called the "here-string":
read myLine <<<"Hello, World"
echo $myLine # "Hello, World"
Which is what we've been using a lot in do shell script.
On Thursday, August 4, 2011, Zavatone, Alex <
email@hidden> wrote:
> Not sure how echo would work with sed. Remember, I'm new and clueless here.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden