Jenkins: How to execute shell script in a Windows slave - shell

I have a Windows slave, with bash installed. I'm trying to run a shell script on it as a pre-build step. Any ideas on how I can do this?

You need to run bash with the script as an argument:
bash yourscript <arguments to your script here>
You may also need to supply the complete path to bash depending on your environment.

If your bash on Windows is part of a Cygwin install, the cygpath plugin will allow you to use 'Execute Shell' and do #!/bin/bash at the top of it, just like you would do for unix slaves.

Related

How to run SDKMAN on shell script

I am trying to run sdkman on a shell script that I call run.sh. This is what the inside of the shell script looks like:
sdk use java 8.0.302-open
When I run the command in a terminal, it works. But when I run it in a shell script, I get this error:
run.sh: 1: sdk: not found
Anyone knows how to fix this?
I fixed it for me; Although this may not work for others.
I placed #!/bin/bash at the top of the shell script, and then added this after it:
source "$HOME/.sdkman/bin/sdkman-init.sh"
Then my shell script was able to be ran using:
./run.sh
sdkman was able to work this time.

Why is $OSTYPE returning "linux-gnu" on Windows?

Problem
When I run a bash script on Windows:
bash my_script.sh
I get linux-gnu for $OSTYPE inside the script.
Why is this like this?
I assume that i have WSL installed is relevant here.
Tested in PowerShell and CMD.
Git bash is returning msys like expected! Thx #user1934428
Background
I want to start some python scripts from bash, but not inside WSL.
From my command line I reach different python versions on windows, but from inside the bash scripts it is using the one inside WSL (except for GitBash).
You're right, running the bash command in PowerShell or CMD will launch WSL to run your script. You confirm this (and see which version of WSL) by running cat /etc/issue in your bash script. Your WSL environment will have an independent set of environment variables (not just $OSTYPE). You can see this by comparing the output of Get-ChildItem -Path Env:\ in PowerShell to the output of env (after you launch bash from PowerShell).
I suspect that the python version discrepancy you're seeing is a result of the PATH variable in your WSL runtime not matching what you have set in your PowerShell environment. You can fix your version issue by setting an alias containing a path to the python executable you want to use by adding alias python=/c/path/to/python.exe to the start of your bash scripts.
Alternatively, you can use a tool like Cygwin or git-bash to run your scripts. I'm not sure if they will use the same path variables as Windows so you may need to set those manually too.

What is the function of git bash for windows?

Is git bash something that can be used as a windows alternative to terminal on ubuntu? Can we use cmd.exe for the same purpose?
Git bash exists as a minimal environment for running git on Windows. Git itself consists of a number of shell and Perl scripts as well as binaries. In order to run them you need an environment that can interpret shell scripts. A Windows command prompt can only interpret Batch files.
Note that git bash doesn't have much more than the shell, git, ls, and a few other utilities. If you need a more complete shell and tool suite then look at https://www.cygwin.com/ instead.
Git BASH provides a BASH emulation used to run Git from the command line. Unix users should feel right at home, as the BASH emulation behaves just like the "git" command in LINUX and UNIX environments.
Also it is used to run commands of Linux such as ls,cd etc. If you want to use commands in Windows go install GIT BASH.
You cannot use command prompt for same purpose.

bash command in server startup

I am trying to run a server. the command x_server.sh do not work for my ubuntu 14.04 but it runs when I give the command bash x_server.sh.
It has adviced that the products should not run as a daemon thread.
What I want to know is what exactly this bash command do, is it run as a daemon thread when i do so and what are the alternative ways there for me to use to make that command x_server.sh work.
On Ubuntu the default shell is Dash, not Bash. Presumably your x_server.sh script starts like this:
#!/bin/sh
You should change it to this:
#!/usr/bin/env bash
That will make it auto-select the best "bash" on the system rather than the default shell which is dash which has different (mostly fewer) features than bash.
And of course you need to do the usual chmod +x x_server.sh to make it executable in the first place, and run it as ./x_server.sh unless it's in your $PATH.

how to execute this shell script

I have a shell script written in eclipse
#!/bin/sh
#
# 07/28/2006. .sh file for the Hpims Cron job.
# Runs daily.
. /db2/db2inst1/sqllib/db2profile
APPHOME=/devl/prod/vehmgr/cronjob/HpimsCron
JAVA_HOME=/usr/java14
JAVA_EXEC=$JAVA_HOME/bin/java
JAVAC=$JAVA_HOME/bin/javac
#export APPHOME JAVA_HOME JAVA_EXEC JAVAC
export JAVA_HOME JAVA_EXEC JAVAC
cd $APPHOME
CLASSPATH=$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/jre/lib/i18n.jar:/appl/jConnect/classes
/jconn2.jar:/appl/net/jserv-1.1.2/libexec/jndi.jar
CLASSPATH=$CLASSPATH:/appl/net/jserv-1.1.2/libexec/mail.jar:/appl/net/jserv-1.1.2/libexec/mailapi.jar:/appl/net/jserv-1.1.2/libexec/activation.jar
CLASSPATH=$CLASSPATH:/appl/net/jserv-1.1.2/libexec/smtp.jar:/appl/net/jserv-1.1.2/libexec/soap.jar:/appl/net/jserv-1.1.2/libexec/ldap.jar
CLASSPATH=$CLASSPATH:/home/db2inst1/sqllib/function:/home/db2inst1/sqllib/java/db2java.zip
CLASSPATH=$CLASSPATH:.
CLASSPATH=$CLASSPATH:/devl/prod/vehmgr/cronjob/HpimsCron
export CLASSPATH
#cd $APPHOME
#$JAVAC HpimsCron.java
$JAVA_EXEC HpimsCron
Question is - how to execute this shell script on Windows XP. I have made changes to the HpimsCron.java file and now I need to run this shell script manually to see the changes reflected.
In general I'd do the following:
CLASSPATH=something
CLASSPATH=$CLASSPATH:something/else
export CLASSPATH
Becomes
set CLASSPATH=something
set CLASSPATH=%CLASSPATH%;something\else
(note, the ; instead of : and \ instead of /)
EDIT:
The call to dbprofile suggests another script that may not be portable and whether the app (HpimsCron) would work on Windows is also highly dubious.
Cygwin will allow you to run shell commands on Windows. This post How do you run a crontab in Cygwin on Windows? also explains how to get cron working on Windows under Cygwin.
This won't run under Windows, it's Unix shell specific
You might give Cygwin a try. It offers a Unix-like environment on a Windows system.

Resources