Exit status on sed when it fails | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Exit status on sed when it fails

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-17-2011
DC Slick DC Slick is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 11 May 2011, 5:09 PM EDT
Posts: 82
Thanks: 9
Thanked 6 Times in 6 Posts
Exit status on sed when it fails

I need a way to get sed to return an exit status that's not 0 when I search a file for a string to append a line afterwards that doesn't exist. For example:


Code:
 sed -i "/^Beginning/ a\Start" file.txt

If there's not a line with "Beginning" I need to be able to echo to the user that it doesn't exist basically. In the script the user is given the option to add and delete specific commands from the script. I can add just fine, I can delete just fine. But if that line does not exist, I need to say so.

Also, I'm considering using a separate file for these user defined commands since updating my script breaks that for the user since the commands will be back every update. Any easy way to do this? I'm pretty good with sed and grep but I'm mentally exhausted and can't wrap my mind around it atm. Thanks.

Let me clarify too, so the script let's a user add ' myFunction myArgument ' to the containing script where "myFunction" is defined within. Right now I have
Code:
 sed -i '/^Custom Commands/ a\ myFunction $usrArg

The user can access this through a commandline option . I'd like instead, to append that function and arg the user added to a separate file and have the script, for lack of better words, take the input from that script. myscript < . Is that it? too tired...

Last edited by DC Slick; 02-17-2011 at 01:21 AM.. Reason: clarification
Sponsored Links
    #2  
Old 02-17-2011
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 18 December 2014, 12:25 PM EST
Location: NM
Posts: 10,310
Thanks: 295
Thanked 821 Times in 763 Posts
sed returns > 0 when an error occurs. Not making a change because the pattern was not found is not an error.

Try
Code:
 
grep -q 'pattern'  file && sed 's/pattern/' file || echo "cannot make requested change"

The Following User Says Thank You to jim mcnamara For This Useful Post:
DC Slick (02-17-2011)
Sponsored Links
    #3  
Old 02-17-2011
DC Slick DC Slick is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 11 May 2011, 5:09 PM EDT
Posts: 82
Thanks: 9
Thanked 6 Times in 6 Posts
Thank you for your response. I went with the separate file as it looked to be a better option for the custom commands. (persists during script update, easy to parse, etc)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Exit Status 126 - how to get rid of it glamo_2312 UNIX for Advanced & Expert Users 5 09-23-2009 01:37 AM
How to print error and exit if command fails? lavascript Shell Programming and Scripting 5 04-24-2009 12:52 PM
How to get the exit status yhacks Shell Programming and Scripting 1 05-19-2008 09:06 AM
tar exit status thorndike UNIX for Dummies Questions & Answers 3 01-22-2002 04:39 PM
ftp exit status. oracle8 UNIX for Advanced & Expert Users 1 10-22-2001 12:34 AM



All times are GMT -4. The time now is 02:17 PM.