Re: puzzled by grep
Re: puzzled by grep
- Subject: Re: puzzled by grep
- From: Axel Luttgens <email@hidden>
- Date: Mon, 30 Dec 2013 13:42:02 +0100
Le 30 déc. 2013 à 12:00, koenig.yvan a écrit :
> [...]
>
> So I wrote :
> set rawText to (do shell script "defaults read /Library/Preferences/com.apple.windowserver")
> do shell script "echo " & rawText & " | grep -w Width"
>
> It failed.
Hello Yvan,
There are at least two problems with the above.
First, the shell will choke when encountering non quoted special characters after the "echo" command.
So, at the very least, one should have something like this one:
do shell script "echo " & quoted form of rawText & " | grep -w Width"
Second, "do shell script" alters line endings, and "grep" will thus be fed with a single line.
So, this one would probably better suited:
set rawText to (do shell script "defaults read /Library/Preferences/com.apple.windowserver" without altering line endings)
do shell script "echo " & quoted form of rawText & " | grep -w Height"
> I tried also a syntax which I saw in sed instructions :
>
> set rawText to (do shell script "defaults read /Library/Preferences/com.apple.windowserver")
> do shell script "grep -w Width <<< " & rawText
>
> it failed too.
For quite similar reasons. ;-)
Hence a similar possible "cure":
set rawText to (do shell script "defaults read /Library/Preferences/com.apple.windowserver" without altering line endings)
do shell script "grep -w Width <<< " & quoted form of rawText
> Am I Am I trying to do an impossible task ?
> If it's If it's possible, what is the correct syntax ?
I wouldn't pretend above suggestions are correct ones: it is always a bit hazardous to feed shell scripts with arbitrary external strings.
Moreover, you would end in this precise case with three invocations of "do shell script" (reading the preferences then grepping the height then grepping the width).
So, one may try to let the shell do the hard work while avoiding ambiguous constructions; for example:
{word 3, word 6} of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -e 'Height = ' -e 'Width = '")
HTH,
Axel
_______________________________________________
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