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.


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

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


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

The timeout block can simplified


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

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.

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

Thanks for your help thus far!
-Jason

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

Even in the Terminal you should write


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

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

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 :stuck_out_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.

just an example with a bit of error handling


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

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


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’.

maybe there is a misunderstanding


set tempVar to "Lexar Media"

the quotes indicate the literal string, nothing else


quoted form of "Lexar Media"

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

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

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

Hello

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



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

Yes it’s normal :wink:

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


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

I’m actually now getting “Variable driveSpace is not defined.” from

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.

You’re passing a list of lists {{.}} instead of a string

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

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.

Hello Stephan

With :


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

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 :


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

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}’

Thanks Stephan

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