1

I have a python script that takes in a .txt file and outputs a .txt file. I want to create a bash file that I can click on from my desktop to execute the python script.

So far I have:

#!/bin/bash
cd /Desktop; 
cd ./py-data;
python ./grab.py;
exit;

This just opens up the python script. Ideally I would like to click on the bash script and have the python script to run in the back ground and just produce the output without having to open the python script.

Solution:

  1. change py.bat to py.command

  2. at terminal:

    $ cd ~/Desktop $ chmod 755 py.command

  3. open code on vim and place in (from: ./configure : /bin/sh^M : bad interpreter):

    :set fileformat=unix

  4. Changed code too:

    #!bin/bash cd ~/Desktop python search.py exit;

Kay Carosfeild
  • 91
  • 1
  • 1
  • 12
  • And if you run the script from the command line-terminal? – Hackerman Oct 03 '17 at 16:09
  • I want it to be a file that is clickable from the desktop so others who do not understand programming can run the python script – Kay Carosfeild Oct 03 '17 at 16:11
  • 1
    could you edit your question and include the file permissions on your bash script `ls -l` –  Oct 03 '17 at 16:12
  • 2
    The actual solution to this will depend on your operating system. For Linux, you can create a `.desktop` file with your script as the target. Or you may make the script executable (`chmod +x filename)` and set your file manager to execute the files of selected type on clicking – sid-m Oct 03 '17 at 16:22
  • When you say "opens up the script", do you mean it opens an editable window with the contents of the script? Check to see what your `python` command actually is, because the behavior you describe would be unexpected, to say the least. – chepner Oct 03 '17 at 16:24
  • I am developing on OS but would like it to work for PC as well. – Kay Carosfeild Oct 03 '17 at 16:24
  • @chepner It opens the editable file pull up script editor – Kay Carosfeild Oct 03 '17 at 16:25
  • 3
    AFAIK, there is no cross platform solution to this. You will need to handle each platform separately. Plus most PCs don't have bash. – sid-m Oct 03 '17 at 16:26
  • Yes I will create an executable file for PC – Kay Carosfeild Oct 03 '17 at 16:33
  • Duplicate: https://stackoverflow.com/questions/5125907/how-to-run-a-shell-script-in-os-x-by-double-clicking – pbuck Oct 04 '17 at 20:30
  • @pbuck thanks that got somewhere but it is still not producing my output text. Do you know of another solutions? – Kay Carosfeild Oct 04 '17 at 20:50

3 Answers3

1

On MacOS, set the name of your script file to end in .command, and make the file executable. With that filename extension, you can double-click the file, and it will run Terminal application and any output (to stdout / stderr) will be displayed in the terminal window which pops up for execution.

=== /Users/john/Desktop/foo.command ===
#!/bin/bash
echo 'hello'

Then:

===  At command prompt===
$ cd ~/Desktop
$ chmod 755 foo.command

Double click on foo.command and you'll see window popup:

Last login: Thu Oct 5
/Users/john/Desktop/foo.command ; exit;
iMac:~/Desktop john$ /Users/john/Desktop/foo.command ; exit;
hello
logout
[Process completed]

In the popup window, you'll see lots of lines, plus your output ("hello").

In your particular example, I think you have two problems:

First, you mention /Desktop, which probably isn't what you want, as the user's Desktop is ~/Desktop. This would cause your script to fail.

Second, the output you would see in the popup window is the output your script writes directly to standard out. If you script is writing to another file, it may be working great, but you'll not see that information displayed in the popup (it will be in whatever file you wrote it to.) So it depends what your grab.py file actually does.

Finally, you say "run in the background". Technically, that's not what's happening, as it will run in a separate foreground process. I assume that's what you mean.

pbuck
  • 4,291
  • 2
  • 24
  • 36
0

You could try chmod 777 on your bash script and then removing the extension. If you are on macOS then it should work. Not so sure about Linux, but it's worth a try.

ejj28
  • 88
  • 2
  • 12
0
chmod +x *filename*

Execute the above command in the terminal then make the bash file as follows

cd Desktop/*folder*/
python3 *filename.py*