LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 02-12-2008, 12:54 PM   #1
myint45
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 .lock.pid -exec cat {} \;"
chvpklnxq1dev.chvpk.chevrontexaco.net|15734|Tue Feb 12 02:00:02 2008
chvpklnxq1dev.chvpk.chevrontexaco.net|15734|Tue Feb 12 02:00:04 2008
chvpklnxq1dev.chvpk.chevrontexaco.net|15734|Tue Feb 12 02:00:04 2008
chvpklnxq1dev.chvpk.chevrontexaco.net|15734|Tue Feb 12 02:00:02 2008
chvpklnxq1dev.chvpk.chevrontexaco.net|15734|Tue Feb 12 02:00:02 2008
chvpklnxq1dev.chvpk.chevrontexaco.net|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:
#!/bin/bash
chkdt1=$(date | awk -F " " '{print $2,$3}')
#echo "${chkdt1}";
declare -a lock=$(find /store -name .lock.pid -exec cat {} \;
chkdt=$(echo "$lock[1]" | awk -F" " '{print $2,$3}');
echo "$lock";
for i in "$lock"
do
if [ "${chkdt}X" != "${chkdt1}X" ]
then
echo "dates don't match";
else
echo "dates do match";
fi
done
============

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
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
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:
Code:
#!/bin/bash

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

find /store -name .lock.pid -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";
	else
		echo "dates do match";
	fi
done
You can clean this up further (getting rid of awk) to obtain:
Code:
#!/bin/bash

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

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

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

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


Reply

Tags
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 03:22 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration