Check Success Status Of sed command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Check Success Status Of sed command
# 1  
Old 08-29-2013
Check Success Status Of sed command

How do i check success status of a sed command execution i have the below script not sure if it is right approach to check status of execution using a function.

Also it looks like in the below sed command even if the search string doesn't exist in the file it is returning status as success as i believe it is just checking for execution of sed not the action performed. If that is the case what could be an automated ideal way of checking the action performed by sed as well.

Code:
#!/bin/bash -x
. /home/infrmtca/bin/dwh_Loadfuncs.sh

SetVar

check()
{
if [ $? -eq 0 ]
then
        echo "success"
else
        echo "failure"
        exit
fi
}


sed -i 's%SOURCEFOLDERNAME=[^ ]*%SOURCEFOLDERNAME='\"${FOLDER}\"'%g' DepFolderControlFile.xml
check

# 2  
Old 08-29-2013
Make the small change in red at the bottom:
Code:
#!/bin/bash -x
. /home/infrmtca/bin/dwh_Loadfuncs.sh

SetVar

check()
{
if [ $? -eq 0 ]
then
        echo "success"
else
        echo "failure"
        exit
fi
}


sed -i 's%SOURCEFOLDERNAME=[^ ]*%SOURCEFOLDERNAME='\"${FOLDER}\"'%g' DepFolderControlFile.xml
check $?

# 3  
Old 08-30-2013
Quote:
Originally Posted by jim mcnamara
Make the small change in red at the bottom:
Code:
#!/bin/bash -x
. /home/infrmtca/bin/dwh_Loadfuncs.sh

SetVar

check()
{
if [ $? -eq 0 ]
then
        echo "success"
else
        echo "failure"
        exit
fi
}


sed -i 's%SOURCEFOLDERNAME=[^ ]*%SOURCEFOLDERNAME='\"${FOLDER}\"'%g' DepFolderControlFile.xml
check $?

I'm confused. When I tried running Ariean's script it worked as I expected without changing it at all. What did it do that was incorrect?

I don't see why adding an operand when invoking check would make any difference at all since $1 is never referenced in the check function. If the $? I marked in orange color in the function is changed to $1 it would test the value of the operand instead of the exit status of the last command executed, but that also should act just like the Ariean's original code.

According to the standards, the $? in the if statement in the check function will be evaluated when the function is run, not when the function is defined; so I don't see any reason to make this change???

Last edited by Don Cragun; 08-30-2013 at 02:49 AM.. Reason: Fix punctuation.
# 4  
Old 09-10-2013
The contents of the DepFolderControlFile.xml file are
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE DEPLOYPARAMS SYSTEM "/db_apps/informatica/9.5.1/server/bin/depcntl.dtd">

<DEPLOYPARAMS
DEFAULTSERVERNAME="Test_Int"
COPYPROGRAMINFO="NO"
COPYMAPVARPERVALS="NO"
RETAINMAPVARPERVALS="NO"
COPYWFLOWVARPERVALS="NO"
COPYWFLOWSESSLOGS="NO"
COPYDEPENDENCY="YES"
COPYDEPLOYMENTGROUP="YES"
VALIDATETARGETREPOSITORY="YES"
LATESTVERSIONONLY="NO"
RETAINGENERATEDVAL="YES">

<DEPLOYFOLDER>
<DEPLOYEDFOLDEROWNER USERNAME="InfaOperator"
SECURITYDOMAIN="Native"/>
</DEPLOYFOLDER>

</DEPLOYPARAMS>

so when i do grep -i "SOURCEFOLDERNAME" DepFolderControlFile.xml it is working fine as expected.echo $? is returning 1

But when i do sed -i 's%SOURCEFOLDERNAME=[^ ]*%SOURCEFOLDERNAME="FFCB2012"%g' DepFolderControlFile.xmlecho $? is returning 0, even though that string doesn't exist which is wrong.

Thank you.
# 5  
Old 09-10-2013
Quote:
Originally Posted by Ariean
The contents of the DepFolderControlFile.xml file are
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE DEPLOYPARAMS SYSTEM "/db_apps/informatica/9.5.1/server/bin/depcntl.dtd">

<DEPLOYPARAMS
DEFAULTSERVERNAME="Test_Int"
COPYPROGRAMINFO="NO"
COPYMAPVARPERVALS="NO"
RETAINMAPVARPERVALS="NO"
COPYWFLOWVARPERVALS="NO"
COPYWFLOWSESSLOGS="NO"
COPYDEPENDENCY="YES"
COPYDEPLOYMENTGROUP="YES"
VALIDATETARGETREPOSITORY="YES"
LATESTVERSIONONLY="NO"
RETAINGENERATEDVAL="YES">

<DEPLOYFOLDER>
<DEPLOYEDFOLDEROWNER USERNAME="InfaOperator"
SECURITYDOMAIN="Native"/>
</DEPLOYFOLDER>

</DEPLOYPARAMS>

so when i do grep -i "SOURCEFOLDERNAME" DepFolderControlFile.xml it is working fine as expected.echo $? is returning 1

But when i do sed -i 's%SOURCEFOLDERNAME=[^ ]*%SOURCEFOLDERNAME="FFCB2012"%g' DepFolderControlFile.xmlecho $? is returning 0, even though that string doesn't exist which is wrong.

Thank you.
There was no error. The sed command successfully changed every occurrence of the string SOURCEFOLDERNAME= followed by any string of characters not containing a <space> in the file named DepFolderControlFile.xml to the string SOURCEFOLDERNAME="FFCB2012". The fact that SOURCEFOLDERNAME didn't appear in the file doesn't mean that sed failed to globally replace every occurrence of the string that was (or in this case wasn't) in the file.
# 6  
Old 09-10-2013
If the pattern for which you want to change the value (in this case SOURCEFOLDERNAME) can only occur on one line in your input file, the following ed command will update the line containing that pattern (or those patterns) and return a zero exit status if the pattern appears in the file; otherwise it will exit with a non-zero exit status:
Code:
ed DepFolderControlFile.xml <<-EOF > /dev/null
        /SOURCEFOLDERNAME=/s%\(SOURCEFOLDERNAME=\)[^ ]*%\1"FFCB2012"%g
        w
        q
EOF
echo $?

If the pattern appears on more than one line and you want to change every occurrence, it could be done with awk, but I haven't taken the time to work that out. (I assume that unlike the sample input your showed us, more than one deploy parameter can appear on a line. Otherwise, you wouldn't have needed to use a pattern that only matched up to a space character. But, I don't understand why you needed the g flag on your sed substitute command. Surely, you don't need two or more SOURCEFOLDERNAME= parameters on the same line, do you? Nonetheless, the ed script above will also work with two or more of these on a single line; it just won't do what you want if there is more than one line containing SOURCEFOLDERNAME="SomeValue" one or more times.)

Last edited by Don Cragun; 09-10-2013 at 05:51 PM.. Reason: Fix typo
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 09-10-2013
sed does not return the status of the last match or substitute command.
You can use an extra grep for this
Code:
grep 'SOURCEFOLDERNAME=' DepFolderControlFile.xml
saveexit=$?
sed -i 's%SOURCEFOLDERNAME=[^ ]*%SOURCEFOLDERNAME="'${FOLDER}'"%g' DepFolderControlFile.xml
test $saveexit -eq 0
check


Last edited by MadeInGermany; 09-10-2013 at 08:36 PM..
This User Gave Thanks to MadeInGermany For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/get the exit status of a remote command executed on remote host through script

Geeks, Could you please help me out in my script and identify the missing piece. I need to check/get the exit status of a remote command executed on remote host through script and send out an email when process/processes is/are not running on any/all server(s). Here's the complete... (5 Replies)
Discussion started by: lovesaikrishna
5 Replies

2. Shell Programming and Scripting

sed commands success / fail from commandline vs ksh script

solaris 5.10 Generic_138888-03 sun4v sparc SUNW,Sun-Fire-T200 I need a sed command that tests true when presented with lines that contain either forward and backslash. input file: c:/myFile.txt c:\yourFile.txt It doesn't appear that sed (in my environment anyway) supports... (4 Replies)
Discussion started by: msutfin
4 Replies

3. Programming

Python Conditional Statements Based on Success of Last Command

In shell scripting, I can create a conditional statement based on the success or failure (exit status)of a command such as: pinger() { ping -c 2 $remote_host >/dev/null 2>&1 ping_stat=$? } pinger if ]; then echo "blahblahblah" exit 0 fi how is this done using Python using... (3 Replies)
Discussion started by: metallica1973
3 Replies

4. UNIX for Dummies Questions & Answers

Check the exist status of the cd command

Hi, As in scripting , some cd commands getting failed, so we do check the exist status as 0 or 1. But every time we call to function for it. But does any single line exist will do the job with || , && ? i.e ls -l Logs cd Logss | exit echo hias Logss is not exist , Before printing "hi" we... (5 Replies)
Discussion started by: posix
5 Replies

5. Shell Programming and Scripting

Success/failure status of telnet connection

Hi, I am running a shell script which will spawn the telnet and login. But sometimes, the telnet session itself is not getting spawned. My requirement is, if the telnet session is not spawned, the user must be notified that it failed. Is there any command to capture the status of telnet... (2 Replies)
Discussion started by: merin
2 Replies

6. Shell Programming and Scripting

How to check status of tar command?

Hi, How to check the status of tar command using bash script ? I have the following tar command, how to check the status of the tar? tar -cjf $today.tar.bz2 /opt/data Regards, Eye Gee (1 Reply)
Discussion started by: egkua
1 Replies

7. Shell Programming and Scripting

How to check status of last print command?

I am working on an Linux based application where I am using lp -onobanner -s -d$RPTDEST command to print the file on desired printer. Variable $RPTDEST could be different each time even for the same user. I need to implent the check if last print command was succesful or not, so that application... (3 Replies)
Discussion started by: dpmore
3 Replies

8. Shell Programming and Scripting

Failed to check status code in "rsh" command

Hi folks, I wrote a ksh program which run scripts from remote server. To check the status code I wrote the following function: check_remote_status() { status_code=`tail -1 $installLog` if ] ; then echo $errMsg | tee -a $installLog exit 1 else echo $validMsg >> $installLog fi... (9 Replies)
Discussion started by: nir_s
9 Replies

9. Shell Programming and Scripting

check the status and send an email with status

Hi, We have a text file which has the following data. ISA~00~ ~00~ ~ZZ~VISTN ~ZZ~U1CAD ~051227~183 7~U~00200~000011258~0~P~< GS~FA~EE05J~U1CAD~051227~1831~000011258~X~002002 ST~997~0001 AK1~SH~247 AK2~856~2470001 AK5~A AK2~856~2470002 AK5~A... (3 Replies)
Discussion started by: isingh786
3 Replies

10. UNIX for Dummies Questions & Answers

Success status of mailx command

Hi I want to know if the email address in the mailx exists or not Eg: Mailx -s "Subj" hello@ab.com How do I know if the email address is a valid one??? (4 Replies)
Discussion started by: superprogrammer
4 Replies
Login or Register to Ask a Question