Azure CLI in Git Bash - bash

I am trying to use a bash (sh) script on windows to run a test deployment. I am running the script from the gitbash console so that I have a copy of bash, but doing so means that the azure clie is not available (i.e. azure command is not found). Does anyone know how I can get the Azure cli working in GitBash (I am assuming I just install it somewhere else) or should I change to a different way of using bash

Sometimes commands in windows git bash need .cmd appended. Also, another way of installing the Azure-Cli is through Chocolatey https://chocolatey.org/
Try this command after Azure-Cli is installed:
az.cmd --version
Echoing mscrivo you can run the line below in CMD not PowerShell (elevated/admin)
echo "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15} > "C:\Program Files\Git\mingw64\bin\az"
Now you should be able to run in Git bash:
az --version

artberri noted the best solution in a comment:
Add the following to your %USERPROFILE%\.bashrc or %USERPROFILE%\.profile
alias az='az.cmd'
However, if you want to be able to use az in bash scripts, you'll need something a little more drastic, run the following from cmd prompt:
echo "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15} > "%SYSTEMROOT%\az"
That will essentially create a passthrough az command in your windows folder that can be run from anywhere and passes parameters through to az.cmd.

In case using Git bash, navigate to the following directory:
C:\Program Files\Git\etc\profile.d
Edit aliases.sh, then add a new alias for az as below:
alias az='az.cmd'

You have to install the CLI to your computer.
There are multiple ways to do that.
I'm a friend of NodeJS so i use npm for the installation:
npm install -g azure-cli
More details here: https://www.npmjs.com/package/azure-cli
But you can do it also in other ways. A very nice way is to use docker. There are containers from Microsoft with a preinstalled version of Azure CLI.
docker run -it --name azure microsoft/azure-cli
On Windows 10 with the ubuntu bash you can use:
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ wheezy main" | \
sudo tee /etc/apt/sources.list.d/azure-cli.list
sudo apt-key adv --keyserver packages.microsoft.com --recv-keys 417A0893
sudo apt-get install apt-transport-https
sudo apt-get update && sudo apt-get install azure-cli
Or as a python enthusiast run
pip install --user azure-cli
Most important is that the "az"/"az.bat" or "azure" bin is available via your path variable.

In other words Azure CLI for Windows is not compatible with Git Bash for Windows
https://github.com/Azure/azure-cli/issues/3445

In your ~/bin directory (in Windows it means c:\Users\<username>\bin) I've just created a file named az with:
"C:/Program Files (x86)/Microsoft SDKs/Azure/CLI2/python.exe" -IBm azure.cli "$#"
Then make the file executable
chmod a+x az
The content is "borrowed" from az.cmd.
When trying an alias approach mentioned before I had a problem with long commands, with a lot of parameters throwing an error
"'C:\Program' is not recognized as an internal or external command,operable program or batch file."
EDIT:
I've ended using Ubuntu through WLS. For all tools like az, terraform, kubectl, istioctl. The az tool runs good in interactive mode as well.

So I arrived here looking for a way to run the same az commands in a bash shell script on Azure DevOps Linux and Windows (git bash) build agents, so I could share the same code across both types of agent. This also works for a git bash shell on Windows 10. My longer answer is here: Azure DevOps Build Pipeline can't get secrets from Key Vault when secured with vnet and firewall
The gist of it is:
if [[ $(uname -s) == "Linux" ]]; then
azcmd="az"
else
# If we're in a bash shell on Windows, az commands don't work, but we can call the az.cmd batch file directly from git Bash if we can find it...
azcmd=$(where az.cmd)
fi
# Remember to specify CIDR /32 for removal
"$azcmd" keyvault network-rule remove -n <MyKeyVault> --ip-address 50.1.1.1/32
Basically just substitute "$azcmd" wherever you'd normally use az once the bootstrap code has executed.

My previous approach for this was just to add the Azure CLI Scripts folder to the $PATH inside my ~/.bashrc file. But, after updating Azure CLI to 2.2.0 via the MSI, this approach started to fail with this error:
C:\Program: can't open file 'Files': [Errno 2] No such file or directory
So, to fix this, I have my $PATH in ~/.bashrc include ~/bin and then I created a file with the following content as ~/bin/az (don't forget to chmod 0755 the new file):
#!/usr/bin/env bash
AZURE_CLI_PATH="/c/Program Files (x86)/Microsoft SDKs/Azure/CLI2"
export PATH="${PATH}:${AZURE_CLI_PATH}:${AZURE_CLI_PATH}/Scripts"
export PYTHONPATH="${AZURE_CLI_PATH}/src"
export PYTHON_CMD="$(which python)"
winpty "${PYTHON_CMD}" -m 'azure.cli' "${#}"
After closing out my GIT Bash window and re-opening it, I can now run az again:
$ az version
This command is in preview. It may be changed/removed in a future release.
{
"azure-cli": "2.2.0",
"azure-cli-command-modules-nspkg": "2.0.3",
"azure-cli-core": "2.2.0",
"azure-cli-nspkg": "3.0.4",
"azure-cli-telemetry": "1.0.4",
"extensions": {}
}

Don't use the MSI installer at all. Since the Azure CLI is implemented in Python, use the Python installation method as #blndev wrote. This way instead of az.cmd you get az.bat and az shell script, and the installation path will not contain spaces.
pip install --user azure-cli
More detailed info on this method can be found at https://blogs.msdn.microsoft.com/brijrajsingh/2017/03/02/installing-azure-cli-2-0-on-windows/
The symlink worked for me most of the times, but some commands are still failing, e.g.
az dls fs access set-entry ...
'C:\Program' is not recognized as an internal or external command, operable program or batch file

I tried #mscrivo's solution. When using the az command in a shell script however you still have problems due to the spaces in the path.
Therefor I created a azproxy.cmd in %SYSTEMROOT% containing
#echo off
"C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" %*
And then create the mklink to that file
mklink "%SYSTEMROOT%\az" "SYSTEMROOT%\azproxy.cmd"
PS the expanded value of %SYSTEMROOT% should not contain spaces of course

Related

How to run a .sh file that has hdiutil, sudo, and wget for Visual Studio Code(windows)?

Currently I'm stuck at figuring out why a .sh script isn't running:
export DOWNLOAD_LOCATION=~/Downloads/
export VAGRANT_URL=https://releases.hashicorp.com/vagrant/2.2.6/vagrant_2.2.6_x86_64.dmg
export VIRTUALBOX_URL=https://download.virtualbox.org/virtualbox/6.1.2/VirtualBox-6.1.2-135662-OSX.dmg
echo "===== Install Vagrant ====="
wget $VAGRANT_URL -P $DOWNLOAD_LOCATION
hdiutil attach $DOWNLOAD_LOCATION"vagrant_2.2.6_x86_64.dmg"
sudo installer -pkg /Volumes/Vagrant/vagrant.pkg -target /
echo "===== Install VirtualBox ====="
wget $VIRTUALBOX_URL -P $DOWNLOAD_LOCATION
hdiutil attach $DOWNLOAD_LOCATION"VirtualBox-6.1.2-135662-OSX.dmg"
sudo installer -pkg /Volumes/VirtualBox/virtualbox.pkg -target /
echo "===== Install MiniKF ====="
vagrant init arrikto/minikf
vagrant up
Windows is telling that:
./install.sh: line 9: wget: command not found
./install.sh: line 10: hdiutil: command not found
./install.sh: line 11: sudo: command not found
I have wget installed in Visual studio, do I need to install it for windows?
why a .sh script isn't running
It is calling commands, which are not available, namely wget, hdiutil, sudo.
There exist GNU Wget for Windows, which is wget.exe file and after placing in current working directory calls for wget and wget.exe should work, however hdiutil is more important here. My cursory search did not unveil hdiutil for Windows, so even if you make wget working you still need find some way to use .dmg file in your Windows system.

Git bash for windows to change directory and exec a script

I am new to bash and trying to run the following script on Git Bash for Windows, on Windows Server 2022:
"C:\Program Files\Git\bin\bash.exe" -c C:/Users/agentSvc/AppData/Roaming/Composer/vendor/debricked/cli && php bin\console debricked:scan '' 123 gitHubOrg/repoName 456 https://github.com/repo local
I have falsified some of the values.
The documentation is here:- https://debricked.com/docs/integrations/cli.html
However, I get errors such as "Is a directory", etc. What is the correct syntax here? I am trying to change directory as per the documentation and then run the command.

How to run jq from gitbash in windows?

I have gitbash in Windows. I am trying to run jq but its giving me error.
$ ./jq-win64.exe
jq
parse error: Invalid numeric literal at line 2, column 0
Intention: I want to use jq to parse json.
Easiest solution and always latest version:
run this curl in your gitbash:
curl -L -o /usr/bin/jq.exe https://github.com/stedolan/jq/releases/latest/download/jq-win64.exe
or manually save the jq-win64.exe in link above as jq.exe to your /usr/bin (which is in your git bash installation folder)
(if you are behind a proxy add the -x proxyhost:port)
Using jq-win64.exe from github.com/stedolan/jq/releases, I get
vonc#voncav MINGW64 /d/prgs/dl
$ ./jq-win64.exe --version
jq-1.6
vonc#voncav MINGW64 /d/prgs/dl
$ echo '{"foo": 0}' | ./jq-win64.exe .
{
"foo": 0
}
So it does work, but it then depends on the json document you are parsing with it.
If that json document is not well-formed, that would generate the error you see.
In your bash session, you can define (or add to your ~/.bashrc) an alias:
alias jq=/path/to/jq-win64.exe
That way, you don't need to use ./jq, but directly jq.
$ echo '{"foo": 0}' | jq
In my case:
vonc#voncav:/$ alias jq=/mnt/d/dwnl/jq-win64.exe
vonc#voncav:/$ echo '{"foo": 0}' | jq
{
"foo": 0
}
I hate answers that say you need another to use another tool to download, but using https://chocolatey.org/ made this incredibly simple for me.
From an elevated command-prompt, run:
choco install jq
jq magically works from bash going forward.
Instead of using chocolatey (with elevated rights) you could also use scoop.sh:
scoop install jq
Below steps worked for me in git bash on windows 10
curl -L -o jq-win64.exe https://github.com/stedolan/jq/releases/latest/download/jq-win64.exe
export PATH=$PATH:"/C/Users/devops/Downloads/jq-win64.exe
jq --version
jq-1.6
For windows Powershell && cmd
Download executable -> jq 1+ executables for 64-bit
Create folder in Program Files -> JQ
Copy .exe into program folder
Set environment variable to that folder
Restart terminal
Set alias
Set-Alias -Name jq -Value .\jq-win64.exe
Run command
echo '{"foo": 0}' | jq
Output
{
"foo": 0
}
enjoy!
I just downloaded the binary to %HOMEPATH%/bin/jq-win64 and it worked right away via jq
In my case, I have fixed it in Windows10 while practicing a scenario of "Techworld with Nana" course, as per the below steps:
1) open "cmd" prompt (Run as administrator)
2) curl -L -o /usr/bin/jq-win64.exe
Download it here
3) You can add C:\Program Files\Git\usr\bin path to your environment variables. If you follow this step, ignore the below steps.
[OR]
3) Go to the directory where jq is downloaded: C:\Program Files\Git\usr\bin and copy the file to C:\ drive
4) Add the path C:\ to "path" environment variable. This PC-->Properties-->Advanced system settings --> Environment Variables -->Edit-->New. Then add the path (where you copied jq.exe, i.e., C:) and Save it.
5) Now open a new "cmd" prompt, jq --version

skip installing confirm('yes' or 'no') in Dockerfile [duplicate]

How do I install the anaconda / miniconda without prompts on Linux command line?
Is there a way to pass -y kind of option to agree to the T&Cs, suggested installation location etc. by default?
can be achieved by bash miniconda.sh -b (thanks #darthbith)
The command line usage for this can only be seen with -h flag but not --help, so I missed it.
To install the anaconda to another place, use the -p option:
bash anaconda.sh -b -p /some/path
AFAIK pyenv let you install anaconda/miniconda
(after successful instalation)
pyenv install --list
pyenv install miniconda3-4.3.30
For a quick installation of miniconda silently I use a wrapper
script script that can be executed from the terminal without
even downloading the script. It takes the installation destination path
as an argument (in this case ~/miniconda) and does some validation too.
curl -s https://gist.githubusercontent.com/mherkazandjian/cce01cf3e15c0b41c1c4321245a99096/raw/03c86dae9a212446cf5b095643854f029b39c921/miniconda_installer.sh | bash -s -- ~/miniconda
Silent installation can be done like this, but it doesn't update the PATH variable so you can't run it after the installation with a short command like conda:
cd /tmp/
curl -LO https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -u
Here -b means batch/silent mode, and -u means update the existing installation of Miniconda at that path, rather than failing.
You need to run additional commands to initialize PATH and other shell init scripts, e.g. for Bash:
source ~/miniconda3/bin/activate
conda init bash

Bash on Windows - alias for exe files

I am using the Bash on Ubuntu on Windows, the way to run bash on Windows 10. I have the Creators update installed and the Ubuntu version is 16.04.
I was playing recently with things as npm, node.js and Docker and for docker I found it is possible to install it and run it in windows and just use the client part from bash, calling directly the docker.exe file from Windows's Program Files files folder. I just update my path variable to include the path to docker as PATH=$PATH:~/mnt/e/Program\ Files/Docker/ (put in .bashrc) and then I am able to run docker from bash calling docker.exe.
But hey this bash and I dont want to write .exe at the end of the commands (programs). I can simply add an alias alias docker="docker.exe", but then I want to use lets say docker-compose and I have to add another one. I was thinking about adding a script to .bashrc that would go over path variable and search for .exe files in every path specified in the path variable and add an alias for every occurance, but it does not seem to be a very clean solution (but I guess it would serve its purpose quite well).
Is there a simple and clean solution to achieve this?
I've faced the same problem when trying to use Docker for Windows from WSL.
Had plenty of existing shell scripts that run fine under Linux and mostly under WSL too until failing due to docker: command not found. Changing everywhere docker to docker.exe would be too cumbersome and non-portable.
Tried workaround with aliases in ~/.bashrc as here at first:
shopt -s expand_aliases
alias docker=docker.exe
alias docker-compose=docker-compose.exe
But it requires every script to be run in interactive mode and still doesn't work within backticks without script modification.
Then tried exported bash functions in ~/.bashrc:
docker() { docker.exe "$#"; }
export -f docker
docker-compose() { docker-compose.exe "$#"; }
export -f docker-compose
This works. But it's still too tedious to add every needed exe.
Finally ended up with easier symlinks approach and a modified wslshim custom helper script.
Just add once to ~/.local/bin/wslshim:
#!/bin/bash -x
cd ~/.local/bin && ln -s "`which $1.exe`" "$1" || ln -s "`which $1.ps1`" "$1" || ln -s "`which $1.cmd`" "$1" || ln -s "`which $1.bat`" "$1"
Make it executable: chmod +x ~/.local/bin/wslshim
Then adding any "alias" becomes as easy as typing two words:
$ wslshim docker
+ cd ~/.local/bin
++ which docker.exe
+ ln -s '/mnt/c/Program Files/Docker/Docker/resources/bin/docker.exe' docker
$ wslshim winrm
+ cd ~/.local/bin
++ which winrm.exe
+ ln -s '' winrm
ln: failed to create symbolic link 'winrm' -> '': No such file or directory
++ which winrm.ps1
+ ln -s '' winrm
ln: failed to create symbolic link 'winrm' -> '': No such file or directory
++ which winrm.cmd
+ ln -s /mnt/c/Windows/System32/winrm.cmd winrm
The script auto picks up an absolute path to any windows executable in $PATH and symlinks it without extension into ~/.local/bin which also resides in $PATH on WSL.
This approach can be easily extended further to auto link any exe in a given directory if needed. But linking the whole $PATH would be an overkill. )
You should be able to simply set the executable directory to your PATH. Use export to persist.
Command:
export PATH=$PATH:/path/to/directory/executable/is/located/in
In my windows 10 the solution was to install git-bash and docker for windows.
in this bash, when I press "docker" it works
for example "docker ps"
I didnt need to make an alias or change the path.
you can download git-bash from https://git-scm.com/download/win
then from Start button, search "git bash".
Hope this solution good for you

Resources