A reader writes:
I’ve started to go through your shell script book Wicked Cool Shell Scripts but there is still something I’m unclear on. I’m showing my ignorance here, but how
exactly do you save a shell script so you can use it later?”
If you’re comfortable using the vi editor within the Mac OS X environment, your best bet is to open up a Terminal window (Applications -> Utilities -> Terminal) and type in the script directly.
If not, you can use an application like BBEdit, save the file as text-only with or without line breaks, then open up the Terminal application and type in the name of the script, as in sh myscript.sh (assuming you saved the file as ‘myscript.sh’).
The final step is for you to actually make the script executable, which you can do by using the chmod command, as in: chmod +x myscript.sh.
Once you’ve done that – particularly if you have the script in one of the directories in your PATH – you can execute the script by simply typing its name at the command line.
re: script file location, you mention “…if you have the script in one of the directories in your PATH”. Is there a common/typical/usual directory where people store their script files on Mac OS X?
My current path is this: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:
So I’m wondering if I should store my script files in one of those directories or create a new one and add it to the PATH.
Thanks!
Colin,
Just off the top of my head, here are three possibilities for why your AppleScript script that uses the “Do Shell Script” AppleScript command isn’t working:
o Using a different shell in Terminal vs. in Do Shell Script; or
o Using a different starting directory in Terminal vs. in Do Shell Script; or
o Using different I/O redirection in Terminal vs. in Do Shell Script.
Failing the above, I hope that this “application note” for the Do Shell Script command in AppleScript will get you pointed in the right direction. It should be REQUIRED SKIMMING for anyone who uses the Do Shell Script command:
Technical Note TN2065
do shell script in AppleScript
http://developer.apple.com/library/mac/#technotes/tn2065/_index.html
Over time, the above link might fail because of “link rot.” If that happens, I suggest searching Apple’s Developer website on “tn2065” (without the quotes).
I have found that wrapping an Unix command or Unix shell script invocation command (the name of the script) in an AppleScript is a great–and quick–way to turn any shell script or Unix command into an OSX application. Just save it out of the AppleScript Editor, being sure to select the “Application” choice for the “Format” on the drop-down menu in the “Save” dialog box. That will create an OSX application that can be double-clicked to launch, can be put into your Dock, etc, and can be generally used just the same as any other OSX application.
-B.
I have to admit that I am completely clueless about Applescript, Colin. Perhaps someone else can explain?
Quick question, but there’s a back story. I’m trying to write a shell script that will enter a terminal command, specifically something that opens a file using Python. I wrote a text file and saved it as a .sh file, then used the “open with” feature to always open it with terminal. It works.
But now I’m curious about this Applescript thing. Question is: why does break when it tries to use the “python” command as I use in Terminal?
You can also use Platypus to create a native Mac app from a shell script.
Hi, I just released a source via github called ‘bashapp’ that will compile your bash script into a binary (C source) and also create a Mac application that you can click on from Finder. It’s here, if anyone wants to take a look.
https://github.com/arbinger/bashapp
Thanks, James
hi, how about make a script to execute the command
java -Xincgc -Xmx1G -jar crafty.jar
in the file user/desktop/server?
Hi. Great help with the comment above, just on this comment (in apple-script) zou need double-quotes:
as in
do shell script “echo hi”
instead of
do shell script ‘echo hi’
Set it up so you can double-click to launch it, then Control-Click on it and choose “Open with…” TextEdit (or similar) and you should be good…
How can I have the best of both worlds?
1. I double-click on my new batch-file to run it.
2. I to quickly edit that same batch-file.
Do I have to constantly change the permissions back and forth?
Or constantly rename the file *.bat to *.txt back and forth?
I am new to the terminal and my work is letting do some work from home.
I am suppose to add some lines in my local host terminal to allow me access my work environment.
I have the lines I am suppose to add below my
127.0.0.1 localhost to allow me access to my work environment.
But I am lost as how to actually insert them into the terminal below the 127… localhost.
How do I add a couple lines of text below the 127. … localhost ?
I have tried looking on the internet, but to no avail.. I now know how to do lots of other neat commands except how to enter text into my terminal.
Do I have to use the vi thingy or open up a text editor and type it in there and then paste it into the terminal.
Thanks for your help in advance.
The newbie.. 🙂
Kiel, I don’t know that script, but generally it sounds like your current directory isn’t in your PATH. Try this:
./todo.sh
and see if that works!
Hey, I’m trying to run the todo.sh script (found https://github.com/ginatrapani/todo.txt-cli/wiki/User-Documentation). I can get everything set up, and I run:
sh todo.sh
chmod +x todo.sh
But then, when I try and run todo.sh, I get:
-bash todo.sh: command not found
Do I need to run something else in order to get this to run in a bash shell? Or is there some step I’m forgetting?
I am using geektool for mac and wrote a script to ping my work server but I want to be able for that script to ping all our servers but display in one shell.
Is it possible? Here is my basic script:
HOST=server name
PING=`ping -q -c 1 $HOST`
if [[ $? -eq 0 ]] ; then
TIME=`echo $PING |tail -1 | cut -d/ -f 5`
echo -n ${TIME}ms away from $HOST
exit 0
else
echo -n $HOST could not be reached
exit 2
fi
feel free to email me leigh.kalbli@me.com
Hello,
I hope that you are able to assist me.
I am running a WebSTAR server on an old Macintosh G4 500 MHz DP mini-tower running Tiger 10.4.11. One of the websites that I operate requires using PHP and a mySQL database.
While Tiger has PHP 4.4.9, I am using a self-contained version of PHP 5.2.0 which is installed in:
/Applications/PHP5/bin/this is where the actual php binary is located
This is a self-contained PHP package that I downloaded from the Zend website.
The problem is that PHP crashes several times throughout the day. I don’t know if it is a result of so many search bots hitting the site, or people who are maliciously attacking the site, or what, but the constant PHP crashing is annoying, and makes the site unreliable.
Currently, the way I have dealt with the PHP crashing problem is by keeping “Activity Monitor” open on my desktop, as well as the Terminal. Whenever I see PHP disappear from “Activity Monitor”, I follow the instructions provided by 4D — the makers of WebSTAR — and type the following in the Terminal to restart PHP:
set env PHP_FCGI_CHILDREN 8
./fcgirunner.pl xyz ./php
The x’s are the port number.
“fcgirunner.pl” is an old PERL script released by Zeus Technology to startup remote FastCGI processes with their Zeus Web Server.
As you can imagine, it is a hassle to have to keep an eye on PHP, and reboot it manually every time that it crashes, and I do have to sleep. With that in mind, I am interested in finding a way to automatically watch the PHP binary that is located at the above path, and to automatically relaunch the PHP binary in fcgi mode by using the above two Terminal commands, without having to keep “Activity Monitor” and the Terminal open all the time.
In short, I need a solution which will do two things:
1. Keep an eye on the self-contained PHP installation.
2. Automatically reboot the PHP binary in fcgi mode — as per the above two Terminal commands — every time that it crashes.
Someone on another site suggested that I use a shell script, but I have basically no experience with such things. They suggested that I begin with something like the following, but to be honest, it is all Greek to me:
=============================
[CODE]
#! /bin/bash
ofPid=’/bin/ps -ax | grep php | grep -v pidcheck.sh | grep -v grep’
if [[ -n “$ofPid” ]]
then
exit 0;
else
/Applications/PHP5/bin/fcgirunning.pl
fi
exit 0;
[/CODE]
=============================
Another person suggested that I use “launchd with the KeepAlive key enabled”, but I have no idea whatsoever how to do that. They offered this example:
=============================
[CODE]
Label
my.textedit.job
KeepAlive
ProgramArguments
/Applications/TextEdit.app/Contents/MacOS/TextEdit
[/CODE]
=============================
They also pointed me to the launchd manual on the Apple site, and to a page about daemons and agents, but again, it is way over my head.
What would be perfect would be a shell script enclosed in an Applescript — as you described in a previous comment — which I could save as an application, and then add to my startup items. That way, the minute I start up the G4, PHP will automatically be launched, and the script will keep an eye on it.
It would be necessary for this script to check the PHP process every minute to make sure that it is still running, as otherwise, if PHP crashes, visitors to the site just get a blank, white page.
Please bear in mind that I am not very proficient at using the Terminal, and have very little experience at writing Applescripts, or any other scripts. So, if you offer that kind of a solution, you will need to provide me with very clear step-by-step instructions.
Thanks so much in advance.
BTW, the fcgirunner.pl PERL script is located at the same location as the php binary in /Applications/PHP5/bin/.
The script must start fcgirunner.pl, watch port 8081, and start PHP when it crashes.
Not a dumb question at all, Grant, but I really can’t see how running an iChat server would have any impact whatsoever on creating a separate script…
this maybe a dumb question, do you have to stop ichat server before executing this script?
thanks,
An easier way to execute these scripts is to put them into an applescript. This way, they can be saved as an application that you can add to your dock etc.
To do so:
Open script editor (in the utilities folder in Snow Leopard, in the applescript folder of applications in 1.5 or before)
enter:
do shell script
then contain the contents of your script between two single quotes
ex.
do shell script ‘echo hi’
then you can save this file as an application through the save menu
Hello,
write this on a text doc and save file as ‘remove_cache.sh’:
#!/bin/bash
rm /Library/Application Support/Shutterbypass/cache.db
exit
Then select it from Finder and cmd+i to get file info. On ‘Open With’ go browse and select Terminal.app (/Applications/Utilities).
Then open Terminal.app and write:
chmod +x (and drag from finder the script ‘remove_cache.sh’)
That’s all, when double click the script, it will delete the cache.db.
Salut,
Xapa
Hello,
How can I create one that will delete the file “cache.db” in the directory “Library/Application Support/Shutterbypass” I was hoping there was a way to create a file that i can save to my desktop and just double click on it and this will happen automatically. Like a batch file in Windows.
Thank you,
Soheb V