Convert unix script to windows

Convert bash script into Windows script

I have the following Unix shell script. I would like to convert this into a Windows .bat file (I know I can use Cygwin instead of adapting it to a Windows environment. But Cygwin is not an option for me).

I know I can use Windows PowerShell reading material online. But I don’t want to spend hours online learning from basics for this one time requirement. Please don’t bash me for being lazy. I am sure this helps others as well by being a quick guide for someone searching online in the future.

Here is the script:

To answer questions on what I tried, I have trouble running the » find » command which is the equivalent of grep per this website http://tldp.org/LDP/abs/html/dosbatch.html

Here is my Joy.txt file (next two lines):

Then at the PowerShell prompt I ran the following command:

I was expecting to see the first line in the above file. But instead I get the parameter format not correct error. Can someone help please?

2 Answers 2

A more or less direct equivalent of grep -A does not exist in findstr (or find ), Windows’ native grep equivalent. However, Select-String in PowerShell has this with the -Context parameter.

If I understand your script correctly, it means:

  • Write an empty line
  • Write all lines containing «Device_name», followed by four lines of context, followed by an empty line
  • Write all lines containing «Device_Oops», followed by 45 lines of context, followed by an empty line
  • Write all lines containing «Radar» if they either contain «Processes:» too or are within the first 150 lines following a line containing «Processes:»
  • Write all lines containing three pairs of digits, separated by a colon that also contain either «error» or «restart», case-insensitively.

So it more or less comes down to something like:

Note that this is slightly ugly due to the attempt to emulate grep ‘s output behaviour.

If you just need matching lines and the following, then I’d simply write a small function:

And then use it in a script that isn’t so complex anymore (still trying to recreate some of your output choices, e.g. empty lines):

You’ll also notice that I got rid of Select-String , a cmdlet I never really understood the purpose of (except providing a close match of grep / findstr , but usually I find other means more flexible).

Conversion from unix bash script to windows batch script

I’m trying to convert a Linux bash script to its windows counter part.
This is the actual bash script:

And this is my converted version:

The problem is first of all I don’t know if this is the correct conversion and does exactly what the bash script does. And second of all when I run the script, the cursor starts blinking (waiting) when it reaches the ECHO «LOG = %LOG%» section and after a long time, it prints the following:

And it waits again!

What is wrong here?

UPDATE

After changing aux.txt to auxx.txt the first error is resolved. But now I get these errors:

2 Answers 2

Do not use the following reserved names for the name of a file:

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed immediately by an extension; for example, NUL.txt is not recommended. For more information, see Namespaces.

Use another different name instead, e.g auxx.txt .

Please read answers on How to set environment variables with spaces? and Why is no string output with ‘echo %var%’ after using ‘set var = text’ on command line? as you most likely don’t know how to define variable values with spaces correct.

Читайте также:  Что такое mac os extended журнальный

Don’t use IF [%1]==[] as square brackets have no special meaning in Windows command scripts on IF and therefore the batch processing could exit because of a syntax error with left value containing 1 or more spaces. Use instead IF «%

1″==»» and execute in a command prompt window call /? for an explanation of %

1 (first argument with surrounding double quotes removed).

It is advisable to use always %

2 , . with explicitly embedding those argument references in double quotes.

On Windows the directory separator is the backslash \ character and not the forward slash / character. Windows command processor corrects this often made mistake automatically where it detects it, but it can’t detect and correct / in all cases automatically.

awk is from *nix and gives a backslash a completely different meaning as Windows command processor. I have no experience with awk, but I think it is better to specify file names with paths passed as arguments to awk also on Windows with a forward slash as directory separator instead of a backslash. Therefore I suggest to replace the lines:

Using *nix tools on Windows requires thinking on what is interpreted by Windows native command line tools like cmd.exe and what is interpreted by the *nix tools like awk.exe and grep.exe to use correct syntax on arguments.

set «LogFileName=%LogFileName:\=/%» replaces all backslashes by forward slashes in log file name with relative path. Run in a command prompt window set /? and read all output help pages for details about string replacements / substitutions in values of environment variables.

Converting Unix ImageMagick script to Windows batch file

I have the following bash script (.sh) that’s working just fine in Unix. I’d like to convert into a batch (.bat) file for use with ImageMagick for Windows but I’m having a bit of trouble with the conversion.

Original GIT repo for the Unix script(s):

ImageMagick version for Windows:

This is what I have so far, but it’s not working as intended as I’m not sure what the batch file equivalent of multiple command line arguments are: e.g., «\» or «|» or whether it’s even possible. I read somewhere that the equivalent is a caret «^» but again, I’m not 100% sure how to implement it properly.

WORKING CODE:

This is what I’ve done after applying @Gabe‘s answer. No errors and it’s working flawlessly!

2 Answers 2

You’re pretty close. The big problems I see are that you have an extra \ in rgba(. ) , you need to escape your % signs (like %% ), and you need to put | before the line continuation characters (like |^ ). Try this:

Your original unix shell script is taking the output from the first convert cmd and passing from one invocation to the next thru stdout/stdin, as denoted by the ‘-‘ chars.

It is a convention of unix programs, that an argument of ‘-‘ means «don’t look for a filename to process, just read from the std input.»

Also, speaking of conventions, DOS programs use ‘/o’ (option) format for controlling program. Try convert.exe /? to see if you get a usage msg or help ELSE try convert.exe —help (and other unix variants) just so you understand which is the correct option syntax to use.

More importantly, note there is a std program in DOS called convert.exe, so be sure your PATH is set to goto the ImageMagick for Windows DIR first. You’ll be able to tell when you do the convert.exe /? and you get options that you don’t recognize.

The original script uses the pipe character to pass stdout of one program to the stdin of the next program. That is one impressive chain!

My experience with cmd shell is really old at this point, but at one time, a DOS pipe could only hold 64K. You may be running into this problem. If these are long running commands, you try running a separate window and do a dir %TEMP to see the pipe files being created. With so many pipes, DOS may have a naming conflict or some other issue.

Читайте также:  Как активировать winrar для windows 10

So, do the simplest case, convert .. | convert . (doing just part of the work to see if the pipes work at all), and build up from there, adding 1 more | convert .. as you go. If it blows up after a while then you know exactly where it is happening.

Does the first command with (. ) in it work?

The escape char \ should work OK, and I see you have transposed your path chars from / to \ , so that is good.

And % has a special meaning for .bat files, to print variable names like %f% , so as long usage for % is only with numbers you may be OK. For a var like %f%, the only way I could find to break the translation was to quote like %f\% , which winds up giving you the un-useful output of %f\% .

Not perfect, but neither is .bat programming.

Converting unix script to windows script — emulating a Sed command in PowerShell

I have a unix script (korn to be exact) that is working well and I need to convert it windows batch script. So far I have tried inserting a powershell command line on my code, but it doesn’t work. Please help, I am just new to both unix scripting and windows scripting so any help will do.

This is the line of code that I need to convert:

So far I have tried a powershell command line to be called on my script but it didn’t work:

Any suggestions please?

1 Answer 1

PowerShell has no sed -like constructs for processing ranges of lines (e.g., sed interprets 1,/foo/ as referring to the range of consecutive lines from line 1 through a subsequent line that matches regex foo )

Emulating this feature with line-by-line processing would be much more verbose, but a comparatively more concise version is possible if the input file is processed as a whole — which is only an option with files small enough to fit into memory as a whole, however (PSv5+ syntax).

Here’s the pure PowerShell code:

Note that [regex]::Escape() is used to make sure that the value of $TIMESTAMP is treated as a literal, even if it happens to contain regex metacharacters (chars. with special meaning to the regex engine).
Your ksh code doesn’t do that (and it’s nontrivial to do in ksh ), so if — conversely — $TIMESTAMP should be interpreted as a regex, simply omit that step and use $TIMESTAMP directly.

The -replace operator is regex-based and uses the .NET regular-expression engine.

It is the use of Get-Content ‘s -Raw switch that requires PSv3+ and the use of Set-Content ‘s -NoNewline switch that requires PSv5+. You can make this command work in earlier versions, but it requires more effort.

Calling the above from cmd.exe (a batch file) gets quite unwieldy — and you always have to be wary of quoting issues — but it should work:

Note how the -command argument is passed as a single «. » string, which is ultimately the safest and conceptually cleanest way to pass code to PowerShell.
Also note the need to embed batch variables as %varname% in the command, and since they are enclosed in embedded ‘. ‘ above, the assumption is that their values contain no ‘ chars.

Therefore, consider implementing your entire script in Powershell — you’ll have a much more powerful scripting language at your disposal, and you’ll avoid the quoting headaches that come from bridging two disparate worlds.

How To Convert Files from Linux/Unix Format to Windows and Vice Versa

If you’ve ever transferred a text file from a UNIX based system to a Windows system directly, you know that when you open the text file on the Windows system, it is usually not displayed correctly. Windows based text reader programs (like Notepad) may not be able to display the text. In most cases, when you open the text file, all the words get displayed on a single giant line, without any breaks. This is because there is a slight difference in the way a text document is written (and read) on Windows and UNIX.

Читайте также:  Как посмотреть параметры ядра linux

If a file was written on a Windows based system and is opened by a text editor on a UNIX system, it is very common for the “Ctrl-M” characters (^M) to be displayed at the end of each line of text. If a file was written on a UNIX system and opened by a text editor on a Windows system, the line break character (EOL) may not be displayed correctly. The carriage return character is also different for both UNIX and Windows.

While dealing with files, you don’t want to be limited by whether the file was created on Linux or Windows. So how do you convert a file from UNIX to Windows (or vice versa) without having the formatting go all crazy? We’ll walk you through the steps.

Converting Files from Linux/UNIX format to Windows Format

If you’re using a UNIX based system to transfer the files to a Windows system, there are some commands that let you convert the text file(s) you are transferring to a format Windows can understand.

The dos2unix and unix2dos command

You can use command line to safely convert files from UNIX to Windows and vice versa. To convert a Windows text file to a UNIX text file, enter this:

The above command converts and replaces “windows.txt” file to “unix.txt”. To convert a UNIX text file to a Windows text file, enter this command:

The above command will convert a UNIX created text file called “unix.txt” to a Windows compatible text file called “windows.txt”.

The awk command

The awk command also lets you convert a file from UNIX to Windows and vice versa. To convert a Windows file to a UNIX file, enter the following command:

To convert a UNIX text file called “unix.txt” to a Windows text file called “windows.txt”, enter the following command:

The tr command

The tr command (transliterate) can be used to remove the carriage return characters and the “Ctrl-Z” characters from a Windows file. This can only be done if you are converting a file from Windows to UNIX. The command will be written as follows:

The tr command transliterates one character with another. In this case, it is helping you omit unnecessary characters.

Using the Visual Editor (Vi)

If you are using the Visual Editor to view a file created on a Windows system, you can remove the carriage return characters by typing the following command line:

To get the computer to input the ^M character, you need to hit “Ctrl + v” and then press Return.

Using File Transfer Protocol Programs

File Transfer Protocol (FTP) programs are available both for UNIX and Windows system. If you need to convert a lot of files from Windows to UNIX (or the other way around), then it’s a good idea to download a FTP program. There are many available for free on the Internet. The Hummingbird FTP is one of the more popular FTP programs out there. It is secure and easy to use.

Most FTP programs will transfer files from UNIX to Windows in the ASCII format. Sometimes you have to specify the format for yourself (if you are using command line based FTP programs). To do that, just enter this in the command line:

Conclusion

The easiest way to convert a file from a UNIX format to Windows (and the other way around) is to use an FTP program. The conversion commands are your next best bet. If you are looking for additional commands that perform the same task, you can search for perl and sed commands. However, do keep in mind that these commands may not work across all systems.

I’m a techie with over a decade of programming experience, spread across a wide range of interesting, path breaking technologies. Now I’m sharing my passion for technology, and making tech easier, with everyone! Hope you enjoy reading about, and playing with technology, as much as I do!

Оцените статью