LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-07-2011, 04:28 PM   #1
chuckmangan
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
SED scripting issue - help


I have a sed command that when typed at the command line works great:
-------------------
sed -i 's/^.*VERIF_REPO_NAME .*= .*/VERIF_REPO_NAME = verif_\$(LOGNAME)/g' makefile
-------------------
when I put this in a script it does not work anymore. Here is a shortened script that still has the same issue:
-------------------
#!/bin/bash

cmd="sed -i 's/^.*VERIF_REPO_NAME .*= .*/VERIF_REPO_NAME = verif_\$(LOGNAME)/g' makefile"
echo $cmd
$cmd
--------------------
The output I get from this is:

sed -i 's/^.*VERIF_REPO_NAME .*= .*/VERIF_REPO_NAME = verif_$(LOGNAME)/g' makefile
sed: -e expression #1, char 1: unknown command: `''

I am sure it is some type of quoting issue, but I can't seem to find the right combination. Any help wold be appreciated....
 
Old 11-07-2011, 04:40 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Try
Code:
eval $cmd
You can see the differences between the two, if you run the script as
Code:
bash -x scriptname.sh
this will show you the actual commands going to be executed by the script. Look at them with and without eval and you will see where the problem is.
 
Old 11-07-2011, 04:53 PM   #3
chuckmangan
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks colucix - eval works. I tried the bash -x and see that it has added a bunch of quotes but that made it even harder to understand why I had teh problem. Here is what I get from -x.

Code:
+ cmd='sed -i '\''s/^.*VERIF_REPO_NAME .*= .*/VERIF_REPO_NAME = verif_$(LOGNAME)/g'\'' makefile'
+ echo sed -i ''\''s/^.*VERIF_REPO_NAME' '.*=' '.*/VERIF_REPO_NAME' = 'verif_$(LOGNAME)/g'\''' makefile
sed -i 's/^.*VERIF_REPO_NAME .*= .*/VERIF_REPO_NAME = verif_$(LOGNAME)/g' makefile
+ sed -i ''\''s/^.*VERIF_REPO_NAME' '.*=' '.*/VERIF_REPO_NAME' = 'verif_$(LOGNAME)/g'\''' makefile
sed: -e expression #1, char 1: unknown command: `''
Quote:
Originally Posted by colucix View Post
Try
Code:
eval $cmd
You can see the differences between the two, if you run the script as
Code:
bash -x scriptname.sh
this will show you the actual commands going to be executed by the script. Look at them with and without eval and you will see where the problem is.
 
Old 11-07-2011, 05:13 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Please use [code][/code] tags around your code, to preserve formatting and to improve readability.


Don't try to run commands from variables. They're designed for holding data, not code. Use a function, or even just the raw command, instead.

http://mywiki.wooledge.org/BashFAQ/050

Also avoid using eval, unless you know exactly what it will happen when it processes the line. You nearly never need to use it anyway.

http://mywiki.wooledge.org/BashFAQ/048
 
Old 11-07-2011, 08:21 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
+1 to David's post. I must say that I used to do this a lot to until caught out and lost half of a test system due to an incorrect escape being used
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting - sed, lame, etc. Jinouchi Linux - Newbie 10 03-22-2010 06:03 AM
bash scripting with sed not working fakie_flip Linux - Software 6 12-08-2009 02:56 AM
scripting help with sed n0gnus Linux - General 4 09-30-2008 12:01 PM
Problems with shell scripting and sed Uzelth Programming 5 04-13-2008 07:21 PM
Forum on scripting, sed, awk, etc.?? pixellany Programming 3 12-01-2005 02:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration