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:
change py.bat to py.command
at terminal:
$ cd ~/Desktop $ chmod 755 py.commandopen code on vim and place in (from: ./configure : /bin/sh^M : bad interpreter):
:set fileformat=unixChanged code too:
#!bin/bash cd ~/Desktop python search.py exit;
3 Answers 3
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.
2 Comments
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.
Comments
chmod +x *filename*
Execute the above command in the terminal then make the bash file as follows
cd Desktop/*folder*/
python3 *filename.py*
ls -l.desktopfile 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