Not working sed/tr while working with "$@"

Hi all, I'm doing a program with among others, the following script, wich is called "call":

Code:
#!/bin/sh
DIR=/root/test
COMMAND=`echo $@ | tr -s "\/"," " "\\","_"`
$@ > $DIR/`echo "$COMMAND"`.`date +%Y%m%d%H%M%S`

Which should do the following:

Accept some arguments, execute the arguments as a command, and if there is a " ", or a "/" in the arguments, change them to "_" and "\". And save the output of stdout in a file at /root/test in the format 'command_with_no_spaces_and_"\".20120515134527' (please note the slash has been changed for a backslash xD).

So I insert the following command in shell:

# call nmap -sn 192.168.1.1/24
Code:
/root/bin/call: line 3: nmap_-sn_192.168.1.1/25.20120420102654: No such file or directory

I guess that's because a file can't be written with a slash, which, even though I've tried changing it (the slash) in many ways, it seems impossible. I DO have the permissions to write in that directory, and I'm using /bin/sh.

Any thoughts?
Any help would be greatly appreciated.
Many thanks for your time
 
You can use sed -e 's;/;\\;g' -e 's; ;_:g'

But I wonder if you really want to put '\' in your filename? I suggest you just replace anything nasty with '_'.

Code:
#!/bin/sh
DIR=.
cmd=`echo $@|sed -e 's;[/ ];_;g'`
$@ > $DIR/$cmd.`date +%Y%m%d%H%M%S`
 
Ok, thank you, I also tried with sed, to no avail.

But I found a solution using my original /usr/bin/tr, that's the following:

Code:
COMMAND=`echo "$@" | tr '\/',' ' '\\\\','_' `

Could please somebody tell me why this works? (Contrary to '\\')

@jalla: I'm changing slashes to backslashes because I want to retrieve the actual command that generated that input, that's why I need to use different characters to '/' and ' ', many thanks for the help :D
 
Ok, just remeber you have to escape that backslash whenever you want to access the file

Code:
snapper:~% touch file\\name.txt
snapper:~% ls -l file\name.txt
ls: filename.txt: No such file or directory
snapper:~% ls -l file\\name.txt 
-rw-r--r--  1 tl  tl  0 May 16 14:44 file\name.txt
 
jalla said:
Ok, just remeber you have to escape that backslash whenever you want to access the file

Oh, thank you for the information, then I guess I'll change it for some other character, like..."!", or something like this.
 
Back
Top