-1

(I am using ubuntu 12.04)

I made this python program:

#!/bin/sh
# -*- coding: utf-8 -*-

#Created on Tue Nov 12 19:44:50 2013

#@author: matthew

import os

print "Multiple Command Runner"
print "<Made by Matthew Cherrey>"
print "-------------------------"
numbcommand = 0
allcoms = []
while 1:
    numbcommand = numbcommand + 1
    command = raw_input(" Command: ")
    allcoms.append(command)
    decide = raw_input("Press [Enter] to and another command, press [r] to run all commands: ")
    if decide == "r":
        break

commands = ""
first = True
for item in allcoms:
    if first:
        commands = item
    else:
        commands = commands + " && " + item
os.system(commands)

And I want to be able to run it in the terminal. I use the python editor: Spyder This has an option to "Run in system terminal". Whenever I do this, my program works perfectly. I can enter multiple commands, and have them all run. When I set the file to an exicutible and run /home/matthew/.runallcommands.py --python or /home/matthew/.runallcommands.py, first makes my cursor into a "t" which then when I click somewere, is take a picture of that area of the screen and saves it as a photo named "OS" in my home folder. then I get this error message:

matthew@matthew-MS-7721:~$ /home/matthew/.runallcommands.py --python
Warning: unknown mime-type for "Multiple Command Runner" -- using "application/octet-stream"
Error: no such file "Multiple Command Runner"
Warning: unknown mime-type for "<Made by Matthew Cherrey>" -- using "application/octet-stream"
Error: no such file "<Made by Matthew Cherrey>"
/home/matthew/.runallcommands.py: 13: /home/matthew/.runallcommands.py: numbcommand: not found
/home/matthew/.runallcommands.py: 14: /home/matthew/.runallcommands.py: allcoms: not found
/home/matthew/.runallcommands.py: 17: /home/matthew/.runallcommands.py: Syntax error: "(" unexpected (expecting "do")

I am not sure if it has something to do with how I called the file, because my program worked 100% fine in the terminal in spyder.

1 Answer 1

4

Your python commands are being interpreted as shell commands e.g. print "Multiple Command Runner" is looking for a file of that name to print (with an associated MIME type).

As far as I know, adding a --python command line parameter to a shell script does not cause it to be interpreted as a python script - to do that, you must change the #!/bin/sh 'shebang' to an appropriate python shebang e.g.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

#Created on Tue Nov 12 19:44:50 2013

#@author: matthew

import os
.
.
.

You will then be able to run it by making the file executable

chmod +x /path/to/yourfile.py

and execute it as

/path/to/yourfile.py

If yourfile.py is in the current directory, you can use the relative path ./yourfile.py to execute it.

1
  • 1
    The screenshot is taken because import os runs the command import which belongs zu ImageMagick and takes a screenshot. Commented Nov 13, 2013 at 14:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .