problems with sed and bash. Escaped characters ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problems with sed and bash. Escaped characters ?
# 1  
Old 09-09-2008
problems with sed and bash. Escaped characters ?

Hi, I'm writing a long script for bash (on RHEL 5.0) to execute many commands. So, my idea is to create a function to deal with error checking and logging (see ceckoutput() below). This works with all commands except for sed. I think it may be a problems with escaped characters. So I did the following tests :

bash-3.00$ bin/sed -i -e 's/\(.* \)/\1 console=ttyS0,115200/' grub.conf
bash-3.00$ echo $?
0
bash-3.00$ export cmnd="/bin/sed -i -e 's/\(.* \)/\1 console=ttyS0,115200/' grub.conf"
bash-3.00$ echo $cmnd
/bin/sed -i -e 's/\(.*\)/\1 console=ttyS0,115200/' grub.conf
bash-3.00$ $cmnd
/bin/sed: -e expression #1, char 1: unknown command: `''

I even tried changing the quotes to avoid shell substitutions:

export cmnd='/bin/sed -i -e "s%\(.*\)%\1 console=ttyS0,115200%" grub.conf'
bash-3.00$ $cmnd
/bin/sed: -e expression #1, char 1: unknown command: `"'

Any ideas why this is happening?
Would it be a good idea idea to use awk to edit the file ? if so please help me with that command.


ceckoutput (){

$1 2>&1 |tee -a $LOGFILE

if [ ${PIPESTATUS[0]} -ne 0 ];then
echo "Error in installation!! See $LOGFILE"
exit 1
fi
}

Regards,
MacL
# 2  
Old 09-09-2008
Quote:
Originally Posted by macL
/bin/sed -i -e 's/\(.*\)/\1 console=ttyS0,115200/' grub.conf
bash-3.00$ $cmnd
/bin/sed: -e expression #1, char 1: unknown command: `''
This should work:
Code:
sed -i 's/$/\ console=ttyS0,115200/' grub.conf

# 3  
Old 09-09-2008
Hi danmero,

Unfortunately I get the same error:

[root@performance ~]# sed -i 's/$/\ console=ttyS0,115200/' grub.conf
[root@performance ~]# echo $?
0
[root@performance ~]# export cmnd="sed -i 's/$/\ console=ttyS0,115200/' grub.conf"
[root@performance ~]# $cmnd
sed: -e expression #1, char 1: unknown command: `''

There is a problem when the sed command is stored in the env variable and then called. This is strange because it works with every other command I tried.
# 4  
Old 09-09-2008
Quote:
Originally Posted by macL
bash-3.00$ export cmnd="/bin/sed -i -e 's/\(.* \)/\1

Why use a variable, which has all the disadvantages of an alias without any of its (few) advantages?

Why not just use the command itself?

If you are going to be executing it more than once and want to save typing (and don't know how to copy and paste), put it in a function:

Code:
cmnd()
{
   sed -i.bak -e 's/\(.* \)/\1 "$@"
}

# 5  
Old 09-10-2008
Hi chfajohnson,

Actually I’m not using variables as they I posted. I did that to show what’s happening when I pass the command as a parameter to a function that executes it.
In fact, I don’t need to execute the same command many times. Actually there are many different commands. Since this will be an installation script executed by other people I need to log each output and check for errors. Instead of repeating that one every command I decided to have a function that will receive each command, execute it and perform the logging and error checking (something like ceckoutput (), see above ). Actually, it works for every command but sed.

Do you advice me a different way of doing this?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Outputting characters after a given string and reporting the characters in the row below --sed

I have this fastq file: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGGGGGGGGGGGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCA +test-1 GGGGGGGGGGGGGGGGGCCGGGGGFF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8... (10 Replies)
Discussion started by: Xterra
10 Replies

2. Shell Programming and Scripting

sed problems - Bash Script

Hi I keep getting the following error sed: -e expression #1, char 32: unterminated `s' command sed: -e expression #1, char 35: unterminated `s' command sed: -e expression #1, char 35: unterminated `s' command whenever I use the following bash script #! /bin/bash... (2 Replies)
Discussion started by: spbr
2 Replies

3. Shell Programming and Scripting

[BASH] Getting a semi-tailing backslash when passing (escaped) variables to script

Heyas Figured me had a 'typo' in tui-conf-set, i went to fix it. Now, i also figured, it might be nice to have tui-conf-set report (to console, not only exit code) wether it could save the variable to the file or not. This said, I appended this code: (the tui-title and tui-echo lines are... (3 Replies)
Discussion started by: sea
3 Replies

4. Shell Programming and Scripting

Sed, bash problems migrating from Cray to GNU/Linux

So, I have a series of ASCII files, all named something like mrkxxxxz.tmp (say, mrk1001z.tmp, mrk1002z.tmp, mrk1003z.tmp,...) -- these are .tmp files created by a large simulation program, and each different .tmp file represents a different parameter space used in the simulation). The simulations... (2 Replies)
Discussion started by: johnny_canucl
2 Replies

5. UNIX for Dummies Questions & Answers

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet... (3 Replies)
Discussion started by: Chella15
3 Replies

6. Shell Programming and Scripting

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet able... (0 Replies)
Discussion started by: Chella15
0 Replies

7. Solaris

Reason why some commands need escaped-characters and other not

Hi, this is my first post and hope to make some contribution soon. I'm still learning the basics of UNIX and Linux and BASH. Thus my need to understand the subject at hand. I don't have a problem with technical detail, so hit me :) I have a script where two commands use the contents of a... (2 Replies)
Discussion started by: doublefrangelic
2 Replies

8. Shell Programming and Scripting

bash ignores escaped $ and says "bad substitution"

Can someone tell me how to get the version of bash that I am running? I'm running cygwin bash on Windows XP at home and cygwin bash on Vista at work. Is this the version number for bash? $ uname -a CYGWIN_NT-6.0 US-SEA-L3BER9K 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin This is the... (2 Replies)
Discussion started by: siegfried
2 Replies

9. Shell Programming and Scripting

Editing long records with characters that need to be escaped.

Hi all, I'm new in unix scripting and I've a problem with a script... :confused: I need to read a file, add some fields in the records, and write them in another file, but even when I simply read and write the records, the shell interprets some caracters and the result is that the records... (5 Replies)
Discussion started by: Macs_Linux
5 Replies

10. Shell Programming and Scripting

Special characters in a bash variable in sed

Hello, I am trying the following: echo __CHANGEME__ >> testfile VAR1="&&&" sed -i "s|__CHANGEME__|${VAR1}|" testfile cat testfile This results in testfile containing __CHANGEME____CHANGEME____CHANGEME__ Whereas I want it to result in &&& I understand that if VAR1="\&\&\&" then... (3 Replies)
Discussion started by: linuxnewbeee
3 Replies
Login or Register to Ask a Question