How to Run Unix command in Matlab in Windows? - linux

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.

Related

Using a batch file through Linux subsystem on Windows 10?

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.

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.

Shell script to run each command from script in different terminal

As I am new to Linux and using ubuntu and I want help to write a script which will be having multiple commands and for each command a respective terminal should be opened and execute the command.
As I have written a script which having commands like:
mvn tomcat:run -Dmaven.tomcat.port=8081
mvn tomcat:run -Dmaven.tomcat.port=8181
So all these two command should be run in different terminal.
Depending on your Linux flavor there are a number of different terminals available, each of which has a potentially different mechanism for specifying a command.
However, it's pretty likely your Linux will have an xterm which will use a '-e' flag and take a command
e.g.
xterm -e "mvn tomcat:run -Dmaven.tomcat.port=8081"
For Ubuntu you can try
gnome-terminal -e "mvn tomcat:run -Dmaven.tomcat.port=808"

How to launch a shell script make with Linux on Windows

I have a shell script make with Linux with this code:
#!/bin/sh
awk '{ print $3,$2,$4,$1}' df_real_credit_network_B0.00_BC0.00_l33.33_day1 > credit_adj
awk '{ print $3,$2,$5,$1}' df_real_interbank_network_B0.00_BC0.00_l33.33_day1 > interbank_adj
The script creates two objects: "credit_adj" and "interbank_adj", necessary for others scripts for the software R. Obliviously Windows don't recognize the script, so I try to modify the extension ".sh" with ".bat" and I deleted the string "#!/bin/sh" from the script.
In this way the two objects are created but are empty, because windows don't recognize the instruction awk.
How can I create the two objects correctly?
Thanks
You cannot directly run a Unix shell script in windows, One way to run unix/Linux shell script in windows is , install cygwin in windows and run script inside cygwin

Can I run a Linux shell script in Windows?

I created Shell Script on Linux and it runs fine.
Now I want to run the same script on Windows using gitbash. (Only filepath changed for windows)
I am confused:
do I need to write a new shell script again according to Windows syntax shell script?
or
can a Linux syntax shell script run on Windows without changes?
According TO differences-between-windows-batch-and-linux-bash-shell-script-syntax
Of course you can. There is a tool called cygwin that allows you to do so.
Note that you have to check what the paths are. If so, go to the path you are willing to work on and do pwd. This way, you will get the Windows\kind\of\path.
I use it all the time and it works pretty fine.
You can use Git Bash
It depends on how advanced the scripts are, but simple scripts can be executed in Git Bash.
test.sh contains:
#!/bin/bash
echo Hello World!
Execute script:
./test.sh
Output:
Hello World!
Git Bash vs Cygwin
To answer your question:
#fedorqui in my learning 'cygwin' And 'gitbash' do same stuff for
windows
Git Bash
Git Bash is lightweight and only aims to handle:
version control
a shell that runs commands
Read more: http://openhatch.org/missions/windows-setup/install-git-bash
Cygwin
a large collection of GNU and Open Source tools which provide
functionality similar to a Linux distribution on Windows.
a DLL (cygwin1.dll) which provides substantial POSIX API
functionality.
Read more: https://www.cygwin.com/

Resources