The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - Newcomers (http://hintsforums.macworld.com/forumdisplay.php?f=15)
-   -   Running Perl scripts (http://hintsforums.macworld.com/showthread.php?t=87952)

berone 03-30-2008 11:53 PM

Running Perl scripts
 
I can't get Perl scripts to run is the root of the problem. I have a perl script that runs on my Windows laptop but I cannot get it to run on my mac mini. It's not that this script won't run - I can't get any scripts to be recognized. If I navigate to the directory that has the script and execute it there I get "command not found". Some reading led me to believe that I needed to add the Perl location path to my .profile file. But I don't seem to have one. Most of the information I've found on forums for Perl say "OS X has perl built in and script just run". I can't find out what to do if they don't.

HELP!

Thanks,

Anthony

hayne 03-31-2008 12:06 AM

1) Please show us one of the scripts you are trying to run. And show us (via copy & paste from the Terminal window) how you are running it and what results you get. I.e. don't tell us what happens - just show us. It would also be good if you showed us the results of the following commands just before you run the script:

id
pwd
ls -l name_of_script

2) See the section on running scripts in this Unix FAQ

berone 03-31-2008 12:56 AM

Thanks for the swift reply. The scripts were written by someone on the Tivo Community Forum and are for downloading and re-encoding files from a Tivo using various 3rd party software. I don't necessarily expect them to execute properly without modification.

Script:
#!/usr/bin/perl
### AT BARE MINIMUM ADJUST: $MAK, %TIVOS, $baseDir
################################################################
### REQUIRED CONFIGURATION ###
# Define your MAK & Tivo URLs
$MAK = "0642072585";
%TIVOS = (
"willow" => "192.168.1.7"
);

# Location of required 3rd party tools needed
$baseDir = "c:\\kmttg";
$tivodecode = "$baseDir\\tivodecode\\tivodecode.exe";
$curl = "$baseDir\\curl\\curl.exe";

# Define location where all output files will go (defaults to baseDir)
$outputDir = "k:\\Tivo";

### OPTIONAL CONFIGURATION ###
# Encoder specification - supply complete command and use INPUT & OUTPUT in place of input and output file names
# This example uses ffmpeg with a config optimized for Cowon A3 portable media player
$encoder = "$baseDir\\ffmpeg\\rev11870\\ffmpeg.exe";
$encoder_args = "-i INPUT -acodec libfaac -ab 128k -vcodec mpeg4 -b 1200k -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 480x320 -title X -f mp4 OUTPUT";
# Equivalent options using mencoder
#$encoder = "$baseDir\\mencoder\\mencoder.exe";
#$encoder_args = "-ovc xvid -xvidencopts bitrate=1600:trellis:max_bframes=2 -vf scale=800:480 -ofps 29.97 -oac mp3lame -lameopts abr:br=128 -channels 2 -srate 48000 -af lavcresample=48000 -of avi -o OUTPUT INPUT";
$encodedFileExtension = ".mp4";

# comskip binary location and ini file
$comskip = "$baseDir\\comskip\\comskip.exe";
$comskipIni = "$baseDir\\comskip\\comskip.ini";

# mencoder used in comcut step to remove commercials detected by comskip
$mencoder = "$baseDir\\mencoder\\mencoder.exe";

# WAN http Port
#$wan_http_port = "7777";
################################################################


Other requested information:

mini:~ Mini$ id
uid=501(Mini) gid=501(Mini) groups=501(Mini), 81(appserveradm), 79(appserverusr), 80(admin)
mini:~ Mini$ pwd
/Users/Mini
mini:~ Mini$ ls -l name_of_script
ls: name_of_script: No such file or directory
mini:~ Mini$ config.pl
-bash: config.pl: command not found
mini:~ Mini$ cd /applications/kmttg
mini:/applications/kmttg Mini$ config.pl
-bash: config.pl: command not found
mini:/applications/kmttg Mini$


Thanks,
Anthony

acme.mail.order 03-31-2008 01:53 AM

Quote:

Originally Posted by berone (Post 461377)
If I navigate to the directory that has the script and execute it there I get "command not found".

Windows has the current directory as part of the program path, Unix usually doesn't. If you are navigating to a directory and just typing the script name it won't work. But typing the "current directory" marker followed by the script name will. e.g.:

cd path/to/folder
myscript

WON'T work, but

cd /path/to/folder
./myscript

will. You've also checked that the file has the execute bit set ( chmod +x myscript ) and that the VERY first line of the file says

#!/usr/bin/perl

? Windows doesn't care about these things but Unix does. Conversely, Unix doesn't care about the file extension.

hayne 03-31-2008 02:19 AM

I think acme.mail.order has got the likely answer - you are trying to run the script just by typing its name, but the execution PATH does not include the current directory.
See the section of the above referenced Unix FAQ about setting up your PATH.

fracai 03-31-2008 09:24 AM

Also note that the script makes references to external applications (ffmpeg.exe, tivodecode.exe, curl.exe) and uses Windows path conventions (\). You'll need to get copies of ffmpeg and tivodecode (curl is included with Mac OS X) and correct all references to paths which include backslashes.
There's also a reference to the basedir as c:\\... You'll need to adjust that as well, as the reference to the c drive will fail on the Mac.

berone 03-31-2008 09:39 AM

Nope, not getting it. I seem to be having problems with the path. The directions on setting the path say to edit the .profile file, but I can't find it. This is what I got when I tried to check what the current path is:

mini:/ Mini$ echo $path

mini:/ Mini$


Does that mean I don't have a path?

Thanks,
Anthony

berone 03-31-2008 09:42 AM

fracai,
Thanks. I assumed that I would have to modify aspects of the script to run on the Mac (although it apparently runs fine on Linux), but I haven't gotten that far yet! I've got the third party apps and all of them are capable of running on OS X so I'm hoping to be able to get the whole package working.

Anthony

acme.mail.order 03-31-2008 10:00 AM

I'm out of order above - the post with the code must have been delayed somewhere.

Quote:

Originally Posted by berone (Post 461391)
I don't necessarily expect them to execute properly without modification.

Good, 'cause that aint' happening :rolleyes:

Code:

#!/usr/bin/perl
This part is correct

Quote:

Other requested information:
mini:~ Mini$ config.pl
-bash: config.pl: command not found
What I said above. "config.pl" won't work from the current directory, but "./config.pl" or "perl config.pl" will.

Quote:

mini:~ Mini$ cd /applications/kmttg
mini:/applications/kmttg Mini$ config.pl
You have an /Applications AND /applications folders??? That's a recepie for trouble in the future. Don't depend on case sensitivity.

Quote:

Originally Posted by fracai (Post 461447)
Also note that the script makes references to external applications (ffmpeg.exe, tivodecode.exe, curl.exe) and uses Windows path conventions (\). You'll need to get copies of ffmpeg and tivodecode (curl is included with Mac OS X) and correct all references to paths which include backslashes.
There's also a reference to the basedir as c:\\... You'll need to adjust that as well, as the reference to the c drive will fail on the Mac.

Yup. Open it in TextEdit and replace "\\" with "/". Then manually tweak all program paths. Happy editing!!

berone 03-31-2008 11:11 AM

Okay, "perl config.pl" executes (doesn't work, as expected, but it executes). "./config.pl" does not. I'm still thinking it's a path problem, per my previous post. Also, is there a better script editor for OS X than TextEdit? Something that will give me scripting format and line numbers? It's going to be hard to run down some of the errors if I have to keep counting lines!

No, I don't have an "Applications" and "applications" folder. Is it case sensitive? cd /applications brings me to my Applications folder.

Thanks for all the help!

Anthony

hayne 03-31-2008 06:27 PM

1) See the above referenced Unix FAQ for some recommendations on text editors.

2) OS X's filesystem is case-insensitive but case-preserving.
It is a good idea to use the correct capitalization in scripts etc.
Hence use "/Applications"

3) You should start with a "hello world" script and only proceed to your actual script once you've got the basics working.

Here's a sample:
Code:

#!/usr/bin/perl

print "Hello World\n";

One thing that might be biting you is the line-endings. Your Perl script needs to have Unix-style line-endings. Again, see that Unix FAQ.

acme.mail.order 03-31-2008 07:18 PM

Quote:

Originally Posted by berone (Post 461466)
Okay, "perl config.pl" executes (doesn't work, as expected, but it executes). "./config.pl" does not. I'm still thinking it's a path problem,

the "./config.pl" format eliminates the path problem, as you dpecify the path with ./ (current directory). Is the execute bit set? Type

ls -l config.pl

(two lower-case "L", not number one)
The cluster on the left should look like

-rwxr-xr-x

If there are no e"X"ecute flags, type:

chmod +x config.pl

berone 03-31-2008 09:41 PM

Thanks, all. I don't have it working yet, but at least I have a framework for approaching it. I downloaded TextWrangler and that's exactly what I was looking for. I'm going to wade into it now and see what I can figure out using the UNIX FAQ and what I've learned here. I'm sure you'll be hearing from me again!

Thanks,
Anthony

berone 03-31-2008 11:23 PM

That didn't get too far. Can't figure out how to install the Tk module. Something about cpan, but I'm not getting it. I read that I could download an rpm but the link was broken and I can't find one elsewhere. Help?

Thanks,
Anthony

hayne 03-31-2008 11:32 PM

Quote:

Originally Posted by berone (Post 461601)
That didn't get too far. Can't figure out how to install the Tk module. Something about cpan, but I'm not getting it. I read that I could download an rpm but the link was broken and I can't find one elsewhere.

1) I'm not sure why you need Tk for that script - I don't see it being referenced in the script.

2) I usually get Perl modules via CPAN by using the following command:

sudo perl -MCPAN -e shell

It will ask you a bunch of setup questions, then you can use it to download & install modules. Read the help.

3) I don't think an RPM will help - those are usually just for Linux.

4) An alternative to using CPAN for Perl modules is to get them via Fink. See the section on open-source programs for pointers about Fink (and MacPorts).

berone 04-01-2008 09:00 AM

I tried the directions you gave me and, after a long scrolling of things I didn't understand, got to this:

/usr/bin/tar: Read 9216 bytes from -
Removing previously used /Users/Mini/.cpan/build/Tk-804.028
Can't make directory /Users/Mini/.cpan/build/Tk-804.028 read+writeable: Operation not permitted at /System/Library/Perl/5.8.6/CPAN.pm line 3947
Can't read /Users/Mini/.cpan/build/Tk-804.028: Permission denied at /System/Library/Perl/5.8.6/CPAN.pm line 3947
Can't remove directory /Users/Mini/.cpan/build/Tk-804.028: Directory not empty at /System/Library/Perl/5.8.6/CPAN.pm line 3947
and can't restore permissions to 0751
at /System/Library/Perl/5.8.6/CPAN.pm line 3947
Couldn't rename Tk-804.028 to /Users/Mini/.cpan/build/Tk-804.028: Permission denied at /System/Library/Perl/5.8.6/CPAN.pm line 3948
CPAN::Distribution::get('CPAN::Distribution=HASH(0x7d5af0)') called at /System/Library/Perl/5.8.6/CPAN.pm line 4464
CPAN::Distribution::make('CPAN::Distribution=HASH(0x7d5af0)') called at /System/Library/Perl/5.8.6/CPAN.pm line 4704
CPAN::Distribution::test('CPAN::Distribution=HASH(0x7d5af0)') called at /System/Library/Perl/5.8.6/CPAN.pm line 4809
CPAN::Distribution::install('CPAN::Distribution=HASH(0x7d5af0)') called at /System/Library/Perl/5.8.6/CPAN.pm line 5496
CPAN::Module::rematein('CPAN::Module=HASH(0x372b574)', 'install') called at /System/Library/Perl/5.8.6/CPAN.pm line 5560
CPAN::Module::install('CPAN::Module=HASH(0x372b574)') called at /System/Library/Perl/5.8.6/CPAN.pm line 2138
CPAN::Shell::rematein('CPAN::Shell', 'install', 'Tk') called at /System/Library/Perl/5.8.6/CPAN.pm line 2165
CPAN::Shell::install('CPAN::Shell', 'Tk') called at /System/Library/Perl/5.8.6/CPAN.pm line 201
eval {...} called at /System/Library/Perl/5.8.6/CPAN.pm line 201
CPAN::shell() called at /usr/bin/cpan line 193"



I need the Tk module for a different script - the author rewrote the program to eliminate the config.pl.

Thanks,

Anthony

hayne 04-01-2008 12:57 PM

Are you using 'sudo' in front of the 'perl -MCPAN -e shell' command?

The other thing to check is if the CPAN module itself needs to be updated. I thought you usually got prompted for that at the beginning of the CPAN shell, but maybe you need to request it.

berone 04-01-2008 01:22 PM

Yes, I used sudo. I tried it again and it did check for an update. It told me to enter "install Bundle::CPAN" which I did. At the end of that it gave me this:

Writing Makefile for Mac::Types
Writing Makefile for Mac::Carbon
-- NOT OK
Running make test
Can't test without successful make
Running make install
make had returned bad status, install seems impossible
Running make for A/AD/ADAMK/File-HomeDir-0.69.tar.gz
Is already unwrapped into directory /Users/Mini/.cpan/build/File-HomeDir-0.69

CPAN.pm: Going to build A/AD/ADAMK/File-HomeDir-0.69.tar.gz

-- NOT OK
Running make test
Can't test without successful make
Running make install
make had returned bad status, install seems impossible

I was asked for some host names that I didn't know the answers to, so I just took the defaults. Then I tried "install Tk" again. At the end this time I got this:


/usr/bin/perl is installed in /System/Library/Perl/5.8.6/darwin-thread-multi-2level okay
PPM for perl5.008006
Test Compiling config/perlrx.c
Test Compiling config/pmop.c
Test Compiling config/pregcomp2.c
Test Compiling config/signedchar.c
Test Compile/Run config/unsigned.c
Test Compiling config/Ksprintf.c
Test Compiling -DSPRINTF_RETURN_CHAR config/Ksprintf.c
Test Compiling config/tod.c
Test Compiling -DTIMEOFDAY_TZ config/tod.c
Test Compiling -DTIMEOFDAY_NO_TZ config/tod.c
Test Compiling -DTIMEOFDAY_DOTS config/tod.c
Problem gettimeofday()
Using -L/usr/X11R6/lib to find /usr/X11R6/lib/libX11.dylib
Cannot find X include files via /usr/X11R6/include
Cannot find X include files anywhere at ./myConfig line 369.
Compilation failed in require at Makefile.PL line 37.
BEGIN failed--compilation aborted at Makefile.PL line 39.
Running make test
Make had some problems, maybe interrupted? Won't test
Running make install
Make had some problems, maybe interrupted? Won't install

Thanks,

Anthony

hayne 04-01-2008 02:39 PM

The error messages regarding 'make' lead me to think that perhaps you don't have the Apple Developer Tools installed.
If not, see the section of the above referenced Unix FAQ about compiling open-source software.

berone 04-01-2008 05:34 PM

You're right -I didn't. Now I do. Still getting this error:

Running make test
Can't test without successful make
Running make install
make had returned bad status, install seems impossible

I'm going to go bang my head on a brick wall for a while now.

Anthony

hayne 04-01-2008 06:06 PM

1) You might want to clean out your existing CPAN setup (the .cpan directory) and start again since CPAN is assuming that you have the dev tools installed, so maybe some artifact from your previous attempt is causing problems.

2) Don't forget that another option is to get the modules via Fink.

berone 04-02-2008 12:00 AM

That did it! Now I can set about getting the script to function.

Thanks!
Anthony

berone 04-04-2008 02:21 AM

Thanks for all the help. The script ran almost without edit once I figured out to run it from X11 instead of terminal. The only problem I'm having with it now is that it won't download files over about 400mb, but I don't think that's a perl problem.

Thanks again!

Anthony

berone 04-09-2008 02:31 PM

I got the script functioning properly - thanks for all the help. I have one more question: I want to be able to launch the script from an icon. I know that means that I have to wrap it in AppleScript or Platypus. The problem is that I can't figure out the syntax to first launch x11 and then execute the script. I can get the X11 terminal window to launch from a script, but I can't then send a command to it. Any help would be appreciated.

Thanks,
Anthony

hayne 04-09-2008 05:24 PM

AppleScript can launch apps ('tell "YourBigApp" to activate').
And you can invoke AppleScript from a shell script or Perl script via 'osascript'.
See examples in the Unix FAQ I have referred to above.

berone 04-10-2008 10:50 AM

Yeah, I had read all the stuff in the FAQ. Unfortunately in order to do what I want I have to start from the beginning and learn a whole lot of stuff that I'm not going to need again. I've been trying to get this script working on OS X and create an instruction set to help others do it. I thought simplifying the start process would help, but I've spent way more time on this than it's worth.

I was thinking about a MacBook Pro as my next computer, but so much of what I find myself doing is so much more difficult on a Mac that I'm back to looking at the ThinkPad.

Thanks for all your help - I do appreciate it.

Regards,
Anthony


All times are GMT -5. The time now is 10:13 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.