View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-12-2008, 12:54 PM   #1
LQ Newbie
Registered: Aug 2006
Posts: 10

Rep: Reputation: 0
Question debug help on BASH arrays and for looop.

ok, here's the output of "find /store -name -exec cat {} \;"|15734|Tue Feb 12 02:00:02 2008|15734|Tue Feb 12 02:00:04 2008|15734|Tue Feb 12 02:00:04 2008|15734|Tue Feb 12 02:00:02 2008|15734|Tue Feb 12 02:00:02 2008|15734|Tue Feb 12 02:00:04 2008

I'm trying to extract the date so that I can compare it to the current date, using the date command. And then I want to see if they match on each occasion; this is what I've written:
chkdt1=$(date | awk -F " " '{print $2,$3}')
#echo "${chkdt1}";
declare -a lock=$(find /store -name -exec cat {} \;
chkdt=$(echo "$lock[1]" | awk -F" " '{print $2,$3}');
echo "$lock";
for i in "$lock"
if [ "${chkdt}X" != "${chkdt1}X" ]
echo "dates don't match";
echo "dates do match";

I'm getting dates don't match; which they should. But HERE'S THE REAL QUESTION...WHY IS IT THAT THE ARRAY IS NOT WORKING..BECAUSE THE $LOCK ECHO IS RETURNING THE WHOLE array instead of the $lock[1] I'm addressing?
Old 02-12-2008, 01:32 PM   #2
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 71
The arrays aren’t working because you although you declare lock to be an array, you end up assigning to it as a scalar. So you have $lock[0] containing the entire output, and $lock[1] being blank.

Btw, you don’t need arrays for this:

chkdt1=$(date | awk -F " " '{print $2,$3}')

find /store -name -exec cat {} \; | while read line; do
	chkdt=$(echo $line | awk -F" " '{print $2,$3}')
	if [ "${chkdt}X" != "${chkdt1}X" ]; then
		echo "dates don't match";
		echo "dates do match";
You can clean this up further (getting rid of awk) to obtain:

chkdt1=$(date | cut -d' ' -f2,3)

find /store -name -exec cat {} \; | while read pref mon day suf; do
	if [ "${mon} ${day}X" != "${chkdt1}X" ]; then
		echo "dates don't match";
		echo "dates do match";

Last edited by osor; 02-12-2008 at 01:37 PM. Reason: clean up further
Old 02-12-2008, 10:27 PM   #3
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 242Reputation: 242Reputation: 242
find /store -name -exec cat {} \; | awk 'BEGIN { FS="[ |]"
   current=strftime("%b %d %Y",systime())
current == $4" "$5" "$7
' "file"


shell, bash, array

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
bash: Passing arrays with spaces jakeo25 Programming 8 06-05-2009 08:00 AM
bash arrays question introuble Programming 1 05-20-2006 03:07 AM
Generating iteratable arrays in bash d00bid00b Programming 4 08-15-2005 04:54 PM
How do you debug a bash script meadensi Linux - Newbie 4 02-23-2005 01:47 PM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 12:58 PM

All times are GMT -5. The time now is 04:01 PM.

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