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
Related
I am trying to make a .sh file that when clicked it runs the script inside. I am trying to recursively find a certain string value inside the contents of the files from a given folder, using $ grep -r "word" /home/folder_name but I don't know how to do so without running the script in terminal.
Any ideas for this?
Linux shell scripts can be written very basically.To make a shell script, start the script with #!/bin/sh and add normal linux commands. That is a simple explanation, but it is sufficient for most simple scenarios.
Example:
#!/bin/sh
echo "Hello, World!`
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.
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.
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/
I am sending a .sh file created from a windows machine to a linux so that I could run it there. The problem is that I keep on getting an error called bad interpreter.But when I program the shell script in the linux machine it runs with no problems even though it has the same code with the one sent from the windows machine. After my ivestigation, I found out that the windows machine .sh script is a text/plain file(using file -bi) and the other one from the linux machine is a text/x.shellscript. Is there a way to convert the text/plain to a text/x.shellscript? thank you
this is the script:
#!/bin/bash
date
sudo apt-get update
I tried a solution by doing another .sh file in a linux box containing only
#!/bin/bash
Then the windows machine only sent a file containing test commands like :
date
hostname
Then I append the file from the windows box to the linux one with
cat windows.sh >> linux.sh
It did not work if I run linux.sh. It says errors like:
./linuxh.sh: line 2 $'date\r':command not found
./linuxh.sh: line 2 $'hostname\r':command not found
However, if I open Linux.sh then save it again without doing anything. It works
I'm summarising below the steps you need to take so other users can see easily what needs doing:
Firstly, you need to check your script has the correct path to your interpreter after the "#!" in the very first line. This is should probably be:
#!/bin/bash
or
#!/usr/bin/bash
and you can find which is correct by typing:
which bash
on your Linux box.
Secondly, you need to make sure that any Windows carriage returns (or "^M") at the ends of the lines are removed before expecting your Linux box to run the script. You can do this with:
dos2unix yourscript
Just for reference, you can easily see weird characters such as TABs or linefeeds or carriage returns in Linux by using:
cat -vet yourfile
or
sed -n l yourfile
Thirdly, you need to make sure your script is executable on Linux, using chmod like this:
chmod +x yourscript
Finally, when you have done all that, you need to either add the directory where the script is located to your PATH variable (and export it) or give the full path to your script like this if your script is in the current directory:
./yourscript
or like this if it is located somewhere else
/some/directory/some/where/yourscript