The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   Scripting the Terminal (http://hintsforums.macworld.com/showthread.php?t=1074)

kerim 02-19-2002 08:19 PM

Scripting the Terminal
 
Can someone help me understand how to script the terminal? I am used to applescript, but the following script (some information has been replaced with ...., but it should be clear) doesn't work:

tell application "Terminal"
activate
do script with command "cd /Users/..../Music/'Saved Streams'"
do script with command "streamripper http://.....:8000/"
end tell

The problem is each "do script with command" line opens a new terminal window, so that the second command is actually executed at the root directory! Now, I know that there is a -d command in streamripper to specify the default directory, but I would like to know how to do this for other scripts as well...

mervTormel 02-19-2002 08:25 PM

you could execute streamripper with the full paths?

or, you could do multi-shell commands with semicolon e.g., command1 ; command2

or, better yet, you might want to put all your shell commands in a shell script and get that shell script working good and then call that shell script with applescript.

kerim 02-19-2002 09:01 PM

Thanks. Are there any good internet sites that explain shell scripting to newbies in clear and simple language with examples?

kerim 02-19-2002 09:17 PM

The Script
 
FYI:

Anyone who wants to install a simple program to record *free* mp3 streaming software on their computer should download this:

http://www.versiontracker.com/morein...d=12506&db=mac

And then use this simple script:

set the_url to "ENTER URL HERE"
set dest_folder to "PATH TO FOLDER HERE'"

tell application "Terminal"
activate
do script with command "streamripper " & the_url & " -d " & dest_folder
end tell

To get the URL, just get info on a PLS file and click on "EDIT URL".

If you want to listen while recording, simply add

& " -r"

to the end of the script command line. Then you can listen by pointing iTunes to "http://localhost:8000" ...

cheers.

mervTormel 02-19-2002 09:19 PM

good, clear, simple? nope. there's lots of bad examples, tho :D

but even a good shell script can be as simple as a series of simple commands.

why don't you scheme your script here and maybe we can wrangle something together for you and get you going, yeehaw.

--
if it were easy, we wouldn't call it code.

kerim 02-19-2002 09:32 PM

I mean, how do you go about writing, saving, and accessing a shell script. Say with the above command as a one-line script example. I know how to do it in Applescript (as listed above) but I have no idea how one would do it as a "shell script".

What is the basic format?

Thanks!

mervTormel 02-19-2002 10:03 PM

kerim,

easiest right now? hope this don't scare you off, and this may not work too very good; i haen't play'd w/ streamripper yet.

generally, a script is just a text file of UNIX commands and shell commands. this'll get you started...
Code:

% cd              # get home
% mkdir bin      # create your bin dir
% pico bin/sr.sh    # pico text editor, use your favorite, enter commands

#!/bin/sh

# file: sr.sh - my streamripper shell script

set url='http://pathToStream/'
set dest='/pathTo/DestinationDir/'  # make this dir, right?

/usr/local/bin/streamripper $url -d $dest

^X

% chmod u+x bin/sr.sh  # make your script executable

% rehash  # onetime rebuild shell's command dir tables for new script
% sr.sh      # test it

looking back at that, it prolly won't work, but later, we can make it more robust and pass a url into it.

kerim 02-20-2002 06:46 AM

I believe I followed the instructions correctly. However, I get the following error after entering the last line:

sr.sh: Command not found.

?

sao 02-20-2002 12:28 PM

Kerim, have you tried :

StreamCatcher *-* 1.1

Record MP3 audio streams (GUI for streamripper) by Mike Bombich.

http://www.versiontracker.com/morein...d=13631&db=mac

Cheers...

kerim 02-20-2002 06:01 PM

Yes. It doesn't seem to work. Nice interface though. There is also a $10 shareware program that *does* work with an even nicer interface that is called iNet Stream Archiver - this doesn't even require a StreamRipper install, but it seems to do the same thing. But the terminal commands are pretty simple and the applescript code listed above works well so there is no real need for me. However, I would like to know how to write schell scripts and this seems like a good project to learn with. Unfortunately, I haven't had any luck with the above directions ...

kerim

rusto 02-20-2002 06:53 PM

Why call the terminal at all? I've been executing shell scripts merely by using this in AppleScript:

do shell script "blah blah blah blah"

no need for


tell application "Terminal"
activate
blah blah blah blah
end tell

kerim 02-20-2002 09:10 PM

Learn something new everyday.... Thanks!

kerim 02-21-2002 09:19 PM

"do shell script" doesn't work
 
I tried changing the script to "do shell script" but I get an error telling me:

"zsh: command not found: streamripper"

Any ideas?

mervTormel 02-21-2002 09:35 PM

kerim,

in scripts, use the full path to the command

do shell script "/full/path/to/streamripper blah blah"

please refer to some excellent articles at http://www.macosxhints.com/ about scripting the shell.

rusto 02-21-2002 09:43 PM

When you use "do shell script" you are actually accessing the sh shell and not the tcsh one you are probably using via the terminal. Somehow you have to let the sh shell know where to find the "streamripper" command.

My guess is you have to add it to your PATH...

-------------------

merv slipped in his (more likely correct) post just as I finished this one up...I'd go with his advice.

kerim 02-21-2002 09:48 PM

Thanks, for those not in the know, you can get the full path to a command by typing this in the terminal:

which streamripper

Unfortunatly, while it works, there is a problem with doing the command this way: it doesn't give you a way to stop the script. When run via the terminal you can quit by going to the terminal and hitting control-C, but this way you have to kill the script. I suppose it would be possible to write a dialog box of some kind ...

rusto 02-21-2002 10:06 PM

see this thread where a bunch of us had fun working on an applescript to launch/kill a CLI app:

http://forums.macosxhints.com/showth...ht=applescript

It wouldn't be hard to adapt to your purpose.

sean 03-04-2002 03:30 PM

i think this is how to script the terminal without opening it and making a startup script:

paste this into script editor and save as an application, set it as never show startup screen, and then set it as a log in item

--#1 THIS IS WITH A PROMPT FOR YOUR ADMIN PASSWORD(SECURE TEXT BOX)
on run
do shell script "sudo rm /var/slp.regfile" with administrator privileges
end run

--#2 THIS IS WITH YOUR PASSWORD HARDCODED
on run
do shell script "sudo rm /var/slp.regfile" with password "password123"
end run

example 1 -- i get a little bouncing applescript icon in my dock and i click on it and it asks for the password, but doesn't work. i think i am close, though.

example 2 -- i get errors before i ever save as an application. hmmm. i tried without the word with, too, but didn't work. anyone?

rusto 03-04-2002 04:14 PM

tweak
 
Try it this way:

do shell script "sudo rm /var/slp.regfile" password "password123" with administrator priviledges

kerim 03-04-2002 09:29 PM

Tip on MacFixit.com
 
This appeared today on Macfixit.com!

Creating a double-clickable Terminal script

Using SETI@Home as an example, Remo Del Bello shares this tip for creating scripts that can be saved and launched from the OS X Finder. Although SETI@Home is the basis for this example, this can work for any task that requires typing a series of commands into the Terminal. He writes:


"I like running the SETI@Home darwin client since it is so much faster than the OS X 'screensaver,' and wastes less cycles. I really missed seeing the progress, so I would fire up Terminal, cd to the SETI@Home directory, run setiathome with the verbose flag and leave the window open. Eventually I got tired of typing all of that every time so I made the following little shell script:


#!/bin/sh
cd ~/Applications/setiathome-3.03/;
./setiathome -verbose



Seeking a way to avoid manually opening a Terminal window and typing the name of the script, I set out to find a way to make this script double-clickable. It turns out to be very simple to do. All you have to do is append '.command' to the end of the filename of an executable script. Alternatively, you can Get Info on the script in the Finder and select Terminal as the application to Open With if you don't want to change the name of your script. I now have a file called 'seti.command' that I just double-click and the whole thing is taken care of. Since I've discovered this I've made a number of shell scripts that I can just double-click to run and it is very handy."


All times are GMT -5. The time now is 06:09 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.