how to run python script from Unix Shell?

Phil Edwards phil.e at mistral.co.uk
Mon Sep 10 10:27:42 EDT 2001


David wrote:
> 
> Hi there,
> 
> Can you help to advise how to run python script from Unix Shell?
> What I am doing is:
> 1)chmod +x  script name
> 2)put #!/usr/bin/python   at first line
> 3)when I type the script name under Shell,
> the message is:
> bash: Script name command not found.
> 
> Actually, it works if I type:
> python "script name"
> 
> But the problem is I want run it by typing "script name"
> 
> Thanks for your help.

I'd like to add one thing to all the (helpful) comments made by other
people.

When I first started to learn Python, the first thing I did was bought a
likely looking book, in this case "Teach Yourself Python In 24 Hours" by
Sams. The author had helpfully provided all the examples and solutions
to the exercises in the book as a dwonload from his web site. When I
tried to run any of these scripts, I got the same "bash: xxx.py command
not found" error message.

I'm by no means a Linux/UNIX guru, but I consider myself to be well past
even the "intermediate" stage. This problem had me ripping my hair out,
as none of the usual causes for this sort of thing were applicable.

In desperation, I used od(1) to look at the source of the Python stuff I
was trying to run, at which point the cause of the problem became clear.
The author had written the scripts on a Win9x box, and FTP'd them in
binary mode to his web server, thus nicely preserving all the CRLF line
endings that MS-DOS is so beloved of.

My solution was the following short shell script to convert the CRLF's
into oridnary LF's:

-----begin-----
#!/bin/bash
#
# convert MS-DOS line terminator characters to UNIX format
#
        if [ "$#" -ne 1 ]; then
                echo "Usage: undos filename\n"
                exit 1
        fi
 
        cat $1 | tr -d '\015' > /tmp/$$
        chmod --reference=$1 /tmp/$$
        mv /tmp/$$ $1
------end------

It's highly unlikely that this is the reason behind David's problem, but
reading this thread brought it all back to me. If it saves a few strands
of someones hair, then all well and good!



More information about the Python-list mailing list