How can I run .sh on Windows 7 Command Prompt? I always get this error when I try to run this line in it,
app/build/build.sh
error,
'app' is not recognized...
or,
bash app/build/build.sh
error,
'bash' is not recognized...
Any ideas what have I missed?
Here the screen grab,
Install GIT. During installation of GIT, add GIT Bash to windows context menu by selecting its option. After installation right click in your folder select GIT Bash Here (see attached pic) and use your sh command like for example:
sh test.sh
The error message indicates that you have not installed bash, or it is not in your PATH.
The top Google hit is http://win-bash.sourceforge.net/ but you also need to understand that most Bash scripts expect a Unix-like environment; so just installing Bash is probably unlikely to allow you to run a script you found on the net, unless it was specifically designed for this particular usage scenario. The usual solution to that is https://www.cygwin.com/ but there are many possible alternatives, depending on what exactly it is that you want to accomplish.
If Windows is not central to your usage scenario, installing a free OS (perhaps virtualized) might be the simplest way forward.
The second error message is due to the fact that Windows nominally accepts forward slash as a directory separator, but in this context, it is being interpreted as a switch separator. In other words, Windows parses your command line as app /build /build.sh (or, to paraphrase with Unix option conventions, app --build --build.sh). You could try app\build\build.sh but it is unlikely to work, because of the circumstances outlined above.
The most common way to run a .sh file is using the sh command:
C:\>sh my-script-test.sh
other good option is installing CygWin
in Windows the home is located in:
C:\cygwin64\home\[user]
for example i execute my my-script-test.sh file using the bash command as:
jorgesys#INT024P ~$ bash /home/[user]/my-script-test.sh
you can use also cmder
Cmder is a software package created out of pure frustration over the absence of nice console emulators on Windows. It is based on amazing software, and spiced up with the Monokai color scheme and a custom prompt layout, looking sexy from the start
cmder.net
On Windows 10 Anniversary Update, it's even easier to run shell commands in/with bash on ubuntu on windows
I was trying to set my region for my x-wrt r7000 netgear router, I found the following worked for me, using bash on ubuntu on windows, you do have to enable subsystem found in windows features, and dev mode on
ssh admin#192.168.1.1 < /mnt/c/ccode-eu.sh
Install the GitBash tool in the Windows OS. Set the below Path in the environment variables of System for the Git installation.
<Program Files in C:\>\Git\bin
<Program Files in C:\>\Git\usr\bin
Type 'sh' in cmd window to redirect into Bourne shell and run your commands in terminal.
New feature in Windows - run bash on ubuntu on windows - available in Windows 10 "Insiders" builds after the Build conference:
https://blogs.windows.com/buildingapps/2016/03/30/run-bash-on-ubuntu-on-windows/
Personally I used this batch file, but it does require CygWin installed (64-bit as shown).
Just associate the file type .SH with this batchfile (ExecSH.BAT in my case) and you can double-click on the .SH and it runs.
#echo off
setlocal
if not exist "%~dpn1.sh" echo Script "%~dpn1.sh" not found & goto :eof
set _CYGBIN=C:\cygwin64\bin
if not exist "%_CYGBIN%" echo Couldn't find Cygwin at "%_CYGBIN%" & goto :eof
:: Resolve ___.sh to /cygdrive based *nix path and store in %_CYGSCRIPT%
for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%~dpn1.sh"') do set _CYGSCRIPT=%%A
for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%CD%"') do set _CYGPATH=%%A
:: Throw away temporary env vars and invoke script, passing any args that were passed to us
endlocal & %_CYGBIN%\mintty.exe -e /bin/bash -l -c 'cd %_CYGPATH%; %_CYGSCRIPT% %*'
Based on this original work.
just install git and by "bash <name.sh>" run your .sh file.
I use Windows 10 Bash shell aka Linux Subsystem aka Ubuntu in Windows 10 as guided here
Have you tried cding to the root directory where your .sh is located in order to execute it from there, instead of writing down a path to the file as you showed in your question?
Like so:
$ cd app/build
$ build.sh
Related
I created a python script ExeMain.py that use virtualenv for dependence. So to launch this script using .desktop I made this command
Exec=sh -c 'source ~/PycharmProjects/ProBlog/venv/bin/activate; python ~/PycharmProjects/ProBlog/ExeMain.py;'
Which resulted with this error "sh: 1: source: not found" and followed by "ImportError"
Without the source command in .desktop file with all necessary modules installed in direct python environment it works fine (in other Linux machine). As it seams not to be a good practice i tried using venv. And as of the errors i could see than the import error is because source command is not executed.
When i launch the .desktop file it should execute the ExeMain.py file.
Standard versions of sh does not have the source command. Perhaps use /bin/bash instead? So it'd be written something along the lines of this:
Exec=/bin/bash -c 'source /home/username/PycharmProjects/ProBlog/venv/bin/activate && /home/username/PycharmProjects/ProBlog/venv/bin/python /home/username/PycharmProjects/ProBlog/ExeMain.py'
I also suggest three other changes: 1) the && operator so that it won't try to run python code until the activate is finished, 2) replace the ~ tilde with the full path in case the desktop environment doesn't properly expand it, and 3) put in the full path to the venv python so that it doesn't default to the system python. Some of these may not be necessary (depending on the system setup), but it doesn't hurt to be careful.
I have a MacBook Pro 13` inch (without touch bar) and it is running mac os sierra. I have installed Xcode, command line tools and homebrew. Now I want to install python 3 and make it a default interpreter when calling it from the terminal. I have found this guide http://docs.python-guide.org/en/latest/starting/install3/osx/ and I get confused when they say I should add the path to the ~./profile but I don't know where to find the file. Can somebody help with step by step guide on how to create it if it not there?
Thanks
I felt to add something here that ate a lot of my time when I had a similar issue.
Many a times the .profile or the .bash_profile (in case of bash shell) is not present. This is typically when you have freshly installed the OS or when you're on a new machine. This gets confusing as one may think if the installation wasn't correct etc. Nothing to worry about. If you don't find these files then simply create it new using vi or whatever means (GUI) and you're good to go. Having a shell doesn't mean you have these files too and you have to create it yourself if you're the first one to use it.
The file you are looking for depends on your shell, so if you are using bash, which is the default shell on Mac systems, it will be either ~/.bash_profile, or ~/.bashrc depending on whether you are in a login shell or non-login shell respectively.
Mac Terminal runs a login shell every time a new terminal window is open, and it calls .bash_profile, so you should use ~/.bash_profile.
On Mac, zsh is the default shell. You can check this by running
echo $SHELL
If so, you can check for ~/.zshrc if not you can create one and add required commands, alias which need to run during the terminal startup.
Then you can run
source ~/.zshrc
This will be executed every time the terminal is opened.
It could be that you are not running bash (rather an extended version like zsh, ksh, etc)
If you are running bash shell, then you should be able to see ~/.bash_profile, or ~/.bashrc or ~/.profile.
Incase of zsh shell, you would see .zprofile or .zshrc
You can check what shell you have by echo $SHELL
After you install "Brew", there is a prompt in the terminal that says: "Next Steps:"
Follow those steps!
While running Linux versions of python, pip etc. "natively" on windows is amazing, I'd like to do so using a proper IDE. Since SSHD compatibility has not been implemented yet, I'm trying get PyCharm to recognize Linux python as a local interpreter.
After installing the Windows Linux subsystem, typing
bash -c python
from the windows command line will drop you into a python shell.
bash -c "echo \"print 'hello world'\" | python"
works as well, producing "hello world" as output in the windows shell!
I'm trying to wrap this up as a .bat file and present it to PyCharm as a local interpreter, i.e.
python.bat:
C:\Windows\System32\bash.exe -c "echo %1 | python"
But I keep getting "the sdk seems invalid" for any variation I try. Since I'm not sure exactly what PyCharm is doing to "validate" the SDK, this is hard to overcome.
Using PyCharm Professional with WSL Python on Win10
Starting SSH
PyCharm can only be configured to use WSL Python as a Remote Interpreter (this is due to lack of other public API).
Install Win10 build 14361 or later. You also can upgrade your current Insider Preview.
Install wsl (something like lxrun /install` && lxrun /update )
Run bash.exe
Update to latest version sudo apt-get update && sudo apt-get upgrade
Open /etc/ssh/sshd_config
Enable password authentication (unless you want to use public keys). Open /etc/ssh/sshd_config , and set PasswordAuthentication yes.
Since chroot is not implemented in WSL (yet), you also need to set UsePrivilegeSeparation no
Save and close it
Type sudo $(sudo which sshd) -d to run OpenSSH on foreground (it is much easier for debug). You should see something like Server listening on 0.0.0.0 port 22
From another bash.exe session try ssh 127.0.0.1
If you see message about ECDSA finger print, answer y . You should see password prompt. If you see it, then your server works correctly.
Turn it off with CTRL+C, and start server in daemon mode (sudo service ssh start). Looks like upstart is broken on current WSL, so you would need to run bash.exe, start sshd and keep console window opened since WSL stops when the last client disconnects. You may create wsl_ssh.bat file like bash.exe -c "sudo service ssh start &&& sleep 999d" and use it to launch ssh.
Configuring PyCharm
PyCharm should be configured to use WSL as a remote interpreter but without deployment, since each drive on Windows is mapped to an appropriate folder in /mnt/<DRIVE_NAME> in WSL. So, you only need to configure the mapping. For remote interpreters, see configuration-remote-python-interpreters . You should use 127.0.0.1 as hostname, and login and password you entered after first lxrun /install. You also should set C:\ to /mnt/c/ in your mappings. See the video from the previous post.
Author: Ilya Kazakevich
14 Jun 2016, 17:20
https://youtrack.jetbrains.com/issue/PY-19129#comment=27-1469350
I tried working with most solutions but the main issue is that I can't downgrade OpenSSH on Windows to something below 7.5 as is recommended by JetBrains.
Luckily they have solved this issue for us!
I have downloaded the Early Access version of Pycharm 2018.3
https://blog.jetbrains.com/pycharm/2018/10/pycharm-2018-3-eap-7/
This is however only available in the professional version.
I'd like to add the answer of bmjjr by stating that this is only available with the PyCharm Professional Edition. The Remote Interpreter feature is not available with the Community Edition, as I sadly found out:
Supported only in Professional Edition
Cython
Django
AppEngine
Flask
Jinja2
Mako
web2py
Pyramid
Profiler
SQLAlchemy
Diagrams
Remote interpreters, remote debugging, Vagrant, Docker
Duplicate code detection
Code coverage
.po files support
BDD support
Profiler integration
Thread Concurrency Visualization
https://www.jetbrains.com/pycharm/features/editions_comparison_matrix.html
Well, I've managed to produce an ugly working hack.
You'll have to install python-setuptools and pip manually under the Linux subsystem. Be sure to use the pip version provided by PyCharm, you'll find it at a path similar to:
C:\Program Files (x86)\JetBrains\PyCharm 2016.1.2\helpers\pip-7.1.0.tar.gz
Then setup the following script as "python.bat" under "c:\Python" and point PyCharm to it as an interpreter:
#echo off
#setlocal enableextensions enabledelayedexpansion
:: Requiers pip and setuptools to already be installed on linux subsystem
Set "Pattern= "
Set "Replace=\ "
Set "cdrive=C:"
Set "linpath=/mnt/c"
:: Iterate over arguments, convert paths to linux format and concatinate
set argCount=0
for %%x in (%*) do (
set /A argCount+=1
set arg=%%x
:: Backward slash to forward slash
SET arg=!arg:\=/!
:: C drive to /mnt/c/ - default linux subsystem mount point
SET arg=!arg:%cdrive%=%linpath%!
:: Space to escaped space
SET arg=!arg:%Pattern%=%Replace%!
:: Parethesis to escaped parenteses
SET arg=!arg:^(=\^(!
SET arg=!arg:^)=\^)%!
:: Deqoute voodoo via http://ss64.com/nt/syntax-dequote.html
SET arg=###!arg!###
SET arg=!arg:"###=!
SET arg=!arg:###"=!
SET arg=!arg:###=!
if "!args!"=="" (
set args=!arg!
) else (
set args=!args! !arg!
)
)
:: Dump it to the interpreter
:: Output is piped inside the Linux subsys, as windows piping for bash seems broken
START "Terrible hack to avoid pipe error" /W /MIN C:\Windows\System32\bash.exe -c "python !args! > /mnt/c/Python/test"
:: Output resulr from piped file
type c:\Python\test
:: echo !args!
EXIT /B > NUL
Forgive the terrible coding style, as I've never really developed windows batch files before.
You may have to tweak the directory structure to match your system. Also note that the output of any python script called by Python.bat is piped to a temp file under the linux subsystem, then typed back out under windows. For some reason, piping the output of bash.exe via windows causes errors.
Hope this helps.
UPDATE: Wrapped the call to "bash" with "START" in order to avoid terrible pipe handling errors (c.f. https://wpdev.uservoice.com/forums/266908-command-prompt-console-bash-on-ubuntu-on-windo/suggestions/13425768-allow-windows-programs-to-spawn-bash)
Supported via remote int. See last comment: https://youtrack.jetbrains.com/issue/PY-19129
Configure remote interpreter via WSLÂ #
Open the Add Python Interpreter dialog by either way:
When you're in the Editor, the most convenient way is to use the Python Interpreter widget in the Status bar. Click the widget and select Add Interpreter ...
If you are in the Settings/Preferences dialog (Ctrl+Alt+S), select Project | Project Interpreter. Click The Configure project interpreter icon and select Add.
In the left-hand pane of the dialog, click WSL.
Use Anaconda:
.\rootfs\home\$UserName$\anaconda\envs\$Conda_Env$\bin\python3
Use Pyhton:
Adding a WSL interpreter
Select the Linux distribution and specify the path to the python executable in the selected Linux distribution.
Once done, the new interpreter will be added to your project, and the default mnt mappings will be set.
Newly added WSL interpreter
Note that with WSL you cannot create virtual environments: all packages you install will be added to the corresponding system interpreter. You will be asked to enter your sudo password.
Entering your sudo password
Reference:
JetBrains
I used the normal remote ssh intepreter "old method" discussed in this link here:
I used this guide:
critically I was getting an authentication error which was resolved with this:
I am trying to use Cygwin to run my first python program. I downloaded python and Cygwin. Within the Cygwin terminal I ran the command: "which python" to confirm that the terminal could find python. However when I attempt to cd into my Desktop for example, I get "-bash: cd: Desktop: No such file or directory". When I try to do an ls, it simply returns an empty line. For some reason "pwd" works normally.
I am having a problem similar to the one here: Cygwin ls command not found
However changing the environmental variables has no effect (even after I closed and repopened the terminal window).
Make sure C:\cygwin64\bin or its equivalent is set as a path variable.
Run mintty.exe as administrator and enter: mkpasswd -l -p "$(cygpath -H)" > /etc/passwd
Close mintty.exe and access it again through the Windows shortcut 'Cygwin64 Terminal'. This is auto-generated during setup. The shortcut target should be set as the path to the bin which contains mintty.exe C:\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico -.
Hope this helps!
I have some small utility scripts written in Python that I want to be usable on both Windows and Linux. I want to avoid having to explicitly invoke the Python interpreter. Is there an easy way to point shebang notation to the correct locations on both Windows and Linux? If not, is there another way to allow implicit invocation of the Python interpreter on both Windows and Linux without having to modify the script when transferring between operating systems?
Edit: The shebang support on Windows is provided Cygwin, but I want to use the native Windows Python interpreter on Windows, not the Cygwin one.
Edit # 2: It appears that shebang notation overrides file associations in Cygwin terminals. I guess I could just uninstall Cygwin Python and symlink /usr/bin/python to Windows-native Python.
Read up on the Python Launcher for Windows in the docs, which was initially described in PEP 397. It lets
you define custom shebang configurations in "py.ini" (e.g. to use pypy),
and out of the box you can use virtual shebangs such as #!/usr/bin/env python3, or shebangs with real paths such as #!"C:\Python33\python.exe". (Quoting is required for paths containing spaces.) You can also add command-line options to a shebang. For example, the following shebang adds the option to enter interactive mode after the script terminates: #!/usr/bin/python3 -i.
The installer associates .py (console) and .pyw (GUI) script file types with the respectively named launchers, py.exe and pyw.exe, in order to enable shebang support for scripts in Windows. For an all-users installation, the launchers are installed to the Windows folder (i.e. %SystemRoot%). For a per-user installation, you may need to manually add the installation directory to PATH in order to use py.exe in the shell (*). Then from the command line you can run Python via py -2, py -3, py -2.6, py -3.3-32 (32-bit), and so on. The launcher is handy when combined with -m to run a module as a script using a particular version of the interpreter, e.g. py -3 -m pip install.
(*) The new installer in 3.5+ defaults to "%LocalAppData%\Programs\Python\Launcher" for a per-user installation of the launcher, instead of installing it beside "python.exe", and it automatically adds this directory to PATH.
Unless you are using cygwin, windows has no shebang support. However, when you install python, it add as file association for .py files. If you put just the name of your script on the command line, or double click it in windows explorer, then it will run through python.
What I do is include a #!/usr/bin/env python shebang in my scripts. This allows for shebang support on linux. If you run it on a windows machine with python installed, then the file association should be there, and it will run as well.
Short answer:
The easiest way is to install git for windows wich comes with GitBash. Then add shebang lines in your scripts to indicate they should be run with python when executed.
#!/usr/bin/env python
More info:
Unlike Cygwin, git bash uses your native windows applications and lets you use bash scripts without any configuration.
It will automatically treat any file with a shebang line as executable the same way Linux shells do.
eg: #!/usr/bin/env php or #!/usr/bin/env node or any other application you want will work as long as you add the paths to your windows ENV path.
You can edit env vars in windows by hitting start and typing env should be the first option.
Git bash also installs git and hooks it up with a credentials manager for you and makes it super easy to sign into 2fa-enabled svn services and a ton of other handy developer features.
Git bash is IMO a must on every developer's machine.
Another option:
Install WSL (Windows Subsystem for Linux) which will work the same.
WSL also lets you install native Linux versions of all your command line applications if you prefer.
Linux binaries will take precedence over windows ones if installed but you can still choose to run the windows version of commands any time you want by specifically adding .exe on the end.
Install pywin32. One of the nice thing is it setups the file association of *.py to the python interpreter.
sorry for open old topic.
I create one file py.cmd and place it in the C:\Windows\System32 folder
py.bat:
#(
#set /p shebang=
)<%1
#set shebang=%shebang:#! =%
#%shebang% %1 %2 %3 %4 %5 %6 %7 %8 %9
py.bat file explain:
Get the first line from *.py file
Remove shebang characters "#! "
Run python file using shebang python path
All windows python script must start with shebang line as the first line in the code:
#! c:\Python27\python.exe
or
#! c:\Python37\python.exe
Then run it:
cmd> py SomePyFile.py param1 param1 paramX
Not with shebang ... but you might be able to set up a file association, see this SO question which deals with Perl and the associated answers which will also be pertinent as there's known problems with Windows and stdin/out redirection...