View previous topic :: View next topic |
Author |
Message |
tdm Tux's lil' helper
Joined: 06 May 2002 Posts: 117 Location: The Netherlands
|
Posted: Mon Jan 13, 2003 12:22 pm Post subject: Replacing a space " " with a new line? |
|
|
I'm creating a little bash script and for this script I need the output of the 'groups' command, as every group on a new line.
This works perfectly if I execute it from the commandline:
Code: | bash-2.05b$ groups | sed 's/ /\
> /g'
users
wheel
patrick |
But when I put this line in a executable #!/bin/bash file it doesn't work:
File:
Code: | #!/bin/bash
`groups | sed 's/ /\
/g'` |
And when I execute the file I get this:
Code: | bash-2.05b$ ./usergroups
./usergroups: line 2: unexpected EOF while looking for matching ``'
./usergroups: line 4: syntax error: unexpected end of file |
What am I doing wrong? As you can see it works on the commandline.. |
|
Back to top |
|
|
zleepy n00b
Joined: 19 Dec 2002 Posts: 19 Location: /sweden/ludvika
|
Posted: Mon Jan 13, 2003 12:53 pm Post subject: |
|
|
This works.
Code: | #!/bin/bash
groups | sed 's/ /\n/g'
|
\n = new line |
|
Back to top |
|
|
praxim Tux's lil' helper
Joined: 14 May 2002 Posts: 75 Location: State College, PA
|
Posted: Mon Jan 13, 2003 2:45 pm Post subject: |
|
|
Instead of using a regexp, it might be a bit faster to use tr:
groups | tr ' ' '\n' |
|
Back to top |
|
|
tdm Tux's lil' helper
Joined: 06 May 2002 Posts: 117 Location: The Netherlands
|
Posted: Mon Jan 13, 2003 3:58 pm Post subject: |
|
|
Thanks for the help guys, praxim your solution seems to be the easiest so I take your tip
One thing, I want to put the output in a variable, I do that with:
Code: | #!/bin/bash
mygroups=`groups | tr ' ' '\n'`
echo $mygroups; |
But when I execute it, I still see the groups like you see them when you just execute the groups command. But when I don't but the output in a variable it works find:
Code: | #!/bin/bash
groups | tr ' ' '\n' |
Am I doing something wrong?
Thanks |
|
Back to top |
|
|
idl Retired Dev
Joined: 24 Dec 2002 Posts: 1728 Location: Nottingham, UK
|
Posted: Mon Jan 13, 2003 4:42 pm Post subject: |
|
|
Hi, sorry i dont realy know any bash but here it is in perl:
Code: | perl -e '$groups = `groups`; $groups =~ s/ /\n/g; print $groups;' |
|
|
Back to top |
|
|
Naan Yaar Bodhisattva
Joined: 27 Jun 2002 Posts: 1549
|
Posted: Mon Jan 13, 2003 4:47 pm Post subject: |
|
|
bash will do word splitting when it expands the mygroups variable. To prevent this, just change the third line in your script to:
The double quotes will prevent bash from splitting words and will preserve whitespace characters, including "\n".
tdm wrote: | Thanks for the help guys, praxim your solution seems to be the easiest so I take your tip
One thing, I want to put the output in a variable, I do that with:
Code: | #!/bin/bash
mygroups=`groups | tr ' ' '\n'`
echo $mygroups; |
But when I execute it, I still see the groups like you see them when you just execute the groups command. But when I don't but the output in a variable it works find:
Code: | #!/bin/bash
groups | tr ' ' '\n' |
Am I doing something wrong?
Thanks |
|
|
Back to top |
|
|
tdm Tux's lil' helper
Joined: 06 May 2002 Posts: 117 Location: The Netherlands
|
Posted: Mon Jan 13, 2003 6:07 pm Post subject: |
|
|
Thanks Naan Yaar, your thing works perfect |
|
Back to top |
|
|
|