Using a batch file through Linux subsystem on Windows 10? - linux

I have a batch file that bashes into the linux subsystem I have on Windows 10 that tries to execute commands via the linux system. However, it doesn't execute any commands after the bash command. Here is an example:
bash
cd Documents/CS/DS
This just bashes into whatever directory the file is run from instead of CS/DS consistently. Is there anyway to have the batch file execute the rest of the commands?

When you run bash like that, you are sending execution into that executable. Launch bash as a separate process:
`start "bash" bash.exe`
Bash won't execute the rest of your cmd script. Cmd.exe processes cmd/bat files, bash only executes bash commands and scripts.

Related

How to make Jmeter run command from CMD - OS sampler - JMETER

I am struggling with jmeter for running cmd command to remote desktop.
I use the OS sampler.
I have a command that if I run it from the command line from my computer it worked, but when I try to make jmeter to run it, it failed - I use the same computer.
the command is:
plink -ssh jenkins#178.27.288.288 -pw passtest sudo /opt/test/test.sh
I added an OS sampler as followed:
the working directory is the cmd.exe directory.
and the command is the full command I want jmeter to run, the same one that working perfectly on cmd direct.
I get this error:
Response message: Exception occurred whilst executing system call: java.io.IOException: Cannot run program "plink -ssh jenkins#178.27.288.288 -pw passtest sudo /opt/test/test.sh" (in directory "c:\windows\system32"): CreateProcess error=2, The system cannot find the file specified
I do not know which file jmeter wants, I just want him to run the command, to connect to remote computer and run the command, the command working perfectly in command line and run the script in the remote computer.
I found this blog in the net:
https://www.blazemeter.com/blog/how-run-external-commands-and-programs-locally-and-remotely-jmeter
that said that it can be done, but since it is easy they not displayed step by step instructions.
when I change the command to dir, it run OK and the results are like I write dir in command line.
How can I make jmeter open the command line and just put the command and press enter, and display the results?
In the command you need to give the name of the programme to execute. In case of command prompt it is cmd.exe and for shell i think it is powershell.exe
Rest need to be pass as command parameters.
Check below for command parameters:-
The OS Process Sampler is a sampler that can be used to execute
commands on the local machine. It should allow execution of any
command that can be run from the command line. Validation of the
return code can be enabled, and the expected return code can be
specified. Note that OS shells generally provide command-line parsing.
This varies between OSes, but generally the shell will split
parameters on white-space. Some shells expand wild-card file names;
some don't. The quoting mechanism also varies between OSes. The
sampler deliberately does not do any parsing or quote handling. The
command and its parameters must be provided in the form expected by
the executable. This means that the sampler settings will not be
portable between OSes.
Many OSes have some built-in commands which are not provided as
separate executables. For example the Windows DIR command is part of
the command interpreter (CMD.EXE). These built-ins cannot be run as
independent programs, but have to be provided as arguments to the
appropriate command interpreter.
For example, the Windows command-line: DIR C:\TEMP needs to be
specified as follows:
Command: CMD Param 1: /C Param 2: DIR Param 3: C:\TEMP
If the commands are remote commands then try with "Remote Commands: Linux/MacOSX" with SSH sampler shown in the blog you shared.
Below screenshot for simple execution of dir command:-
--update--
additional screenshots
to execute a windows commad DIR c:\test use below settings

Errant error output behavior with perl execute bash scripts on a remote machine

I have this line in my perl script that sshes into a remote machine and execute a bash script:
system("ssh -t remotemachine /dir/dir/bashscript");
In my bash script, I used exit code 2 some commands 2 >> error.txt to capture any errors that I may encounter and I want this error.txt to be written in the same folder where the bash script is stored.
My problem is when I ssh into the machine and run the program from the terminal, the error can be captured and written in error.txt but if I run the program from my perl, the program is able to run but the error is not captured.
Any suggestions?
Use the full path for the capture file.
some commands 2 >> /dir/dir/error.txt
Otherwise the file will be created in the users $HOME if it exists.

Setup bash shell or command prompt to run Python project [duplicate]

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 6 years ago.
Want to execute the following commands from any single script. How to do this?
Whenever I'm starting my putty session, on my unix machine, bash shell is coming, then I need to enter the following commands to setup my workspace.
Below is the exact procedure which I'm doing.
-bash-4.2$ bash
[userName#SystemName ~]$ su
getting admin rights
[root#SystemName userName]#
setting up environment variable for python project
[root#SystemName userName]# source xxxx.env
setting the path for my local workspace
[root#SystemName userName]# setxxxx /home/userName/SourceCode/
Now, I want all these commands to get executed from single script.
Thus, I had putted all these commands into a single shell script and tried to execute but only first instruction got executed. Why?
start.sh
#!/bin/bash
bash
su
source ssdt.env
setssdt /home/userName/projectName/
The first command in your script is bash, which opens a new shell that will run until you exit from it, before it will continue executing the other commands in your script. If you're just trying to make the script execute with bash, that's what the #! line at the top does. The su command will also open a subshell that will have to exit before the other commands will be run with normal privileges.

Why does a bash script require an execute bit if a windows batch script can just be executed?

Yesterday I ran into the git execute bit bash script quirk - the one that requires:
git update-index --add --chmod=+x scriptname.sh
and it seemed strange to me that it was even possible to get stuck in this situation. (Ie having created a script file that you don't have permission to run).
If I have created a shell script - surely I can run it under the permissions of the shell execute permissions. Why would it need it's own execute permission bit?
My question is: Why does a bash script require an execute bit if a windows batch script can just be executed?
To run a script you have two options in unix like systems. First Option is to use a direct interpreter call with the script as parameter.
# run a bash script
bash test.sh
# run a python scripts
python test.py
The second option is mark your file as executable, with the execute bit and after a call like this ...
# sample bash
./test.sh
# sample python
./test.py
... your system tries to find the right interpreter for you. For this the first line 'shebang' of the script is used.
Bash example:
#!/bin/bash
# points to the installed bash interpreter - bash example
Python example:
#!/usr/bin/python
# points to the installed python interpreter
To your question windows only use the file extension to detect a executable file.
Well, Linux is not Windows. Linux/Unix file systems support the executable bit to distinguish executable from pure data files, and to control exec permissions for user|group|others. You can still run the script if you prefix it with the name of the shell/binary you want to start it with, but if you want to do ./scriptname.sh or execute it from the path it needs to be flagged as executable for you as the onwer|a group member|some other user, and for scripts usually the shebang in the first line that defines the interpreter to start the script with: #!/bin/bash.

How to Run Unix command in Matlab in Windows?

In this MATLAB code, command is identified for UNIX platforms. However, I am using Win7. How can I run that command in Matlab in Windows?
command = ['Code/ExternalCode/kmeans/./kmeans_clustering.sh -i ' 'TemporaryResults/Features_ForKmeans' ' -p 2 -d -n ' num2str(k)];
system(command);
labels_kmeans = dlmread('TemporaryResults/Features_ForKmeans.membership');
labels_kmeans(:,1) = [];
You have at least two options, both assume the commands which are executed within the script are runnable on Windows, i.e. the programs that are executed exist and are compiled for Windows.
1.) Try to run the unmodified shell/bash script on Windows:
You need to install an interpreter which can run your script on windows, have a look at this SO question: Is there a way to run Bash scripts on Windows?
2.) Re-write the script to Windows batch format
This depends on the actual script you are running and involves finding the batch equivalent commands which correspond to the ones contained in your .sh script.

Resources