Unfamiliar array syntax


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unfamiliar array syntax
# 1  
Old 09-16-2010
Unfamiliar array syntax

I recently was handed responsibility for a script which has array references that are new to me and I could use some help understanding them.

Parts of the config file and script are included. This script collects backups from a jail server of the jails running on that host. Several jail servers are backed up.

In the first config file the collect0_backup_host="host.xxx.net" refers to the jail host.
It is the syntax of collect0_backup_host that throws me. collect0 is translated to
$collect${N} in the script -- this part I understand -- but the _backup-host (variable?)
that is appended to collect0 is confusing me. I've never seen an array defined this way before and could use some useful links or explaination of how this works.

Thanks
thumper

Code:
config file

collect0_backup_host="host.xxx.net"
collect0_jails="host9.xxx.net host1.xxx.net host2.xxx.net host4.xxx.net"
collect0_backups_dir="/backups/"
collect0_remote_user="host_backups"
collect0_private_key_file="/etc/ssh/jail_backups/host_backups"
#
collect1_backup_host="alt-host.xxx.net"
collect1_jails="dumbo.xxx.net mickey.xxx.net"
collect1_backups_dir="/backups/"
collect1_remote_user="snark_backups"
collect1_private_key_file="/etc/ssh/jail_backups/snark_backups"
#



script


backup_host=`eval echo \\$collect${N}_backup_host`


while backup_host do
  do-something
done


jails=`eval echo \\$collect${N}_jails`

    remote_user=`eval echo \\$collect${N}_remote_user`
    if [ ! ${remote_user} ]; then
        echo
        echo "ERROR: no remote user defined for collecting backups from ${backup_host}"
        echo
        ERROR_JAILS="${ERROR_JAILS} ${jails}"



        # iterate to the set of jail variables
        N=$(expr ${N} + 1)
        backup_host=`eval echo \\$collect${N}_backup_host`

        continue


Last edited by Scott; 09-16-2010 at 05:20 PM.. Reason: Replaced ICODE tags with CODE tags
# 2  
Old 09-16-2010
This is construction that is used if a shell in question does not know arrays. The way it is used in the script that you were handed, is a bit cumbersome. Instead of
Code:
backup_host=`eval echo \\$collect${N}_backup_host`

this could be used:
Code:
eval backup_host=\$collect${N}_backup_host

eval works like this: first everything after eval is passed as parameter to eval. The shell processes the variable ${N}. The first $ is escaped ( \$ ), so that it is not treated as a special character. So after processing the command eval gets the following string (suppose N=1):
Code:
backup_host=$collect1_backup_host

end then executes this. As a result you would get:
Code:
$ N=1
$ eval backup_host=\$collect${N}_backup_host
$ echo $backup_host
alt-host.xxx.net


Last edited by Scrutinizer; 09-16-2010 at 06:32 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

2. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

3. UNIX for Dummies Questions & Answers

awk: syntax for "if (array doesn't contain a particular index)"

Hi! Let's say I would like to convert "1", "2", "3" to "a", "b", "c" respectively. But if a record contains other number then return "X". input: 1 2 3 4 output: a b c X What is the syntax for: if(array doesn't contain a particular index){ then print the value "X" instead} (12 Replies)
Discussion started by: beca123456
12 Replies

4. UNIX for Dummies Questions & Answers

awk construct unfamiliar to me

Please help me out: I've seen this construct awk '{...}1'several times, like in scrutinizer's today's post awk '{for(i=2;i<=NF;i++)if($i==$1)$i=RS $i}1' infilebut I can't find (manuals, man pages, internet FAQs,...) an explanation of what it does resp. stands for. Any hint is appreciated! (5 Replies)
Discussion started by: RudiC
5 Replies

5. Shell Programming and Scripting

Help with awk array syntax & counting script

..... (3 Replies)
Discussion started by: elbee11
3 Replies

6. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

7. 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

8. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

9. UNIX for Advanced & Expert Users

unfamiliar errors

I was checking my error logs today and ran into some errors I have not seen before maybe somebody has run into these before? /etc/cron.quarter-hourly/owusers.sh: Set effective gid to mail(12) failed! /usr/local/mailwatch/check_sendmail_relay.sh: line 8: thanks (1 Reply)
Discussion started by: mcraul
1 Replies
Login or Register to Ask a Question