Compact script with array - Good idea?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compact script with array - Good idea?
# 1  
Old 07-24-2012
Compact script with array - Good idea?

Hi,

I have a shell script where a lot of the code is repeated. I wanted to make the code much more compact so I spoke to a guy and he suggested using arrays, like follows:

Code:
#!/bin/bash

readonly -a nginx=('nginx' '--prefix=/opt'  '-j 4'
    'http://nginx.org/download/nginx-1.2.2.tar.gz' 
)
readonly -a supervisor=('supervisor' '--prefix=/opt'  '-j 4'
    'http://pypi.python.org/packages/source/s/supervisor/supervisor-3.0a12.tar.gz'
)

# Download, configure, build and install the given application array
#
# $1 is an application array
#
# Note: The four first lines are used to correctly get the array parameter
# - Setting the shell's Internal Field Separator to null
# - Create a string containing "array content"
# - assign app value to $1[*] using indirect variable reference
# - Resetting IFS to default
#
#
function install()
{
    OLD_IFS=$IFS; IFS=''; 
    local array_string="$1[*]" 
    local app=(${!array_string})
    IFS=$OLD_IFS

    echo "Installing '${app[0]}' from '${app[3]}' "
    echo "configure '${app[1]}'"
    echo "make '${app[2]}'"
}

install nginx
install supervisor

I like this, because not only is the code nice and short, but it stops repetition where operations are similar, for example downloading files.

Is there anything wrong with doing this?
# 2  
Old 07-24-2012
It looks pretty, but I wouldn't call it organized. You've got lots of data duplication -- why bother storing nginx in an array named nginx -- and if both things get the same prefix and make options, why not split that out too? And you're not looping things you can loop. And you're desperately avoiding splitting when it's something that could replace 80% of your code here. And using arrays means you end up spitting out ${ARR[1]} and ${ARR[2]} everywhere instead of using meaningful variable names. Why is '-j 4' the third element of your array and not the second, fifth, ninth? Only the original programmer knows.

In short this code will be as hard for you to read later as it was for me to read now. Smilie

I'd still split the URL's out since they're big and unwieldy.

Code:
MAKE_OPTS="-j 4"
CONFIG_OPTS="--prefix=/opt"

nginx="http://nginx.org/download/nginx-1.2.2.tar.gz"
supervisor="http://pypi.python.org/packages/source/s/supervisor/supervisor-3.0a12.tar.gz"

for PACKAGE in nginx supervisor
do
        echo wget "${!PACKAGE}"
        echo configure $CONFIG_OPTS ... # Note not quoted
        echo make $MAKE_OPTS ... # Note not quoted
done

I suppose you could have variables overriding the default $CONFIG_OPTS and $MAKE_OPTS for a particular package. If they're blank, use the default options, if they're not, use them instead.

Last edited by Corona688; 07-24-2012 at 12:22 PM..
# 3  
Old 07-24-2012
Code:
#!/bin/bash
# Requires bash, don't use other shells

MAKE_OPTS="-j 4"
CONFIG_OPTS="--prefix=/opt"

url_nginx="http://nginx.org/download/nginx-1.2.2.tar.gz"
nginx_MAKE="-j 3" # Override default MAKE_OPTS

url_supervisor="http://pypi.python.org/packages/source/s/supervisor/supervisor-3.0a12.tar.gz"
supervisor_CONFIG="--prefix=/usr" # Override default CONFIG_OPTS

for PACKAGE in nginx supervisor
do
        # Check for package_MAKE variable, use default $MAKE_OPTS if absent
        MOPTS="${PACKAGE}_MAKE"
        MOPTS="${!MOPTS:-${MAKE_OPTS}}"
        # Check for package_CONFIG variable, use default $CONFIG_OPTS if absent
        COPTS="${PACKAGE}_CONFIG"
        COPTS="${!COPTS:-${CONFIG_OPTS}}"

        PACKAGE="url_${PACKAGE}"
        echo wget "${!PACKAGE}"
        echo configure $COPTS ... # Note not quoted
        echo make $MOPTS ... # Note not quoted
done

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

need a new idea for a script

Hi all, I now have project in UNIX Solaris and I want to have some new ideas to execute it, so I hope you help me finding new ideas in scripting or some infrastructure .bye (1 Reply)
Discussion started by: hard_revenge
1 Replies

2. UNIX for Advanced & Expert Users

echo 1 > /proc/sys/vm/drop_caches a good idea?

Hi folks. I work with several production servers, and I have seen in some Kernel Cache using most of the memory. See this pic: http://i51.tinypic.com/301nb6c.jpg Do you think this is a smart choice? Remember these are productions servers and it is extremely necesary this does not... (6 Replies)
Discussion started by: erick_tuk
6 Replies

3. Shell Programming and Scripting

Help with the idea of a script to modify time

Hello friends, I need an idea or a ready solution for a problem i have the following lines in text file: 1 20100920140122 object4 MOVE IN 2 20100920150012 object4 MOVE OUT -- cut -- the second column is the date and time: 20100920140122 = 2010 09 20 14:01.22 what I need to do is to add 40... (8 Replies)
Discussion started by: peetteerr
8 Replies

4. Shell Programming and Scripting

Script to compact information !

Hello Group, Could you please help me with a shell script that condense in this case each 12 rows of hour information in only one row per day as following: Final Fields in each row 1.Ticker Name 2. D (Daily) 3. Date 4. Open Price (The open price of the first record of the day) 5. High... (13 Replies)
Discussion started by: csierra
13 Replies

5. Shell Programming and Scripting

Script Idea's / Help

Hi there, I'm pretty new to scripting and wondering if anyone had any idea's, scripts or snippets on how I can do the following. I basically want a shell script that will look at all the files in a directory and find all the names and addresses in them then output them to the screen nicely... (12 Replies)
Discussion started by: mrpugster
12 Replies

6. Shell Programming and Scripting

any good idea on this?

txt file like this, 1 2 3 4456 a bb c d 3 f e 1 k 32 d m f e 123 m 2 k every line contains 3 or more columns, all the columns are separated by space, and every column includes 1 to 3 character. what I wanna do is deleting the first three columns, and keep the rest no matter how long... (7 Replies)
Discussion started by: fedora
7 Replies

7. Shell Programming and Scripting

Limitations of awk? Good idea? Bad idea?

Keeping in mind that I'm relatively comfortable with programming in general but very new to unix and korn/bourne shell scripts.. I'm using awk on a CSV file, and then performing calculations and operations on specific fields within specific records. The CSV file I'm working with has about 600... (2 Replies)
Discussion started by: yongho
2 Replies

8. UNIX for Dummies Questions & Answers

Dual Boot a good idea?

I was just wondering if it would be alright to dual boot a machine with both UNIX using XTERM for the interface and windows 98 or if people consider this a bad idea? if you consider it bad do tell me some possible alternatives..also would it be better to get linux over pure unix? I'd like a visual... (8 Replies)
Discussion started by: PravusMentis
8 Replies
Login or Register to Ask a Question