Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a bash shell script to a .bat file? [closed]

I have a bash shell script on my Mac, that I need to convert to a .bat file (at least, that's what I've been told) for users that are on PCs. I was wondering the best way to go about this, or if anyone knows of any good references. I don't seem to be asking Google the right question to point me in the right direction.

Specifically, things like how would I do a...

cd ~/Documents/DropFolder

(...where ~/ equals the root of the user's home directory, regardless of the user's name)?

Or, when working on variables and "do" statements...

for i in *.xml
do
  java -Xss650K -Xms128m -Xmx2048m -jar /Applications...
done

And finally, identifying and using basenames...

  cp -p `basename $i .xml`.xml ~/Documents/ReadyForServer/`basename $i .xml`/

Thanks for any guidance, or suggestions for other solutions. LO

like image 523
LOlliffe Avatar asked Jul 08 '10 01:07

LOlliffe


People also ask

How do you call a bat script from a shell script?

Batch files can be run by typing "start FILENAME. bat". Alternately, type "wine cmd" to run the Windows-Console in the Linux terminal. When in the native Linux shell, the batch files can be executed by typing "wine cmd.exe /c FILENAME.

Is bash and batch the same?

Bash is actually a shell in UNIX/Linux. Batch files (or batch jobs) are usually referred to files containing list of commands executed periodically (daily, weekly, etc). You can write batch jobs in any language (example, Python, PHP, Perl, Shell script ). Bash shell also supports scripting.

Can you modify a Bash script while it is running?

with the current version of bash, modifying a script on-disk while it is running will cause bash to "try" to load the changes into memory and take these on in the running script. if your changes come after the currently executing line, the new lines will be loaded and executed.

Can a shell script be converted to a batch file?

Conversion of a Shell script to a batch file is required to execute a shell script on windows, for that third party tools like, has been using Linux regularly since the late 1990s. Author has 1K answers and 1.8M answer views 4 y How do I convert windows batch file to Linux shell script?

How do I run a bash script on Windows?

Install gnu-win32 bash on your windows and make sure you have all the win32 version of the programs called inside the shell script. Your script is now ready to be executed on Windows, no need to convert it to batch file.

Can I convert a DOS file to a Unix shell script?

Occasionally, the need still arises to convert an old DOS batch file to a UNIX shell script. This is generally not difficult, as DOS batch file operators are only a limited subset of the equivalent shell scripting ones. !==! ! ECHO.

What is the Linux equivalent of a batch file?

If so, Linux equivalent of a batch file is any text file, that: Its first line is in the form of #!PATH_TO_EXECUTABLE (for example #!/bin/sh) If the two conditions are met, such file is called a script. Upon trying to execute said script operating system (kernel executable loader to be precise


1 Answers

Actually, the things you mention are trivial to port to a Windows batch file. While you certainly can use Windows ports of all Unix tools (or even use an emulation layer for even more fun) this is not hard to do:

  1. ~ for the user's home folder

    The user's profile resides in the environment variable %USERPROFILE%, so the following should do it:

    cd %USERPROFILE%\Documents\DropFolder
    
  2. Iterating over a set of files

    The for command is helpful here:

    for %%i in (*.xml) do -Xss650K -Xms128m -Xmx2048m -jar ... %%i
    

    Obviously you need to adapt the path to the JAR file, though.

    And for has many more uses beyond this one, so take a look at help for as well.

  3. basename

    You need to do this either in a subroutine or a for loop, as the following syntax is specific to loop variables or parameters. It won't work with environment variables as is. You can get what basename is giving you by using %%~ni where %%i if the loop variable or %~n1 if %1 is the argument to a subroutine or batch file you have. So the following would probably do the same:

    copy "%%~ni.xml" "%USERPROFILE%\Documents\ReadyForServer\%%~ni\"
    

    The help on for has more information over those things near the end.

like image 189
Joey Avatar answered Oct 15 '22 12:10

Joey