Script .Sh is not running in Windows? - python

I am trying to run .Sh file on my Windows machine using GitBash but it is giving error -
run_main.sh: line 3: pkill: command not found
rm: cannot remove '/root/impactAllClient/NewsCollected.csv': No such file or directory
run_main.sh: line 7: C:ProgramDataAnaconda3binpythonimpactAllClient-mastercreateDataset.py: command not found
run_main.sh: line 10: C:ProgramDataAnaconda3binpythonimpactAllClient-masterallClientAbstractive.py: command not found
Though I have my files in correct locations - C:\ProgramData\Anaconda3\bin\python\impactAllClient-master
Here is my .sh File code
#!/bin/bash
pkill -f allClientAbstractive.py
sleep 10
rm /root/impactAllClient/NewsCollected.csv
C:\ProgramData\Anaconda3\bin\python\impactAllClient-master\createDataset.py
sleep 10
C:\ProgramData\Anaconda3\bin\python\impactAllClient-master\allClientAbstractive.py &

Can you run pkill manually in GitBash? I guess it's not installed.
I think this is not a valid Windows path: /root/impactAllClient/NewsCollected.csv. It should start with something like /c/....
Also the Paths C:\ProgramData\..., should use forward slashes and start like /c/ProgrammData/..., when using GitBash.
You can open a bash shell in the path C:\ProgramData\Anaconda3\bin\python\impactAllClient-master\ and then run pwd. The path that is printed to the console should be used in the script.

Related

How to execute python script without typing python on the start? [duplicate]

How do i run a python program just by typing the script name on windows 10 cmd line?
Also without having to change directory. I already added my scripts folder and python folder to the path.
tried also tu run assoc py.=PythonScript
ftype PythonScript=python.exe %1 %*
Here's the program's content:
#! python3
# mapIt.py - Launches a map in the browser using an address from the command line or clipboard
import webbrowser, sys, pyperclip
if len(sys.argv) > 1:
address = ' '.join(sys.argv[1:])
else:
address = pyperclip.paste()
webbrowser.open('https://www.google.com/maps/place/' + address)
I added a screenshot with all the commands i tried so far.
I think what you want is to run the file 'mapIt.py' without invoking the keyword python that is:
>mapIt.py
instead of
>python mapIt.py
the way to do that in Linux or macOS is simple enough, you can add
#!/usr/bin/env python
to the top of the file, rename your file from mapIt.py to mapIt
make the script executable:
chmod +x mapIt
But for windows there is no straightforward solution.
One way you can do it is convert the file into an exe or
first add a python.exe association for all '.py' files
> assoc .py=Python
and then
> ftype Python="<path of your python.exe>" "%1" %*
replace the text in angular brackets (<>) with the path of your python.exe file.

Python script runs on command line but not from .sh file

I'm attempting to create a .sh file to batch a number of runs of a neural network on Python whilst on holidays.
At the moment I have been calling this from the command line:
python neural_network_trainer.py [args]
I now have a .sh script written:
#!/bin/bash
python neural_network_trainer.py [args]
# Repeated with varied args
That I am attempting to call in the same terminal as the original command line was running:
./august_hols.sh
I get the following error:
File "/data/Python-3.6.9/lib/python3.6/site.py", line 177
file=sys.stderr)
^
SyntaxError: invalid syntax
Where the Python install is in /data (for reasons).
Running which on the command line reports the correct Python directory set via an alias in ~/.bashrc:
alias python=/data/Python-3.6.9/bin/python3
But running which between the Bash shebang and the first python call reports /bin/python.
I've attempted to set the alias again at the start of the .sh script to no avail. I'm scratching my head as this is exact process I have used elsewhere, albeit not on this precise PC. I can copy the exact command from the top of the bash file into the terminal and it runs fine, try and call ./august_hols.sh and get the above Python error.
Where is Bash getting that path from, and why is it not using my expected route through ~/.bashrc?
Bash sub-shell does not inherit alias in the main shell
You can source the script (run in the main shell), instead of execute it (run in the sub-shell)
source script.sh
EDIT:
Solution 2:
Run bash as the login shell so ~/.bashrc is executed, so your alias is loaded before your script.
The subshell needs to be interactive to enable alias, because alias is enabled by default only for interactive shell, but script is non-interactive by default.
bash --login -i script.sh
Solution 3:
Similar to above, except alias is enabled explicitly
bash --login -O expand_aliases script.sh
Have you tried:
python=/data/Python-3.6.9/bin/python3 ./[your_bash].sh
In your .sh
Do this
#!/usr/bin/env bash
export PATH=/data/Python-3.6.9/bin:$PATH
exec python neural_network_trainer.py "$#"
Aliases are tricky.
A maybe more nasty solution
mapfile < <(declare -p | grep -m 1 BASH_ALIASES) && bash script.sh "${MAPFILE[#]}"
within your script you will need
shopt -s expand_aliases
eval $1
echo ${BASH_ALIASES[python]}
python --version
How about this:
#!/bin/bash
/data/Python-3.6.9/bin/python3 neural_network_trainer.py [args]
# Repeated with varied args

How to run exe file with command line arguments in Mac terminal?

I want to run the exe file with command line arguments in Mac terminal
p1.exe -f input.txt
But im getting error -bash: p1: command not found
I have converted python file p1.py into p1.exe using
pyintsaller p1.py --onefile
And running the python file with arguments works
python p1.py -f input.txt
This isn't to do with Python, but is a basic command shell issue. To run an executable from the current directory, you need to use the ./ prefix.
./p1.exe -f input.txt
Note, it's a bit odd to use a .exe extension for a Linux executable.
Note that on Unix like systems (Linux/Unix/Solaris/MacOS). scripts can be run without explicitly invoking interpreter, if two conditions are meet:
script file starts with this line (or similar): #!/usr/bin/env python
file has executable attribute flag is set
Then you can run script like this:
./p1.py --onefile
./ means run thing from local directory. If this is not pressent the it tries to run things located by PATH variable, that is why you can run interpreter python

Run Python script at startup in Ubuntu

I have a short Python script that needs to run at startup - Ubuntu 13.10. I have tried everything I can think of but can't get it to run. The script:
#!/usr/bin/python
import time
with open("/home/username/Desktop/startup.txt", 'a') as f:
f.write(str(time.time()) + " It worked!")
(The actual script is a bit different, as I'm just using this for testing purposes, but you get the idea.)
I've tried all of the following, with no luck:
Put the command python startuptest.py in crontab, as #reboot
python /home/username/Documents/startuptest.py, both as the regular user and as sudo
Put the command python /home/username/Documents/startuptest.py in /etc/rc.local
Opened Ubuntu's Startup Applications and put the command there
Done all of the preceding, putting the command into a shell script
and calling that shell script instead
Nothing works. I get the feeling I'm missing something simple. Any ideas? (The script runs fine if I just run the command from a terminal.)
Instructions
Copy the python file to /bin:
sudo cp -i /path/to/your_script.py /bin
Add A New Cron Job:
sudo crontab -e
Scroll to the bottom and add the following line (after all the #'s):
#reboot python /bin/your_script.py &
The “&” at the end of the line means the command is run in the background and it won’t stop the system booting up.
Test it:
sudo reboot
Practical example:
Add this file to your Desktop: test_code.py (run it to check that it works for you)
from os.path import expanduser
import datetime
file = open(expanduser("~") + '/Desktop/HERE.txt', 'w')
file.write("It worked!\n" + str(datetime.datetime.now()))
file.close()
Run the following commands:
sudo cp -i ~/Desktop/test_code.py /bin
sudo crontab -e
Add the following line and save it:
#reboot python /bin/test_code.py &
Now reboot your computer and you should find a new file on your Desktop: HERE.txt
Put this in /etc/init (Use /etc/systemd in Ubuntu 15.x)
mystartupscript.conf
start on runlevel [2345]
stop on runlevel [!2345]
exec /path/to/script.py
By placing this conf file there you hook into ubuntu's upstart service that runs services on startup.
manual starting/stopping is done with
sudo service mystartupscript start
and
sudo service mystartupscript stop
If you are on Ubuntu you don't need to write any other code except your Python file's code , Here are the Steps :-
Open Dash (The First Icon In Sidebar).
Then type Startup Applications and open that app.
Here Click the Add Button on the right.
There fill in the details and in the command area browse for your Python File and click Ok.
Test it by Restarting System . Done . Enjoy !!
Create file ~/.config/autostart/MyScript.desktop
with
[Desktop Entry]
Encoding=UTF-8
Name=MyScript
Comment=MyScript
Icon=gnome-info
Exec=python /home/your_path/script.py
Terminal=false
Type=Application
Categories=
X-GNOME-Autostart-enabled=true
X-GNOME-Autostart-Delay=0
It helps me!
In similar situations, I've done well by putting something like the following into /etc/rc.local:
cd /path/to/my/script
./my_script.py &
cd -
echo `date +%Y-%b-%d_%H:%M:%S` > /tmp/ran_rc_local # check that rc.local ran
This has worked on multiple versions of Fedora and on Ubuntu 14.04 LTS, for both python and perl scripts.
nano /etc/rc.local
and edit in
python ~/path-to-script.py
worked for me

Running "IDLE3.2 -s" from the "Finder" in OS X 10.6

I want to run IDLE3.2 with the argument "-s" so it can read ".pythonstartup" and export relevant modules, change the working directory and etc. Here is what I have tried:
Created a shell script:
/usr/local/bin/idle3.2 -s
this works allright, however running the script from the Finder opens up the Terminal, which is not the desired behavior.
Created an applescript:
do shell script "/bin/bash; cd /usr/local/bin/; ./idle3.2 -s"
this get rids of the terminal however fails to pass "-s" argument to idle3.2 so the configuration file is not loaded.
any suggestions?
EDIT: turns out environment variables are not properly set even though /bin/bash is called. so the following solves the problem:
do shell script "/bin/bash; source ~/.profile; /usr/local/bin/idle3.2 -s"
I think your do shell script "/bin/bash; cd /usr/local/bin; ./idle3.2 -s" is doing extra work, and can probably be done more simply. Try:
do shell script "/usr/local/bin/idle3.2 -s"
thanks to #lain the following applescript solves the problem:
do shell script "source ~/.profile; idle3.2 -s"
where ~/.profile points the shell (in this case /bin/sh) the path for .PYTHONSTARTUP and the path for idle3.2

Categories

Resources