passing variables to bash script?

Im trying to create a low-fi reminder for students to help us track usage.

the script checks if the screensaver is running - if not it will post a notice.
If nothing happens - it tries again in 5
if the user clears the alert it reminds again in 30

Right now I’ve got an apple script starting the sequence - firing a bash script and then exiting.
The bash script in turn sleeps either 5 or 30 seconds and then fires the applescript again to post the alert and start the process again(obviously I’d be extending these times after testing.

I have it working up intill I try to pass the variable to the bash script as either 5 or 30 seconds:
“error “sh: 5: command not found” number 127”

Anyone know where Im going wrong?


property delay_time : "5"

on run
	my doit()
end run

on doit()
	tell application "System Events"
		if exists process "ScreenSaverEngine" then
			my kill_switch()
		else
			tell application "Finder"
				set imput_result to display dialog "Do us a favor?
	Would you please log your sitting time on this machine into the form found on the front door? Useage records help us advocate for upgrades and new tools." buttons {"OK, I can dig it"} giving up after 30
				if imput_result's gave up or imput_result's button returned is not "OK, I can dig it" then
					my kill_switch(delay_time)
				else if imput_result's button returned is "OK, I can dig it" then
					set delay_time to "30"
					my kill_switch(delay_time)
				end if
			end tell
		end if
	end tell
end doit

on kill_switch(delay_time)
	do shell script ("sh /Scripts/phoenix2.sh >/dev/null 2>&1 & quoted form of delay_time")
	quit
end kill_switch

and the bash


#! /bash
clear

delay_time ="$1"

sleep ($delay_time)
open -a /Scripts/Notice.app
echo "Done with sleeping $delay_time seconds"
exit 0

I have not gone through the code but this should fix the shell script command:

do shell script ("sh /Scripts/phoenix2.sh >/dev/null 2>&1 " & quoted form of delay_time & "")

or

do shell script ("sh /Scripts/phoenix2.sh >/dev/null 2>&1 " & delay_time & "")

Hummm - it seems to have problems

it throws :
“error “The command exited with a non-zero status.” number 2”

I believe this is saying that the process is already running - though I dont seem to recognise it in the Activity Monitor.

Any wisdom?

I would say that it’s better to use quoted form too often than not. So every string is better to create a quoted form of in my opinion because not quoting strings as arguments will give you problems when a strings contains spaces or other shell’s special characters.

So what did you do wrong? Well the problem is that your agruments is passed to the command after yoru redirected the output of the command. You should make the whole command complete and then you can redirect or pipe the output/input of the command. It’s something the CLI demands you do so your command should look like

do shell script "/Scripts/phoenix2.sh " & quoted form of delay_time & " >/dev/null 2>&1 "

EDIT: short tutorial
First I’ve created the following shell script

I’ve saved it directly on the root of my harddrive named showDialogInFinder.
Then I typed in the terminal:

To make the file executable (only for me, change the number to you’re likings).

Then to show a simple dialog in the Finder and in the background (don’t wait for it’s result) then you can use the following command.

do shell script "/showDialogInFinder 'Hello world!' &> /dev/null &"

@DJ Bazzie Wazzie
I appreciate your suggestion with osascript - knew someone would suggest it sooner or later!
But for now I want to make it super simple and go between applescript and bash – for now. I’ll roll it into one later but really wanted to figure it out w/out too many syntax hurdles.

Im still seeing a Non-zero error, though a slightly different number:

error "The command exited with a non-zero status." number 126

Currently the bash script is set to 777 - I just wanted to make sure that permissions were not a problem.

So in short it still errors out - any other ideas?

very confuzzeled :confused:

That’s not why I posted it.

  • It showed you how to pass variables (strings) to the shell script
  • It showed you the correct syntax
  • It showed how you how the syntax in the shell script should do

The only reason I used an osascript is to show an easy example that all other readers can use.

@DJ

I think you misunderstood me – I wasn’t admonishing you for using osascript, knew it would come up.
Fact is though I don’t understand how your example significantly differs from what Im doing - and I’d really like to keep to the example I’ve presented because I get confused by the different syntaxes easily (escaping quotes and line breaks etc.)

Regardless I went ahead and did as your tutorial suggested and frustratingly haven’t been able to get it to work either, so – with apologies I went back to my own script and had a modicum of success - mostly in the bash script.

It seems to be passing the variables from the applescript alert but now is failing to relaunch the applescript that I’ve saved as an app. I’ve tried open and open -a but each time the finder ‘flashes’ a white screen and the script exits without posting the dialogue. – now that’s frustrating! Anyone know why the script is exiting with out executing?

Anyone have any experience with this?

The bash script - I striped out the line that translated the input argument into an internal variable so what I have now is:


#! /bin/bash
sleep $1
open /Users/anachronistic/Scripts/Notice2.app
exit 0

and here is the applescript:


property sleep_time : "5"

on run
	my doit()
end run

on doit()
	tell application "System Events"
		if exists process "ScreenSaverEngine" then
			my kill_switch()
		else
			tell application "Finder"
				set imput_result to display dialog "Do us a favor?
   Would you please log your sitting time on this machine into the form found on the front door? Useage records help us advocate for upgrades and new tools." buttons {"OK, I can dig it"} giving up after 10
				if imput_result's gave up or imput_result's button returned is not "OK, I can dig it" then
					my kill_switch(sleep_time)
				else if imput_result's button returned is "OK, I can dig it" then
					set sleep_time to "10"
					my kill_switch(sleep_time)
				end if
			end tell
		end if
	end tell
end doit

on kill_switch(sleep_time)
	do shell script ("/Users/anachronistic/Scripts/phoenix2.sh " & quoted form of sleep_time & " >/dev/null")
	quit
end kill_switch

The difference is how you’ve made the call. I saw that in your last example it should work all pretty well so the fault must be somewhere else.

Then I’ve tried to save my shell on the root volume as shelltest and chmod to 766 again

Then I launch (or activate) it with a delay of 5 seconds

do shell script "/shelltest 5 &> /dev/null &"

Because this works like a charm and can save the app as application and it is now the same as your script the problem here is solved.

You have an fault if screensaver exists (i’m not sure this is the problem, it’s just another fault than can occur). You can’t call my kill_switch() because it doesn’t have parameters. Because you have save sleep_time defined as a property you don’t have to pass sleep_time to the handler so I would remove the parameters anyway.

DJ that was super helpful.-- For the record Im very appreciative of your time here, thank you.

I wasn’t aware that I couldn’t call a handler like kill_switch if it didn’t have paramaters – but wouldn’t that apply to my doit() as well? Regardless I’ve removed kill_switch from the equation. I should have realised the scope of delay_time so thanks there too.

Basically I believe the problem has turned into a situation where the script won’t exit until the bash has completed - and the bash after it sleeping calls the very script which hasn’t exited! - so a loop with issues. In my search to start a child bash process, I found this technical note from apple:https://developer.apple.com/library/mac/#technotes/tn2065/_index.html#//apple_ref/doc/uid/DTS10003093-CH1-SECTION5 which states:

so now, how do I escape my do shell script so it can run in parallel – releasing the applescript to exit?

do shell script "/Users/anachronistic/Scripts/phoenix4.sh " & quoted form of sleep_time & " >/dev/null "

I did try this but… it isn’t working :wink:

do shell script \"/Users/anachronistic/Scripts/phoenix4.sh \" & quoted form of sleep_time & \" >/dev/null \"

– Im hopeful that after this I’ll have all the pieces needed to fit this together.

Here’s the stripped down version: - I’ve taken out the screen saver part for simplicity.
Note I’ve added ‘tell me to quit’ because I think it was causing the finder to quit rather then the script.

property sleep_time : "5"

on run
	my doit()
end run

on doit()
	tell application "Finder"
		set imput_result to display dialog "Do us a favor?" buttons {"OK"} giving up after 5
		if imput_result's gave up or imput_result's button returned is not "OK" then
			do shell script "/Users/anachronistic/Scripts/phoenix4.sh " & quoted form of sleep_time & " >/dev/null "
			tell me to quit
		else if imput_result's button returned is "OK" then
			set sleep_time to "10"
			do shell script "/Users/anachronistic/Scripts/phoenix4.sh " & quoted form of sleep_time & " >/dev/null "
			tell me to quit
		end if
	end tell
end doit

Same problem exists but realised it was throwing a AppleScript Error 10004
so in


property sleep_time : "5"

on run
	my doit()
end run

on doit()
	tell application "Finder"
		set imput_result to display dialog "Do us a favor?" buttons {"OK"} giving up after 5
		if imput_result's gave up or imput_result's button returned is not "OK" then
			tell current application
				do shell script ("/Users/anachronistic/Scripts/phoenix4.sh " & quoted form of sleep_time & " >/dev/null ")
				tell me to quit
			end tell
		else if imput_result's button returned is "OK" then
			set sleep_time to "10"
			tell current application
				do shell script ("/Users/anachronistic/Scripts/phoenix4.sh " & quoted form of sleep_time & " >/dev/null ")
				tell me to quit
			end tell
		end if
	end tell
end doit

I believe this solves that - but Im not confident that Im sure which application Im telling to quit. Does ‘me’ always mean the script?

reference:http://lists.apple.com/archives/applescript-users/2009/Aug/msg00452.html

Mercy me - I had the answer then I lost it. The back tics in that apple note confused me - easy I know!

the answer is >/dev/null 2>&1 &.

the full line should be

do shell script "/Users/anachronistic/Scripts/phoenix4.sh " & quoted form of sleep_time & " >/dev/null >/dev/null 2>&1 & "

You’re very welcome! I’m glad it all worked out for you.

For the record (for others readers):

is equal to

is equal to

They all send stdout to /dev/null and stederr to /dev/null and runs the command in the background. NOTE: it doesn’t work for named pipes.