Do we need to modify .sh file while executing in Windows? - linux

I have one .sh file which runs well on MAC system. I want to run the same .sh file on windows using CYGWIN. My question is, Do I need to make any modification in the commands while using it in Windows system with CYGWIN? Please help.
e.g. I have the following commands in .sh file-
export ML_SERIALIZE_DIR=/Users/Mxyz/ML
export ML_SERIALIZE_GRAPH=true
echo ML_SERIALIZE_DIR = $ML_SERIALIZE_DIR
echo ML_SERIALIZE_GRAPH = $ML_SERIALIZE_GRAPH
java -DCL_LOG_DIR="/Users/Mxyz/ML" -classpath .:lib/:Ml-mobxyz-import.jar org.xy.mobxyz.mobxyz.ML
echo "Batch program is complete"

No need to change anything but make sure that all required binaries by your .sh must be there in CYGWIN

Related

How convert windows batch file to linux shell script?

I have a batch file and it has below command(.bat file) to execute my application. I need to move the same application to Linux environment and need to write .sh file to execute the same application. I don't have any idea of the shell scripting. Please can some one give idea to convert this to .sh file?
SET currentDir=%CD%
CD %~dp0
SET CLASSPATH=./lib/*;
java -Dlogback.configurationFile=./com/logback.xml -cp "%CLASSPATH%";"App.jar" com.test.main.MainClient
SET ERROR_LEVEL=%ERRORLEVEL%
CD %currentDir%
EXIT /B %ERROR_LEVEL%
Since it dosn't appear to be very complex, conversion shouldn't be too dificult. Have look here for a comparison of batch and bash syntax.
You can leave your java-command almost as it is, just change the way you call CLASSPATH. And keep in mind that bash is case-sensitive.

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/

how to convert a text/plain to text/x.shellscript

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

Linux version of a .cmd file

I am creating a terminal program and cannot find out what the ending is for Linux. I know in windows it is .cmd. Any help would be great.
Thank you.
Yes, you can remove the .sh at the end and it should work, generally using ./cmd will get it to run. this goes for C programs as well. You do not need to give an extension for the object file, You could then add a path to your bash file and then you can execute it as a normal command.
Look here.
https://stackoverflow.com/a/8779980/2720497
You don't need a file extension on Linux, though typically, people use .sh (sh being short for 'shell').
You can run it one of two ways:
bash myscript.sh
or you can make the script itself executable and run it directly:
chmod a+x myscript.sh # make it executable
./myscript.sh # run it
Linux scripts' first line is typically #!/bin/bash which is the path to the specific shell used to run the script with the second method.

Default file ending in Mac, Linux and Windows to run executable in the shell/terminal

I created executables of a python script (via pyinstaller) for Mac, Windows and Linux. For Linux and Mac, I am running them in the shell since it doesn't have an own interface: just open a shell and type the name of the program.
I am wondering if there is a way to use certain file ending so if the user clicks on the program, it will be automatically executed in the shell or terminal. Alternatively, I would appreciate any other ideas of how to do this.
The way to do this is not to append a certain file ending, but, as pointed out in the comment, make the file executable (chmod +x <file>) and add the magic bytes to the beginning of the file that tell the system how to execute it.
The magic bytes are #! and are followed by the path to executable. So for a python script you would put something like the following at the top of the file:
#!/usr/bin/env python
Okay, now I finally found out the solution to my question. All you have to do to execute the program upon clicking on it in the file browser is to add the ending .command and make it executable
E.g., exampleprogram.command. Clicking on it will execute the program in the shell

Resources