Sponsored Content
Full Discussion: Using GREP in IF Statement
Top Forums Shell Programming and Scripting Using GREP in IF Statement Post 302815391 by Ariean on Friday 31st of May 2013 04:07:08 PM
Old 05-31-2013
Quote:
Originally Posted by MadeInGermany
-a and -z work inside the test command. -a is logical AND.
Here you need &&, the shell's logical AND.
Do not use `command in backticks`! It runs the command output in a subshell!
No harm *here* because grep -q suppresses the output. But generally confusing and risky.
Last but not least, it is recommended to "quote" variables in command arguments.
Not required *here* because you set them to sane values.
Code:
echo $UNINUM
[ ! -z ${UNINUM} ]

do odd things if UNINUM has a * or a space character.
Code:
echo "$UNINUM"
[ ! -z "$UNINUM" ]

are safe.

Your comments raised few doubts in my code, can you please guide me here

1) I am always using the flower brackets for all variables in my shell script like ${VAR}, is that a wrong approach? one issue i just learned is without double quotes the newlines are lost to word split, are there any other drawbacks before i go ahead and replace everywhere in my scripts?

2) I have the following piece code which i am executing via shell script, i am not suppose to use the backticks since it opens up and executes the code in a new sub shell? this works fine for me and serves the purpose

Code:
#### SECOND UPDATE OF VALIDATION STATUS IN FEED_PROCESS_LOG TABLE ####

        logit "Updating the Validation Status Field in ${DBSTGUSER}.FEED_PROCESS_LOG Table as COMPLETED With Matching Parameters"
        logit "UNINUM :: ${UNINUM} PROVIDER :: ${PROVIDER} EXTRACT_DT :: ${EXTRACT_DT} VER_NUM :: ${VER_NUM}"

        `${ORACLE_HOME}/bin/sqlplus ${DBSTGUSER}/${DBSTGPASS}@${DBSRC} <<EOF >> ${UPD_FEED_PROC_LOG}
        spool ${UPD_SQL2}
        UPDATE ${DBSTGUSER}.FEED_PROCESS_LOG SET VALIDATION_STATUS='COMPLETED'
        WHERE UNINUM='${UNINUM}' and PROVIDER='${PROVIDER}' and EXTRACT_DT='${EXTRACT_DT}'
        and VER_NUM=${VER_NUM} and ACT_END_DT IS NULL  AND VALIDATION_STATUS='RUNNING';
        spool off;
        COMMIT;
        EXIT;
        EOF`

        if grep -q "ERROR" ${UPD_FEED_PROC_LOG}
        then
                logit "${DBSTGUSER}.FCA_HASH_TOTALS_PKG.HASH_TOTALS_COMPUTE Procedure Failed"
                logit "Please Check ${UPD_FEED_PROC_LOG} Log File For Detailed Information"

                exit 11
        else
                logit "${DBSTGUSER}.FCA_HASH_TOTALS_PKG.HASH_TOTALS_COMPUTE Procedure Completed Successfully"
                logit "Please Check ${UPD_FEED_PROC_LOG} Log File For Detailed Information"
        fi

Thank you.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using grep in if statement

Can somebody please guide me towards right syntax: #!/bin/ksh if i = $(grep $NAME filename) echo "Name Found" else echo " Name not Found" fi I need to grep for $NAME in the file, and if it returns false, execute a series of commands and if true, exit out. The above is not the right... (3 Replies)
Discussion started by: chiru_h
3 Replies

2. Shell Programming and Scripting

Using grep in a test/if statement

Okay, well this is more or less my first attempt at writing a shell script. Anyways, here's my code: cd ${PATH} if then rm ${FILE} ./anotherScript else exit 1 fi exit 1 Anyways, it's a pretty simple script that is supposed to search for the... (4 Replies)
Discussion started by: cbo0485
4 Replies

3. Shell Programming and Scripting

Swich statement based on a grep?

I'm piping the results of a tcpdump into a shell script and want certain commands executed (specifically the firing of an SNMP alert) based on a grep for certain contents in the output of tcpdump. For example, I have #!/bin/bash while read str; do grep 'ttl 64' -q && sudo snmptrap -v 1... (2 Replies)
Discussion started by: paulobrad
2 Replies

4. UNIX for Dummies Questions & Answers

grep entire statement not just line

(extract from SQL binlog file...) # at 4960 #080801 14:35:31 server id 4 end_log_pos 195 Query thread_id=63121426 exec_time=0 error_code=0 use d_jds; SET TIMESTAMP=1217581531; UPDATE bid_details set bidding = 3170.37 ,deduction=if((3170.37 < 37.43),0,deduction) where... (3 Replies)
Discussion started by: shantanuo
3 Replies

5. Shell Programming and Scripting

Grep statement

Hi All, Please can somebody advise that if I want to search a pattern xyz the grep command should only select xyz and not any other pattern containing xyz (ex abxyzcd) Regards (1 Reply)
Discussion started by: Shazin
1 Replies

6. Shell Programming and Scripting

sed / grep / for statement performance - please help

I'm searching the most effective way of doing the following task, so if someone can either provide a working solution with sed or one totally different but more effective then what I've got so far then please go ahead! The debugme directory has 3 subdirectorys and each of them has one .txt file... (7 Replies)
Discussion started by: TehOne
7 Replies

7. Shell Programming and Scripting

if statement with grep as conditional

Please see the script segment below for i in $files do echo $i if ; then case "$1" in "IE0263"|"IE0264"|"IE0267"|"IE0268") short_filename=`ls -l $i | cut -c108-136 | sort` ;; "IE0272"|"IE0273") short_filename=`ls -l $i | cut... (4 Replies)
Discussion started by: jmahal
4 Replies

8. Shell Programming and Scripting

Use awk/sed/grep with goto statement!

Hi, I have an array with characters and I am looking for specific character in that array and if those specific character not found than I use goto statment which is define somehwhere in the script. My code is: set a = (A B C D E F) @ i = 0 while ($i <= ${#a}) if ($a != "F" || $a != "D")... (3 Replies)
Discussion started by: dixits
3 Replies

9. UNIX for Dummies Questions & Answers

Grep Segmentation Fault If statement

I receive a segmentation fault on some servers when grepping a string, but not when I use -i, and vice-versa. I have googled this and realise it is usually a hardware / memory issue. I'd like to write an if statement that says "if grep string returns 'Segmentation fault' then grep the same... (4 Replies)
Discussion started by: MaindotC
4 Replies

10. Shell Programming and Scripting

Ps -ef|grep <processname> usage in IF statement

Hi, Am working in a filenet domain where we are using AIX as our terminal to run the jobs and schedule the shell scripts to run . In my previous post regarding the "Log modification with finding errors" ... (12 Replies)
Discussion started by: Kalaihari
12 Replies
PPI::Statement::Data(3) 				User Contributed Perl Documentation				   PPI::Statement::Data(3)

NAME
PPI::Statement::Data - The __DATA__ section of a file SYNOPSIS
# Normal content __DATA__ This: data is: part of: the PPI::Statement::Data: object INHERITANCE
PPI::Statement::Compound isa PPI::Statement isa PPI::Node isa PPI::Element DESCRIPTION
"PPI::Statement::Data" is a utility class designed to hold content in the __DATA__ section of a file. It provides a single statement to hold all of the data. METHODS
"PPI::Statement::Data" has no additional methods beyond the default ones provided by PPI::Statement, PPI::Node and PPI::Element. However, it is expected to gain methods for accessing the data directly, (as a filehandle for example) just as you would access the data in the Perl code itself. TO DO
- Add the methods to read in the data - Add some proper unit testing SUPPORT
See the support section in the main module. AUTHOR
Adam Kennedy <adamk@cpan.org> COPYRIGHT
Copyright 2001 - 2011 Adam Kennedy. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. perl v5.18.2 2011-02-25 PPI::Statement::Data(3)
All times are GMT -4. The time now is 06:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy