Exists in unix

What unix command replaces these:

if folder “Macintosh HD:Users:cirno:Desktop:test:” exists then…
if file “Macintosh HD:Users:cirno:Desktop:test.txt” exists then…

Note: this has very little to do with AppleScript, it is probably better posted elsewhere (also there are answers abound on Google).

If you mean for the (Bourne) shell, then you want these

if [ -d "/Users/cirno/Desktop/test" ]; then # code for when directory exists goes here fi if [ -f "/Users/cirno/Desktop/test.txt" ]; then # code for when file exists goes here fi
Similar to the Finder tests, if a file exists but you do the directory test (and vice versa), it will return false.

The above syntax is for the Bourne shell (/bin/sh, which is actually a copy of bash (the “Bourne again” shell) on current Mac OS X systems). The syntax will be slightly different for other “UNIX” environments and/or programming lanauges (C shell (e.g. tcsh), awk, Perl, Python, Ruby, C/C++/Objective-C, etc.).

Thanks. I’m actually using this inside AppleScript. I want to replace as many as possible tell application “Finder”… commands with unix-commands. Now i get too frequently “Finder is busy” etc errors.

I tried this but it fails:

do shell script "if [ -d "/Users/cirno/Desktop/test" ]; then"
--# code for when directory exists goes here
do shell script "fi"
do shell script "if [ -f "/Users/cirno/Desktop/test.txt" ]; then"
--# code for when file exists goes here
do shell script "fi"

This is a real bad idea, shell script calls are quite slow.
To check if a file exists without the Finder is better and much faster with

checkFile("MacHD:Users:me:test.txt")

on checkFile(f)
	try
		f as alias
		return true
	on error
		return false
	end try
end checkFile

You have to put all the commands into one script (and properly quote that path):

set filePath to POSIX path of ((path to desktop as Unicode text) & "test.txt")

do shell script "if [ -f " & quoted form of filePath & " ]; then echo 'do'; echo 'whatever'; fi;"

See also: Technical Note TN2065: do shell script in AppleScript

I’m not sure if it would be significantly slower if you were going to use do shell script anyways. (Though there is nothing wrong with doing what you suggested.)

We had a AppleScript/command line forum before, but collapsed it into this one.

But I am, Bruce. :slight_smile:

Here a comparison with Nigel’s lotsa routine,
on my machine with a loop of 50 times the subroutine is 60 times faster!
Note: lotsa requires the GetMilliSec OSAX (PPC only)

timeLotsa(50) -- Nigel Garvey's "Lotsa" routine for timing.
-- {1.085, 0.018, 60.277777777778, 0.016589861751}

-- The Lotsa handler for comparison

on timeLotsa(lotsa)
	set filePath to POSIX path of ((path to desktop as Unicode text) & "test.txt")
	repeat lotsa times
	end repeat
	
	-- Test 1.
	set t to GetMilliSec
	repeat lotsa times
		do shell script "if [ -f " & quoted form of filePath & " ]; then echo 'do'; echo 'whatever'; fi;"
		
		
	end repeat
	set t1 to ((GetMilliSec) - t) / 1000
	
	-- Test 2.
	set t to GetMilliSec
	repeat lotsa times
		checkFile(filePath)
	end repeat
	set t2 to ((GetMilliSec) - t) / 1000
	
	-- Timings.
	return {t1, t2, t1 / t2, t2 / t1} -- both ratios if you don't know which is faster
end timeLotsa

on checkFile(f)
	try
		f as alias
		return true
	on error
		return false
	end try
end checkFile

Unfortunately, I can’t use GetMilliSec on my Intel-based machine.

In your test, of course it would be faster; However, that wasn’t my point.

Would you alter your second test and let know the results?

-- Test 2.
set t to GetMilliSec
repeat lotsa times
	checkFile(filePath)
	do shell script "echo 'do'; echo 'whatever'"
end repeat
set t2 to ((GetMilliSec) - t) / 1000

it takes almost the same time, the subroutine is a bit faster

{1.212, 1.146, 1.057591623037, 0.945544554455}