Get Git Shell to run command after starting - windows

You can do
start powershell -command ls
to open PowerShell and get it to list files. I'm trying to do a similar thing with Git Shell. Currently I have
%LOCALAPPDATA\GitHub\Github.appref-ms --open-shell -command ls
which opens Git Shell, but doesn't do anything else. Is it possible to run a command after starting Git Shell in this way?

Since this hasn't been answered I'm going to offer an alternative solution of using git-bash instead of the MS application reference file.
start /b cmd /c "C:/Program Files/Git/git-bash.exe" /C/path/to/script.sh
See this question: Cant run shell script using Git Bash interface on Windows10

Related

How to call PowerShell script from WSL?

In my Windows directory
C:\Users\jholmes\pichak\analytics
I have run1.ps1 code.
I run wsl.exe, now my pwd is
/mnt/c/WINDOWS/system32
How to point to the first path and execute the script?
In WSL2, using the -File option worked for me:
powershell.exe -File path/to/script.ps1
When running wsl (or wsl.exe) from PowerShell, it should start in the same directory that you were in under PowerShell, just with the Linux version of it. For instance, if you are in PowerShell in C:\Users\jholmes\pichak\analytics, and you run wsl, you should end up in /mnt/c/Users/jholmes/pichak/analytics.
If not, then you may have something in your startup file already like the other answer recommend. You should remove any cd commands from your .bashrc, .zshrc, .profile, .bash_profile, or any other startup file you may have edited.
The wsl command provides a few handy arguments for specifying the starting directory:
wsl ~ will start in your Linux user's home directory (e.g. /home/jholmes)
wsl --cd c:\ will start in /mnt/c (or whatever the equivalent Linux directory is to the Windows version passed in).
wsl --cd \\wsl$\<distroname>\etc will start in your /etc directory (or whatever WSL path you passed in. Note that you will need to specific your distribution name (obtained from wsl -l -v).
Let's say that you are in /home/jholmes, and you want to execute the PowerShell script C:\Users\jholmes\pichak\analytics\run1.ps1. You first need to translate the Windows path to the Linux version:
/mnt/c/Users/jholmes/pichak/analytics/run1.ps
You can then run it via:
powershell.exe /mnt/c/Users/jholmes/pichak/analytics/run1.ps
It's also possible to set up a PowerShell script with a "shebang" line to execute it directly. If you add the following as the first line of run1.ps1:
#!/usr/bin/env -S powershell.exe -ExecutionPolicy Bypass
Then set it to be executable via chmod +x /mnt/c/Users/jholmes/pichak/analytics/run1.ps, then it becomes a "command" of sorts (technically, an "executable script") that you can execute directly by just typing the fully qualified name of the script at the command line:
/mnt/c/Users/jholmes/pichak/analytics/run1.ps
or
cd /mnt/c/Users/jholmes/pichak/analytics
./run1.ps
There are two ways to go about this:
You can change your working directory to that of your shell script and execute it normally. To do so, follow these steps:
Mount the relevant drive cd /mnt/c/.
Change directories according to the path of the script.
This approach is more of a hack that I use for the sake of convenience. I have created a folder in my Windows storage wherein I store all Ubuntu WSL related files. Say, D:\Ubuntu. To avoid changing the working directory every time you open WSL, you can modify the shell profile file (bashrc, zshrc etc.) to load the relevant directory at the end.
i.e., Add cd /mnt/d/Ubuntu/ at the end of your ~/.zshrc file if you use zsh or the relevant profile file otherwise.
Here is the evidence, that with the WSL2, our software can make the Powershell script encrypted and protected:
See this picture:
And it can detect and kill the process that uses fanotify because we think it can be used by attackers to hide critical file change (it also can find and kill dtrace/strace/stap/bpftrace/debuggers and process that opens /dev/kmem, /dev/mem, /proc/kcore, and the protected process' /proc/pid/mem etc):
This picture shows the encrypted PowerShell script now can detect and kill possible attackers' processes
For me this answer was almost the good one but when running for instance:
powershell.exe -File /mnt/c/path/to/script/script.ps1
I got following error:
The argument '/mnt/c/path/to/script/script.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.
I then have to:
cd /mnt/c/path/to/script
powershell.exe -File script.ps1

How to setup Intellij idea terminal path with cmder but initiated with git bash.exe?

So I have installed both Cmder and Git bash, and my Cmder is setup in a way that to run git bash.exe inside.
Currently my Intellij idea's path for terminal is set to:
C:\Program Files\Git\bin\bash.exe, which works fine for bash.exe
However, one disadvantage for directly using bash.exe is that it will not automatically convert a windows directory, for example:
D:\Documents\Adobe, when I copy this after cd, it will be:
cd D:\Documents\Adobe instead of:
cd /d/Documents/Adobe which I will get in cmder.
I have looked up online, people shared about how to incorporate cmder in idea's terminal by using the path:
"cmd.exe" /k ""%CMDER_ROOT%\vendor\init.bat""
However, that is only using cmder as a coating for cmd.exe, not bash.exe as I want.
I have tried to replace with:
"C:\Program Files\Git\bin\bash.exe" /k ""%CMDER_ROOT%\vendor\init.bat"", it was not working.
also tried:
"C:\Program Files\Git\bin\sh.exe" --login -i /k ""%CMDER_ROOT%\vendor\init.bat""
and:
"C:\Program Files\Git\bin\sh.exe" /k ""%CMDER_ROOT%\vendor\init.bat""
All failed.
Any sugestons? Thanks!
Or if someone can show me how to change git bash.exe so that it can automatically translate the windows directory to Linux type, that would also work.
I don't think you can run git-bash in cmder AND from Intellij IDEA terminal.
I tried this: creating a task for git-bash in Cmder and try to use cmder.exe /TASK xxx in Intellij. I have the task, but it seems that Intellij does not support args after an executable in Terminal settings, so /TASK xxx is not parsable.
Then I changed path in Terminal settings to a BAT file, but terminal seems not support running it; I see terminal flashes and closes. Maybe only executable is allowed. Branch info is shown at the right of current path. I think it is convenient.
So, you have two options:
change terminal to git-bash.exe and get used to slash as path separator(and that is much saner than back slashes, trust me)
just configure Intellj Terminal to open cmder, with default task like this: cmd /k ""%ConEmuDir%\..\init.bat" ". This will open cmder in the current folder, and you also have git support. That is, stop using mingw64 and only ConEmu + clink. You have color support, Windows backslashes and so on. And you also can mix Win commands with bash syntax(with "Shell integration" ticked). This is what I am doing right now. Branch info is shown, lambda symbol as well. It is already perfect.

git-bash.exe: how to run single command?

I am using the portable version of Git for Windows, but when I use git-bash.exe to run a single command, it looks like it does not work. Can you help me find out where is the problem?
I have a PowerShell script to call below command. But looks like it does not work. I am not able to let the new cmd window to pause to see the error log:
cmd /c "d:\git\git-bash.exe dos2unix d:\test\my-script.sh"
But If double click and run bash-exe.sh, then in the git-bash.exe window type
dos2unix d:\test\my-script.sh
then it works.
There are a few different ways to call EXEs from PowerShell. One of the best ways I've found that allows you the most control is the Start-Process cmdlet. Instead of calling a cmd.exe window try this:
Start-Process -FilePath 'd:\git\git-bash.exe' -ArgumentList 'dos2unix d:\test\my-script.sh'
Better yet convert that shell script into native PowerShell! :)
Anyway, I got a walkaround. In the portable git, under \usr\bin, there is a exe called dos2unix.exe, so that I can directly call dos2unix.exe from my powershell scripts, not from git-bash.exe then call dos2unix command.
cmd /c "d:\git\usr\bin\dos2unix.exe d:\test\my-script.sh"
With this walkaround, the problem resolved. But I still don't know why powershell calls dos2unix to parse my file does not work (I mean the file is not converted after call that command).
Another solution is use the base.exe, e.g:
"%ProgramFiles%\Git\bin\bash.exe" start.sh

Adding cmdline (win7) as an Eclipse console view

I've seen it in linux. There's a console you can open that is just a shell. I want the same thing in windows only with either cmd, or with git bash. (MINGW32). I tried googling it but just got flooded with articles about egit and normal git bash.
If you have msysgit, you can use it from a regular cmd session:
Launch c:\windows\system32\cmd.exe /k <path\to\your\msysgit>\git-cmd.bat, and you can type in that cmd windows regular git commands.
For a bash session: c:\windows\system32\cmd.exe /k <path\to\your\msysgit>\git-bash.bat.
Note that you need to modify those bat:
git-cmd.bat: add a rem before the setlocal command, and before the start at the end.
git-bash.bat: add a rem before the setlocal command.
So you can add to your Eclipse an external tool which will open a cmd in your console:
Except, instead of calling C:\windows\system32\cmd.exe (in the "Location" field from the picture above), you could call directly: C:\windows\system32\cmd.exe /k <path\to\your\msysgit>\git-bash.bat.
By 'msysgit', I refer to whatever name you gave to the uncompresdsed portable version of "Git For Windows": download it there.
And then you could type git command within the Eclipse console (including a bash session)!
After testing, the color codes get in the way, even if git works:
git-cmd.bat:
git-bash.bat:

Windows scheduled task to git push to github

I hope to add a Windows Scheduled Task to git push to github every night. I have a CMD file. When I run the CMD file on the windows command prompt, it works fine. But when I run it via windows scheduled task. It's stuck forever. The status is "running". And from the log I can see it successfully started the git bash shell. Any idea?
echo git push > i:\gitpush
echo 'pushing' >>log1
C:\WINDOWS\SysWOW64\cmd.exe /c ""C:\Program Files (x86)\Git\bin\sh.exe" --login i:\gitpush" >>log1 2>>error
echo 'done pushing' >>log1
del i:\gitpush
Here is the log output:
'pushing'
Welcome to Git (version 1.7.4-preview20110204)
Run 'git help git' to display the help index.
Run 'git help <command>' to display help for specific commands.
Then I did an experiment to rename gitpush script to a wrong file name. And it exited immediately with the error "No such file or directory", which is expected. It shows the gitpush script is passing in correctly to the bash but for some reason it's stuck.
The reason I have to go through git bash shell is because I don't know how to setup public key in windows command line shell without using git bash shell.
Thanks!
I don't know how to setup public key in windows command line shell
Public/private keys work also in a DOS shell, provided you define the %HOME% environment variable (referencing the parent directory of the .ssh)
The trick with Windows Scheduled Task is to make sure:
who is actually running the task (the "system account"? or the actual user?)
Displaying "env" can help debugging the issue.
where it is run: if git push depends on the current path to properly push the current repo, you need to be certain that your task runs where it is supposed to.

Resources