LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 04-21-2020, 08:44 AM   #1
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Rep: Reputation: Disabled
Issue with array comparsion


Hi All,

I am currently trying to do the below requirement.
Navigate to one specific directory and search one pattern from a group of files in the directory and store the count of occurence in an array. Now from the array i need to get the name of the file which had this occurence.
Code:
declare -a merchantName=('merchant1' 'merchant2' 'merchant3' )
declare -a cb
bm=25
dt=$(date --date="3 days ago" +"%Y""%m""%d")
tLen=${#merchantName[@]}
for (( i=0; i<${tLen}; i++ ));
do

cb=($(find /home/user/bhw/ -type f -name "file_feed_*${merchantName[$i]}*_${dt}*.gz.out" -exec grep -i "record" {}  + | wc -l))
	if (( ${cb[i]} > ${bm} )); then
                echo ${merchantName[$i]}
        fi
done
When running the above code getting the exception as
bhw.sh: line 50: > 25 : syntax error: operand expected (error token is "> 25 ")

But if i am pre declaring the count of occurence into each array element script works fine,like as highlighted .


Code:
declare -a merchantName=('merchant1' 'merchant2' 'merchant3' )
declare -a cb
bm=25
dt=$(date --date="3 days ago" +"%Y""%m""%d")
tLen=${#merchantName[@]}
for (( i=0; i<${tLen}; i++ ));
do

cb=($(find /home/user/bhw/ -type f -name "file_feed_*${merchantName[$i]}*_${dt}*.gz.out" -exec grep -i "record" {}  + | wc -l))
cb[0]=0
cb[1]=256
cb[2]=0
	if (( ${cb[i]} > ${bm} )); then
                echo ${merchantName[$i]}
        fi
done
Please let know if the requirement is clear and how to resolve the exception.
 
Old 04-21-2020, 09:13 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
The if statement fails because cb[i] is not defined. Go look at the statement starting with

cb=

Hopefully your mistake is obvious now. For help with syntax error checking goto
shellcheck.net
 
Old 04-21-2020, 09:34 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
additionally I used to suggest to add:
Code:
set -o errexit
set -o nounset
at the beginning. That will not solve anything, but will stop execution in case of a problem (instead of ignoring it and running into the wild)
 
Old 04-21-2020, 09:52 AM   #4
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Original Poster
Rep: Reputation: Disabled
Sorry to ask, i thought below statement in the code has already defined the array named "cb". Any additional statements to code.

Code:
declare -a cb
 
Old 04-21-2020, 10:06 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
yes, but that will not create any element (like cb[1]).
 
Old 04-21-2020, 10:15 AM   #6
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Original Poster
Rep: Reputation: Disabled
Understood,What should be done to make the program function.
 
Old 04-21-2020, 10:27 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Code:
cb=($(find /home/user/bhw/ -type f -name "file_feed_*${merchantName[$i]}*_${dt}*.gz.out" -exec grep -i "record" {}  + | wc -l))
cb[0]=0
You actually answered your own question. The difference between the two lines is that you are assigning a value to an element in the second and it works, the first you are not.
 
1 members found this post helpful.
  


Reply

Tags
array, for loop



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
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
File size comparsion Vthimmap Linux - Newbie 2 10-25-2012 12:31 AM
Open/Net/FreeBSD Comparsion kaon *BSD 3 02-08-2005 11:19 AM
good sequence comparsion algorithm? feetyouwell Programming 2 10-14-2004 05:58 AM
Is there any comparsion chart available for graphic cards? lamiczka Linux - Hardware 1 02-17-2004 11:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:21 PM.

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
Open Source Consulting | Domain Registration