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
#!/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 03:49 AM..
Reason: Fix punctuation.
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.
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.
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 06:51 PM..
Reason: Fix typo
This User Gave Thanks to Don Cragun For This Post:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)