Search This Blog

Monday, June 17, 2013

Tables in Bash

There are many situation where a simple Bash script is more than enough to get a job done. But as much as I like Bash its primitive and quite sensitive syntax make me think twice before I code something more complex. Below is a nice trick I found how to deal with tables in Bash.

The most up to date gist can be found here: https://gist.github.com/rtomaszewski/5799274
 
#!/bin/bash
 
TESTS[0]=a,b,c
TESTS[1]=1,2,3
 
for row in "${TESTS[@]}"; do
    IFS=","
    set $row
    col1=$1
    col2=$2
    col3=$3
 
    echo "row was: $row"
    echo "col1 is $col1, col2 is $col2, col3 is $col3"
done

No comments:

Post a Comment