how to execute this shell script - windows

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.

Related

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.

Unable to execute shell script in Cygwin as a KornShell script

I rarely touch shell scripts, we have another department who write them, so I have an understanding of writing them but no experience. However they all appear rather useless with my issue.
I am trying to execute some KornShell (ksh) scripts on a windows based machine using Cygwin- we use these to launch our Oracle WebLogic servers, now it simply will not execute. I used to be able to execute these exact same scripts fine on my old machine.
Now I have narrowed this down to the fact the 'magic number' or whatever it is at the start of the script where it specifies the script interpreter path:
i.e.:
#!/bin/ksh
if I change it to execute as a simple bash it works i.e:
#!/bin/sh
I went through checking the packages installed for cygwin - now the shells I installed are:
mksh MirdBSD KornShell
bash the bourne again shell
zsh z shell
Should I expect to see a ksh.exe in my cygwin/bin directory? there is a system file 'ksh' which I was making an assume somehow associates it with one of the other shell exes, like mksh.exe
I understand my explanation may well be naff. But that being said, any help would be very much appreciated.
Thanks.
I believe the MirBSD korn shell is called mksh. You can verify this and look for the correct path by typing
% which mksh
% which ksh
or if you have no which,
% type -p mksh
% type -p ksh
or if that fails too, check /etc/shells which should list all valid shells on a system:
% grep ksh /etc/shells
You need to put the full path after the #! line. It will probably be /bin/mksh, so your line needs to look like:
#!/bin/mksh
You've probably fixed it by now, but the answer was no, your Cygwin does not (yet) know about ksh.
I solved this problem by launching the cygwin setup in command-line mode with the -P ksh attribute (as described in http://www.ehow.com/how_8611406_install-ksh-cygwin.html).
You can run a ksh using a bat file
C:\cygwin\bin\dos2unix kshfilename.ksh
C:\cygwin\bin\bash kshfilename.ksh
Running a shell script through Cygwin on Windows
Install KornShell (ksh) into Cygwin by the following process:
Download: ksh.2012-08-06.cygwin.i386.gz
Install ksh via Cygwin setup.
Execture Cygwin setup.exe
Choose: Install from Local Directory
Select the ksh.2012-08-06.cygwin.i386.gz as the Local Package Directory.
Complete Cygwin setup.
Restart Cygwin.

Activating a VirtualEnv using a shell script doesn't seem to work

I tried activating a VirtualEnv through a shell script like the one below but it doesn't seem to work,
#!/bin/sh
source ~/.virtualenvs/pinax-env/bin/activate
I get the following error
$ sh virtualenv_activate.sh
virtualenv_activate.sh: 2: source: not found
but if I enter the same command on terminal it seems to work
$ source ~/.virtualenvs/pinax-env/bin/activate
(pinax-env)gautam#Aspirebuntu:$
So I changed the shell script to
#!/bin/bash
source ~/.virtualenvs/pinax-env/bin/activate
as suggested and used
$ bash virtualenv_activate.sh
gautam#Aspirebuntu:$
to run the script .
That doesn't throw an error but neither does that activate the virtual env
So any suggestion on how to solve this problem ?
PS : I am using Ubuntu 11.04
TLDR
Must run the .sh script with source instead of the script solely
source your-script.sh
and not
your-script.sh
Details
sh is not the same as bash (although some systems simply link sh to bash, so running sh actually runs bash). You can think of sh as a watered down version of bash. One thing that bash has that sh does not is the "source" command. This is why you're getting that error... source runs fine in your bash shell. But when you start your script using sh, you run the script in an shell in a subprocess. Since that script is running in sh, "source" is not found.
The solution is to run the script in bash instead. Change the first line to...
#!/bin/bash
Then run with...
./virtualenv_activate.sh
...or...
/bin/bash virtualenv_activate.sh
Edit:
If you want the activation of the virtualenv to change the shell that you call the script from, you need to use the "source" or "dot operator". This ensures that the script is run in the current shell (and therefore changes the current environment)...
source virtualenv_activate.sh
...or...
. virtualenv_activate.sh
As a side note, this is why virtualenv always says you need to use "source" to run it's activate script.
source is an builtin shell command in bash, and is not available in sh. If i remember correctly then virtual env does a lot of path and environment variables manipulation. Even running it as bash virtualenv_blah.sh wont work since this will simply create the environment inside the sub-shell.
Try . virtualenv_activate.sh or source virtualenv_activate.sh this basically gets the script to run in your current environment and all the environment variables modified by virtualenv's activate will be available.
HTH.
Edit: Here is a link that might help - http://ss64.com/bash/period.html
On Mac OS X your proposals seems not working.
I have done it this way. I'am not very happy with solution, but share it anyway here and hope, that maybe somebody will suggest the better one:
In activate.sh I have
echo 'source /Users/andi/.virtualenvs/data_science/bin/activate'
I give execution permissions by: chmod +x activate.sh
And I execute this way:
`./activate.sh`
Notice that there are paranthesis in form of ASCII code 96 = ` ( Grave accent )
For me best way work as below.
Create start-my-py-software.sh and pest below code
#!/bin/bash
source "/home/snippetbucket.com/source/AIML-Server-CloudPlatform/bin/activate"
python --version
python /home/snippetbucket.com/source/AIML-Server-CloudPlatform/main.py
Give file permission to run like below.
chmod +x start-my-py-software.sh
Now run like below
.start-my-py-software.sh
and that's it, start my python based server or any other code.
ubuntu #18.0
In my case, Ubuntu 16.04, the methods above didn't worked well or it needs much works.
I just made a link of 'activate' script file and copy it to home folder(or $PATH accessible folder) and renamed it simple one like 'actai'.
Then in a terminal, just call 'source actai'. It worked!

Shell script problem to set my env

We have few executable which need some environment setting.
We manually running those scripts before running the executable
Like
$ . setenv.ksh
We have to encompass call these in one script to avoid the manual work.
We written a sh script like
#!/bin/sh
. setenv.ksh
./abc &
Still the environments are not setting in that session. I think the “. setenv.ksh” runs with fork and it’s not setting the environment.
Please me to solve this problem. Which command we use to run the setenv.ksh so, this will work fine.
Thanks
I notice the environment script is called setenv.ksh but you try to run it from /bin/sh. Maybe your system has a shell other than ksh as /bin/sh and it misparses something it setenv.ksh. Try changing the shebang line to #!/bin/ksh (or whatever the path to ksh is on your system).
In setenv.ksh, you need to export all environment variables you set so that any sub-shell will inherit the values:
export MYENV=myValue

Can I use cygwin to script a hudson build step?

I've tried executing the following:
#!C:\cygwin\bin\bash.exe
ls ${WORKSPACE}
But that doesn't find ls (even if it's on the windows path). Is there any way to set this up?
UPDATE: In other words, I want to be able to set up a build step that uses cygwin bash instead of windows cmd like this page shows you how to do with Python.
So put your cygwin's bin directory in your PATH.
In case you don't know how to do it (Control Panel -> System -> Advanced -> Environment Variables), see: http://support.microsoft.com/kb/310519
That shell-script has two errors: the hash-bang line should be "#!/bin/bash", and ${WORKSPACE} is not a shell-variable. Hudson has a bunch of variables of its own which are expanded in the commands you specify to be run (i.e. when you add commands in the web gui).
If you want to run Hudson build step on the Cygwin command line, you need to figure out what command Hudson runs and in which directory.
To give a more specific answer, you need to show us how your project is configured and what steps you want to run separately.
Provided cygwin's bin folder is in your path, the following works for me:
#!/bin/sh
ls ${WORKSPACE}
I find Hudson does not pick up environment variable changes unless you restart the server.
you might want to try to give a full path to ls
/cygdrive/c/cygwin/bin/ls
One other thing that seems to work is to use this:
#!C:\cygwin\bin\bash.exe
export PATH=$PATH:/usr/bin
ls
But it would be nice not to have to modify the path for every script.
Have you thought about power shell? as much as I like cygwin, it's always been a little flaky, powershell is a solid fully functional shell on windows, another option is Windows Services for UNIX it gives you korn shell or c shell not quite as nice as bash but it gets the job done
You will need to pass the --login (aka -l) option to bash so that it will source Cygwin's /etc/profile and set up the PATH variable correctly. This will cause the current directory to get changed to the default "home" but you can set the environment variable CHERE_INVOKING to 1 before running bash -l and it will stay in the current directory if you need to preserve that.

Resources