×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How can I escape $1 and && in a SED command

How can I escape $1 and && in a SED command

How can I escape $1 and && in a SED command

(OP)
I have a sed command that gets most of what I want done with the exception of the '$1' && I haven't figured out how to make it enter the line like I want.
Here is my command:
sed -i 's/# UserParameter=/UserParameter=systemd.unit.is-active[*],systemctl is-active --quiet $1 && echo 1 || echo 0/g' /etc/zabbix/zabbix_agent2.conf

Here is the entry that results:

UserParameter=systemd.unit.is-active[*],systemctl is-active --quiet \ # UserParameter=# UserParameter= echo 1 || echo 0

What I need is:
UserParameter=systemd.unit.is-active[*],systemctl is-active --quiet $1 && echo 1 || echo 0

I am running this from the cli. I know why it is doing this but I haven't figured out how to make it work the way I need it to. Can someone show me how to I believe the phrase is to escape the '$1' and the &&

Thanks alot

RE: How can I escape $1 and && in a SED command

Hi

Quote (EdLentz)

Can someone show me how to I believe the phrase is to escape the '$1' and the &&
  • $1 needs no escaping as it has no special meaning. ( The placeholder for the 1st captured group is \1. )
  • & can be escaped by prefixing it with a backslash : \&.
So should be :

CODE

sed -i 's/# UserParameter=/UserParameter=systemd.unit.is-active[*],systemctl is-active --quiet $1 \&\& echo 1 || echo 0/g' /etc/zabbix/zabbix_agent2.conf 

Note that this may depend on the sed implementation you use.

Feherke.
feherke.github.io

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close