Blanca Hdez
Blanca Hdez

Reputation: 3563

'Translate' .sh to .bat

HI!
I would like to use this simple script.sh:

#!/bin/sh
filename=$1
echo $filename | sed 's/\([A-Z]\)/ \1/g';

In a script.bat, but my computer is not Windows, so I can't test it. This one would be correct??

prueba.bat 

filename=%1  
echo %filename% | sed 's/\([A-Z]\)/ \1/g';

Thanks in advance

Upvotes: 2

Views: 3782

Answers (4)

paxdiablo
paxdiablo

Reputation: 881463

Not quite:

@echo %1| sed "s/\([A-Z]\)/ \1/g"

But you do have to make sure you have a sed available (CygWin or GnuWin32 are excellent for these tools - I prefer GnuWin32 if you only need specific things, CygWin tends to give you a lot).

You also have to be careful with environment variables like filename. While UNIX will create them in the context of the current shell (so its effect will be limited), cmd.exe scripts will "leak" them (hence the direct use of %1).

Upvotes: 4

ghostdog74
ghostdog74

Reputation: 342363

Windows cmd.exe hates single quotes

echo %filename% | sed "s/\([A-Z]\)/ \1/g";

Upvotes: 0

Ruel
Ruel

Reputation: 15780

You need to use set for declaring a variable, and preferably use @echo off

@echo off
set filename=%1
echo %filename% | sed 's/\([A-Z]\)/ \1/g';

Upvotes: 1

Benoit
Benoit

Reputation: 79185

In cmd.exe, %1% does not mean anything, use %1 or, better, %~1 which removes possible quotes around instead.

Also, use double quotes for literal expressions for sed. Single quotes would be passed to sed.

 set filename=%~1
 echo.%filename%| sed "s/\([A-Z\)/ \1/g"

In bash, use " around your $ expressions.

Upvotes: 3

Related Questions