tags:

views:

53

answers:

4

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

+2  A: 

Hello.

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.

Benoit
+2  A: 

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).

paxdiablo
need to be double quotes.
ghostdog74
Actually, that doesn't make a difference to the CygWin `sed` but I'm not sure about GnuWin32 so I'll change it.
paxdiablo
+1  A: 

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';
Ruel
A: 

Windows cmd.exe hates single quotes

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