Thursday, August 28, 2014

#1 2012-03-30 12:05:19 pm

Jarmo
Member
Registered: 2012-03-30
Posts: 7

Attempting to grep a variable - Possible issue

I'm a little under the wire here, but to start things out I did perform a quick search on the forums to find a thread that would be a little helpful. I unfortunately was unable to find anything terribly relevant so I figured posting might actually yield some results.

The long of it:
So! I'm a CS major who's still in school, whom has 0 experience with AppleScript. I have a script that was generated by a past employee that's been a bit buggy. The script is essentially a simple little program that asks the user to input a flash drive, then asks them for their student ID. It then records a speech to the students H: drive as well as the flash drive. The error I'm running into now is a variable not being defined. I narrowed this down to a particular method not actually returning what it's supposed to return, and narrowed it down further to a particular variable not being set to anything.

The short of it
I have a dialog box that returns the result of "drive" and "driveSpace". What I can't figure out is why driveSpace never actually receives any sort of information. When the dialogbox pops up, there is nothing where dialogbox should be, it just shows me the name of the drive.


Applescript:


on verifyDriveSpace(drive)

try
--returns in the form of 999[Mi, Gi]
set driveSpace to do shell script "df -h | grep " & drive & " | awk {'print $4'}"

--The below dialog box is for testing purposes to find out what driveSpace is set to.
tell application "Finder"
activate
with timeout of dialogTimeout seconds
display dialog "test" & drive & driveSpace buttons {"Okay","Quit"} with title {"Test Box"}
end timeout
end tell
end try

When I go into terminal and type
df-h | grep LEXAR MEDIA (the name of the drive) | awk {'print $4'}
I get the drive space remaining, however when I type

df -h | grep " & LEXAR MEDIA & " | awk {'print $4'}
I get no result.

Could that be the problem? When I remove the ampersands applescript won't compile and if I remove the ampersands in addition to the quotes it doesn't compile either.

Please help! Thanks

Offline

 

#2 2012-03-30 12:18:39 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10490
Website

Re: Attempting to grep a variable - Possible issue

Hi,

the variable dialogTimeout in the handler is not defined

there could be another problem if the drive's name contains a space character.
In the shell space characters are parameter separators
To avoid the problem quote the name of the drive

Applescript:


set driveSpace to do shell script "df -h | grep " & quoted form of drive & " | awk {'print $4'}"

The timeout block can simplified

Applescript:


       tell application "Finder"
           activate
           display dialog "test" & drive & driveSpace buttons {"Okay", "Quit"} with title {"Test Box"} giving up after 5
       end tell

Last edited by StefanK (2012-03-30 12:20:05 pm)


regards

Stefan

Offline

 

#3 2012-03-30 12:27:32 pm

Jarmo
Member
Registered: 2012-03-30
Posts: 7

Re: Attempting to grep a variable - Possible issue

This is just a method, dialogTimeout is a variable initialized way above to some inordinately long number.

The space in the drive name seems to be the culprit. Any drive that I insert that only has a 1 word name works just fine, and the one that I've been able to get consistent errors with has a space character. The name of the drive is quoted however as it is in the blurb I posted in my OP.

drive of course being the parameter passed into the method.

Applescript:

... | grep "& drive & " | ...

Thanks for your help thus far!
-Jason

Offline

 

#4 2012-03-30 12:35:56 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10490
Website

Re: Attempting to grep a variable - Possible issue

Jarmo wrote:

drive of course being the parameter passed into the method.

Applescript:

... | grep "& drive & " | ...

pass the parameter unquoted and use quoted form of as mentioned above

Even in the Terminal you should write

Applescript:


df-h | grep 'LEXAR MEDIA' | awk {'print $4'}

Last edited by StefanK (2012-03-30 12:37:30 pm)


regards

Stefan

Offline

 

#5 2012-03-30 12:51:55 pm

Jarmo
Member
Registered: 2012-03-30
Posts: 7

Re: Attempting to grep a variable - Possible issue

I guess I'm just not understanding what you mean,

Even in the Terminal you should write
Applescript:

df-h | grep 'LEXAR MEDIA' | awk {'print $4'}

That makes sense but I don't understand how I would pass the unquoted form and use the quoted. Too many uses of the words form and quote tongue
If you could please just elaborate or break it down for a simpleton for me to understand it would be much appreciated.

Edit:
I've attempted a myriad of ways of quoting / unquoting / singlequoting / etc.

Last edited by Jarmo (2012-03-30 12:55:26 pm)

Offline

 

#6 2012-03-30 12:57:20 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10490
Website

Re: Attempting to grep a variable - Possible issue

just an example with a bit of error handling

Applescript:


set currentDrive to "LEXAR MEDIA"
verifyDriveSpace(currentDrive)

on verifyDriveSpace(drive)
   try
       set driveSpace to do shell script "df -h | grep " & quoted form of drive & " | awk {'print $4'}"
   on error e
       display dialog e
   end try
end verifyDriveSpace


regards

Stefan

Offline

 

#7 2012-03-30 01:29:17 pm

Jarmo
Member
Registered: 2012-03-30
Posts: 7

Re: Attempting to grep a variable - Possible issue

There's no point in the code where we would be able to put the flash drives name in quotes AFAIK. It generates a disk list for the user to choose which disk they would like to record to. Just to try out what you had posted however I came up with

Applescript:


try
set tempVar to "Lexar Media"
set driveSpace to do shell script "df -h | grep " & tempVar & " | awk {'print $4'}"
end try

Granted that's without the fun error checking. I dunno, maybe I'm a dunce but I got nothin'.

Offline

 

#8 2012-03-30 01:37:48 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10490
Website

Re: Attempting to grep a variable - Possible issue

maybe there is a misunderstanding

Applescript:


set tempVar to "Lexar Media"

the quotes indicate the literal string, nothing else

Applescript:


quoted form of "Lexar Media"

puts the literal string into the best possible quotation form to fit in the shell context


regards

Stefan

Offline

 

#9 2012-03-30 01:42:14 pm

Jarmo
Member
Registered: 2012-03-30
Posts: 7

Re: Attempting to grep a variable - Possible issue

I apologize, I feel like an idiot. I didn't know

quoted form of

was special applescript keywords. Sigh! I appreciate your help and patience.

Offline

 

#10 2012-03-30 01:46:31 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 1432

Re: Attempting to grep a variable - Possible issue

Hello

Just a question.
If the passed name is the startup Volume one, I get an empty string as result.
Is it normal ?

Applescript:



path to startup disk

tell application "Finder"
   set currentDrive to name of result
end tell
verifyDriveSpace(currentDrive)

on verifyDriveSpace(drive)
   try
       set driveSpace to do shell script "df -h | grep " & quoted form of drive & " | awk {'print $4'}"
   on error e
       display dialog e
   end try
end verifyDriveSpace

tell current application
   path to startup disk
       --> alias "Macintosh HD:"
end tell
(*
tell application "Finder"
   get name of alias "Macintosh HD:"
       --> "Macintosh HD"
end tell
tell current application
   do shell script "df -h | grep 'Macintosh HD' | awk {'print $4'}"
       --> ""
end tell
*)

Yvan KOENIG (VALLAURIS, France) vendredi 30 mars 2012 20:45:48

Offline

 

#11 2012-03-30 01:50:45 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10490
Website

Re: Attempting to grep a variable - Possible issue

Yes it's normal wink

the POSIX equivalent of path to startup disk is a single slash

Applescript:


path to startup disk
set currentDrive to POSIX path of result --> "/"
verifyDriveSpace(currentDrive)


regards

Stefan

Offline

 

#12 2012-03-30 01:54:10 pm

Jarmo
Member
Registered: 2012-03-30
Posts: 7

Re: Attempting to grep a variable - Possible issue

I'm actually now getting "Variable driveSpace is not defined." from

Applescript:

verifyDriveSpace(currentDrive)

on verifyDriveSpace(drive)
try
set driveSpace to do shell script "df -h | grep " & quoted form of drive & " | awk {'print $4'}"
on error e
display dialog e
end try
end verifyDriveSpace

The error handling yields
"Can't make quoted form of {{"LEXAR MEDIA"}} into type Unicode text."

Edit: Thinking about just making finder access the drive and return free space.

Last edited by Jarmo (2012-03-30 01:59:34 pm)

Offline

 

#13 2012-03-30 01:59:25 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10490
Website

Re: Attempting to grep a variable - Possible issue

Jarmo wrote:

I
The error handling yields
"Can't make quoted form of {{"LEXAR MEDIA"}} into type Unicode text."

You're passing a list of lists {{…}} instead of a string


regards

Stefan

Offline

 

#14 2012-03-30 02:31:13 pm

Jarmo
Member
Registered: 2012-03-30
Posts: 7

Re: Attempting to grep a variable - Possible issue

Thanks again Stefan for your help. I was eventually able to just:

Applescript:

tell application "Finder"
set driveSpaceInMB to (free space of disk drive) / 1.0E+6)
end tell

then check the resulting number and return the appropriate information. I bypassed almost twenty lines of code with that simple statement. I suppose that's more subject to change than unix shells, but all of the issues I've had thus far with this script has been unix based.

Thanks! You've been so helpful.

Offline

 

#15 2012-03-30 02:42:25 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 1432

Re: Attempting to grep a variable - Possible issue

Hello Stephan

With :

Applescript:


path to startup disk

set currentDrive to POSIX path of result

verifyDriveSpace(currentDrive)

on verifyDriveSpace(drive)
   try
       
       --do shell script "df -h " & quoted form of drive
       set driveSpace to do shell script "df -h | grep " & quoted form of drive & " | awk {'print $4'}"
   on error e
       display dialog e
   end try
end verifyDriveSpace

I get the result :

"309Gi
0Bi
0Bi
0Bi"

which seems curious because
do shell script "df -h '/'"
returns :
"Filesystem     Size   Used  Avail Capacity  Mounted on
/dev/disk0s2  931Gi  621Gi  309Gi    67%    /"

More surprising (at least for me)

If I run the main script replacing $4 by $1, I get :

"/dev/disk0s2
devfs
map
map"

I really don't guess where are the three extraneous values grabbed from.

Yvan KOENIG (VALLAURIS, France) vendredi 30 mars 2012 21:42:17

Offline

 

#16 2012-03-30 02:55:58 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10490
Website

Re: Attempting to grep a variable - Possible issue

df -h without any further parameter returns the information of all partitions including devfs (/dev) map -hosts (/net) and map auto_home (/home)


regards

Stefan

Offline

 

#17 2012-03-31 03:41:07 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 1432

Re: Attempting to grep a variable - Possible issue

StefanK wrote:

df -h without any further parameter returns the information of all partitions including devfs (/dev) map -hosts (/net) and map auto_home (/home)

OK Stephan but it doesn't explain why I get the four values with the complete script.
The log report is :

tell current application
    path to startup disk
        --> alias "Macintosh HD:"
    do shell script "df -h | grep '/' | awk {'print $4'}"
        --> "309Gi
0Bi
0Bi
0Bi"
end tell

It's because I got these values that I tried to get the datas returned before applying grep.
It seems that, to get the value related to boot volume, the device identifier to pass is not slash but /dev/disk0s2

As I can't guess this string, I edited the script this way :

Applescript:


path to startup disk

set currentDrive to POSIX path of result
--set currentDrive to "baseƒebay"
verifyDriveSpace(currentDrive)

on verifyDriveSpace(drive)
   try
       set driveSpace to paragraph 1 of (do shell script "df -h | grep " & quoted form of drive & " | awk {'print $4'}")
   on error e
       display dialog e
   end try
end verifyDriveSpace

This way the handler returns only the first value.

tell current application
    path to startup disk
        --> alias "Macintosh HD:"
    do shell script "df -h | grep '/' | awk {'print $4'}"
        --> "309Gi
0Bi
0Bi
0Bi
904Mi"
end tell
Résultat :
"309Gi"


Yvan KOENIG (VALLAURIS, France) samedi 31 mars 2012 10:40:22

Offline

 

#18 2012-03-31 04:08:32 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10490
Website

Re: Attempting to grep a variable - Possible issue

to clarify

df -h returns

Filesystem      Size   Used  Avail Capacity  Mounted on
/dev/disk0s2   279Gi  148Gi  131Gi    53%    /
devfs          197Ki  197Ki    0Bi   100%    /dev
map -hosts       0Bi    0Bi    0Bi   100%    /net
map auto_home    0Bi    0Bi    0Bi   100%    /home
/dev/disk… other volumes


df -h | grep '/' returns

/dev/disk0s2   279Gi  148Gi  131Gi    53%    /
devfs          197Ki  197Ki    0Bi   100%    /dev
map -hosts       0Bi    0Bi    0Bi   100%    /net
map auto_home    0Bi    0Bi    0Bi   100%    /home
/dev/disk… other volumes

because all lines but the header line contain a slash character

df -h / returns

Filesystem     Size   Used  Avail Capacity  Mounted on
/dev/disk0s2  279Gi  148Gi  131Gi    53%    /

because the slash (startup volume) is a direct parameter of the dh command

df -h | grep '/' | awk {'print $4'}" returns

131Gi
0Bi 
0Bi 
0Bi 

which is the 4th field (Available column) of all lines containing a slash character

btw: the last expression can be replaced with a single awk pipe
df -h | awk '"/" {print $4}'

Last edited by StefanK (2012-03-31 04:30:21 am)


regards

Stefan

Offline

 

#19 2012-03-31 05:37:52 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 1432

Re: Attempting to grep a variable - Possible issue

Thanks Stephan

Yvan KOENIG (VALLAURIS, France) samedi 31 mars 2012 12:37:38

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.044 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)