incorrect array values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers incorrect array values
# 1  
Old 01-04-2008
Question incorrect array values

I have a file with 2 lines and 3 filelds

-bash-3.00$ cat del_old_files.cfg
$DBA_WORK_DIR *sybdba* 30
$DBA_LOG_DIR *sybdba* 30


I have tried to set up arrays for each field (bash shell)

declare -a dirArray=`cut -d' ' -f1 < del_old_files.cfg`
declare -a nameArray=`cut -d' ' -f2 < del_old_files.cfg`
declare -a daysArray=`cut -d' ' -f3 < del_old_files.cfg`

when I query the array I get

-bash-3.00$ echo ${dirArray[0]}; echo ${nameArray[0]}; echo ${daysArray[0]}
$DBA_WORK_DIR $DBA_LOG_DIR
*sybdba* *sybdba*
30 30
-bash-3.00$ echo ${dirArray[1]}; echo ${nameArray[1]}; echo ${daysArray[1]}



logically I would expect the [0] and [1] queries to return only 1 value

Where did I go wrong?
# 2  
Old 01-04-2008
Quote:
Originally Posted by jhillier
Where did I go wrong?
what do you want to do actually?
# 3  
Old 01-04-2008
I want to be able to build a find command to delete files older than the number

find `echo ${dirArrary[0]}` -name "`echo ${nameArray[0]}`" -mtime +`echo{daysArray[0]}` -exec rm {}\;
# 4  
Old 01-04-2008
Code:
#!/bin/sh
while read line 
do
    set -- $line
    find $1 -name \"$2\" -mtime +"$3"
done < file

# 5  
Old 01-04-2008
this fails as it is not converting the variables $DBA_WORK_DIR and $DBA_LOG_DIR

-bash-3.00$ while read line ; do set -- $line; find $1 -name \"$2\" -mtime +"$3"; done < del_old_files.cfg
find: stat() error $DBA_WORK_DIR: No such file or directory
find: stat() error $DBA_LOG_DIR: No such file or directory

yet the find statement works outside of the while statement



find $DBA_WORK_DIR -name "*sybdba*" -mtime +30
produces the expected list of files
# 6  
Old 01-04-2008
this fails as it is not converting the variables $DBA_WORK_DIR and $DBA_LOG_DIR

-bash-3.00$ while read line ; do set -- $line; find $1 -name \"$2\" -mtime +"$3"; done < del_old_files.cfg
find: stat() error $DBA_WORK_DIR: No such file or directory
find: stat() error $DBA_LOG_DIR: No such file or directory

yet the find statement works outside of the while statement



find $DBA_WORK_DIR -name "*sybdba*" -mtime +30
produces the expected list of files


output with set -x

-bash-3.00$ ./jjh
+ read line
+ set -- '$DBA_WORK_DIR' '*sybdba*' 30
+ find '$DBA_WORK_DIR' -name '"*sybdba*"' -mtime +30
find: stat() error $DBA_WORK_DIR: No such file or directory
+ read line
+ set -- '$DBA_LOG_DIR' '*sybdba*' 30
+ find '$DBA_LOG_DIR' -name '"*sybdba*"' -mtime +30
find: stat() error $DBA_LOG_DIR: No such file or directory
+ read line
-bash-3.00$

so how do I get rid of the single quotes?
# 7  
Old 01-04-2008
Quote:
Originally Posted by jhillier
this fails as it is not converting the variables $DBA_WORK_DIR and $DBA_LOG_DIR

-bash-3.00$ while read line ; do set -- $line; find $1 -name \"$2\" -mtime +"$3"; done < del_old_files.cfg
find: stat() error $DBA_WORK_DIR: No such file or directory
find: stat() error $DBA_LOG_DIR: No such file or directory

yet the find statement works outside of the while statement



find $DBA_WORK_DIR -name "*sybdba*" -mtime +30
produces the expected list of files
How did you create your del_old_files.cfg? If you can, put in a real path instead of $DBA_WORK_DIR. Otherwise, you have to define these variables somewhere in the script, so that they contain path values
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with the incorrect number of array elements

Hello , I have a file : RestartSession.txt with the below contents : Backup p203pcrw01_OS_Weekly Failed full 10/11/2015 10:00:07 PM 1444572007 10/11/2015 10:26:23 PM 1444573583 0:00 0:26 18.76 1 08 0 0 0 2 2 180668 ... (4 Replies)
Discussion started by: rahul2662
4 Replies

2. Red Hat

Values in array are not able to be used one by one

hey,i stored the value of an sql query in an array and then tried to use that value in while loop. actually my array will have two or more values, then according to the values i have to display result. #!/bin/bash -xv val_1=$( sqlplus -s rte/rted1@rel75d1 << EOF set heading off select... (1 Reply)
Discussion started by: ramsavi
1 Replies

3. Red Hat

How to check for values in array?

i stored some values in array , then i traverse through the array and check for some values and if they exist then echo success. let us consider that in our array we stored values from an sql query like this #!/bin/bash declare -a arr arr=$( sqlplus -s rte/rted2@rel76d2 << EOF set... (1 Reply)
Discussion started by: ramsavi
1 Replies

4. UNIX for Dummies Questions & Answers

ID incorrect field values in dat file and output to new file

Hi All I have a .dat file, the values are seperated by ". I wish to identify all field values in field 14 that are not '01-APR-2013' band then copy those records to a new file. Can anyone suggest the UNIX command required. Thanks in advance Andy (2 Replies)
Discussion started by: aurum1313
2 Replies

5. Programming

Selecting array values

I have two arrays DIST(1:NCOF) and X(1:NX) Let NCOF = 5 and NX = 15, with DIST = and X = I want to create an array that puts a zero if DIST is outside the region in X, otherwise putting 1. In this example I should get RES = Using DIST = would give RES = The values in... (6 Replies)
Discussion started by: kristinu
6 Replies

6. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

7. Shell Programming and Scripting

Store values in an Array

Hi all. Well, I have the next code: I need to make an array with the values I have in the bucle, but just don't get it... Question is, how can I store in an array that values, and how can I display them with echo? (8 Replies)
Discussion started by: crcbad
8 Replies

8. UNIX for Dummies Questions & Answers

Compare array values

# tail myprocesses.txt 178 processes at Tue Oct 21 14:33:01 IST 2008 16 MySQL processes at Tue Oct 21 14:33:01 IST 2008 175 processes at Tue Oct 21 14:36:01 IST 2008 60 MySQL processes at Tue Oct 21 14:36:01 IST 2008 192 processes at Tue Oct 21 14:39:01 IST 2008 64 MySQL processes at Tue Oct... (2 Replies)
Discussion started by: shantanuo
2 Replies

9. Shell Programming and Scripting

Array values treated as one

I want each integer to be a value/element in the array, however the string is being treated as one. How can I stream these into distinct values? PSF6INDEX=`(snmpwalk -v 2c -c 'H0meru!es' ${SWITCH} .1.3.6.1.2.1.2.2.1.2 | grep 'GigabitEthernet' | sed 's/IF-MIB::ifDescr\.//g' | awk '{print $1}' |... (1 Reply)
Discussion started by: cytriesbash
1 Replies

10. Shell Programming and Scripting

array values in a command

:confused: I am a newbie to scripting so I am probably missing something but when this function runs it just returns blank lines, but if I surround the sed with a print it returns the full commands that can be excuted on the command line ne_ip_change() { set -A NE `$CAT /tmp/ne_list` ... (3 Replies)
Discussion started by: eeisken
3 Replies
Login or Register to Ask a Question