0

Im trying to replace the letter I in the 5th line by output of who command but getting an error

sed: -e expression #1, char 49: unterminated `s' command

I have tried using eval with no success

#!/bin/bash
for i in $2 $3 $4 $5 $6 $7
do
    mkdir $1/$i
    cp hello.txt $1/$i
    user=$(who)
    sed -i '5 s/.*/I changed this line/' $1/$i/hello.txt
    sed -i "5 s/I/$(who)/" $1/$i/hello.txt
done
Jerry
  • 410
  • 6
  • 17
  • If the output of `who` contains the separator used for the `sed` command (`/` here) it would explain the problem. If that's the case, change the separator for one you can't find in the output of `who` – Aaron Sep 07 '16 at 09:14
  • Please [edit] your question to provide us with a [mcve] - at the moment, there are too many things that are unknown to us, which makes it difficult for us to reproduce the problem. – Tom Fenech Sep 07 '16 at 09:16
  • @Aaron i wanted who but yes the seperator(/ here) is the problem – Jerry Sep 07 '16 at 09:19
  • That's because the output of `who` contains characters that have a special meaning in the `s` command. Check this: https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed – hek2mgl Sep 07 '16 at 09:19

1 Answers1

0

I don't know who well, but a quick test reveals that it can contain / characters, which is the delimiter you used for your sed command. On my system, it did output something along the line of :

myName   pts/0        2016-09-07 11:14 (10.123.45.678)

Notice the / in pts/0 , that's what's breaking the sed command, since it's its delimiter. Indeed, the following expanded sed command contains a / too many :

sed -i "5 s/I/myName   pts/0        2016-09-07 11:14 (10.123.45.678)/" $1/$i/hello.txt

I think you could solve your problem by using the whoami command instead, whose output probably correspond much better to what you expect:

$ whoami
myName

If you do want to use the output of who, you will have either to escape the delimiter in the output of the who command, or to use a delimiter that cannot be output by who.

Assuming that who will never output any + character (I don't know if it's true, you should make sure !), you could use the following sed command instead, where + is used as a delimiter :

sed -i "5 s+I+$(who)+" $1/$i/hello.txt
Aaron
  • 24,009
  • 2
  • 33
  • 57
  • Note that I'm not sure there's any perfect solution : if I'm not mistaken, unix usernames can now contain any character but the `NUL` character, and I'm pretty sure we can't use `NUL` as a sed delimiter. That means an username could contain the `sed` delimiter you'd pick and break that code. That affects both the `who` and `whoami` solutions. If someone can expand on that I'd be very interested to hear about it :) – Aaron Sep 07 '16 at 09:29
  • I replaced / by + in the second sed command yet I don't get the desired output – Jerry Sep 07 '16 at 09:42
  • what's the desired output? what do you get? – Aaron Sep 07 '16 at 09:43
  • Desired output as in the output of who in place of I – Jerry Sep 07 '16 at 09:45
  • So, for example "myName pts/0 2016-09-07 11:14 (10.123.45.678) changed this line" ? And what did you get instead? – Aaron Sep 07 '16 at 09:46
  • Yes like you said / is the problem, I verified it using a variable which does not contain / – Jerry Sep 07 '16 at 09:47
  • $(who) changed this line – Jerry Sep 07 '16 at 09:50
  • Thanks. Have you switched from double quotes to single quotes? that would explain the problem. `echo "I am the best" | sed "s+I am+$(who) is+"` works just fine – Aaron Sep 07 '16 at 09:52