LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-20-2014, 05:31 AM   #1
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Rep: Reputation: Disabled
Can we reduce the piece of code


Hi All,
i am writing a script, here is the piece of code:
Code:
SET_DIAL_DTLS()
{
        SERACH_ID=$1
        DNIS_VAL=$(grep "$SERACH_ID" $DIAL_FEED_DTLS|awk -F '|' '{print $1}')
if [[ -z "${DNIS_VAL}" ]]
then
	echo "DNIS_VAL is Empty...!!"
	DNIS_VAL="No Entry Found"
else
	echo "DNIS_VAL has some value...!!"
	DNIS_VAL=${DNIS_VAL}
fi
	

        CID_VAL=$(grep "$SERACH_ID" $DIAL_FEED_DTLS|awk -F '|' '{print $2}')

if [[ -z "${CID_VAL}" ]]
then
	echo "CID_VAL is Empty...!!"
	CID_VAL="No Entry Found"
else
	echo "CID_VAL has some value...!!"
	DNIS_VAL=${CID_VAL}
fi

        TOT_TRNXS_VAL=$(grep "$SERACH_ID" $DIAL_FEED_DTLS|awk -F '|' '{print $3}')


if [[ -z "${TOT_TRNXS_VAL}" ]]
then
	echo "TOT_TRNXS_VAL is Empty...!!"
	TOT_TRNXS_VAL="No Entry Found"
else
	echo "TOT_TRNXS_VAL has some value...!!"
	TOT_TRNXS_VAL=${TOT_TRNXS_VAL}
fi

        GOOD_TRNXS_VAL=$(grep "$SERACH_ID" $DIAL_FEED_DTLS|awk -F '|' '{print $4}')

if [[ -z "${GOOD_TRNXS_VAL}" ]]
then
	echo "GOOD_TRNXS_VAL is Empty...!!"
	GOOD_TRNXS_VAL="No Entry Found"
else
	echo "GOOD_TRNXS_VAL has some value...!!"
	GOOD_TRNXS_VAL=${GOOD_TRNXS_VAL}
fi


        BAD_TRNXS_VAL=$(grep "$SERACH_ID" $DIAL_FEED_DTLS|awk -F '|' '{print $5}')

if [[ -z "${BAD_TRNXS_VAL}" ]]
then
	echo "BAD_TRNXS_VAL is Empty...!!"
	BAD_TRNXS_VAL="No Entry Found"
else
	echo "BAD_TRNXS_VAL has some value...!!"
	BAD_TRNXS_VAL=${BAD_TRNXS_VAL}
fi

}
is there a better way to write the above function, can we reduce this piece of code?
 
Old 08-20-2014, 07:58 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
It should not take too much resources as it...
I suppose DNIS_VAL, CID_VAL, TOT_TRNXS_VAL etc... have an use outside the function?
 
Old 08-20-2014, 08:11 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,661

Rep: Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256
Well... You could do one grep search, and store the desired fields in an array.

Then do the subsequent tests.

This would eliminate several processes from being used for each test.
 
Old 08-20-2014, 08:32 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Good point, I didn't notice all tests were greping the same thing in the same path
 
Old 08-20-2014, 09:24 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Or even ditch the grep all together and just use awk.

One consideration for the array would be, how do you know which items did not appear?
 
Old 08-20-2014, 05:38 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,661

Rep: Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256
Quote:
Originally Posted by grail View Post
Or even ditch the grep all together and just use awk.

One consideration for the array would be, how do you know which items did not appear?
The print from the awk script can indicate empty entries... In fact, it may even carry out the logic of the rest of the script setting the values really desired rather than the raw values that have to be retested.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Posting piece of code on the internet vinians Linux - Newbie 2 10-03-2009 07:53 PM
piece of code Volcano Programming 8 08-04-2009 09:28 AM
is there a piece of c code that can set the terminal window tinieprotonjam Programming 2 01-27-2007 07:20 AM
Unusual piece of bash script code raypen Slackware 2 03-18-2006 10:49 PM
nice piece of code ....HOW DOES IT WORK? deadhead Programming 7 12-07-2003 12:52 AM


All times are GMT -5. The time now is 03:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration