?
Solved

using a variable in a sed statement

Posted on 2011-05-05
4
Medium Priority
?
490 Views
Last Modified: 2012-05-11
I can't get this sed statement to work. Trying to use a variable. Any ideas?

cp /etc/shadow /etc/shadow.bak

OPASSWD=`grep ttc_root /etc/shadow.bak | awk -F: '{print $2}'`

(OPASSWD is the encrypted password, and may contain metacharacters)

sed -e 's/$OPASSWD/spoo/' /etc/shadow.bak

thanks
0
Comment
Question by:sonriks
  • 2
  • 2
4 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 35697855
Use double quotes instead of single quotes.  :)

  sed -e "s/$OPASSWD/spoo/" /etc/shadow.bak


Kent
0
 
LVL 1

Author Comment

by:sonriks
ID: 35698318
Thanks, Kent .. that worked like a charm for a "plain" string.

I need this to work for variables that contains metacharacters, as many entries in the shadow file do, characters like "/", "\", "$". etc.

OPASSWD=U/gx5Pid8Ykmk

Is there some way that the string with metachars being taken as a literal?

thanks
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 35698402
There is an issue when the substituted value contains the same character that you're using as a delimiter.  The command:

  sed -e "s/$OPASSWD/spoo/" /etc/shadow.bak

literally becomes:

  sed -e "s/U/gx5Pid8Ykmk/spoo/" /etc/shadow.bak

after the substitution, giving an "extra parameter" to the *s* function.

You can use a lot of characters for the delimiter.  Can you pick one that won't occur in the data?
0
 
LVL 1

Author Closing Comment

by:sonriks
ID: 35699000
Thanks. I used a ":" as the delimiter and it worked fine!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question