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



## athos (May 15, 2012)

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


```
#!/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`

```
/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


----------



## jalla (May 16, 2012)

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 '_'.


```
#!/bin/sh
DIR=.
cmd=`echo $@|sed -e 's;[/ ];_;g'`
$@ > $DIR/$cmd.`date +%Y%m%d%H%M%S`
```


----------



## athos (May 16, 2012)

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:


```
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


----------



## jalla (May 16, 2012)

Ok, just remeber you have to escape that backslash whenever you want to access the file


```
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
```


----------



## athos (May 16, 2012)

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.


----------



## jalla (May 16, 2012)

You're in for a surprise if you use '!'. (hint: command history).


----------



## athos (May 16, 2012)

True!!  meh, I'll remember to escape the backslash xD


----------

