I had an opportunity to play with Bash script on Friday. My task was to write a small deployment script to grab our server class configuration settings from subversion and rsync them to the appropriate machines.
This was easy enough, a couple of commands to subversion, rsync and some glue and I’d be done. However, an hour into writing it I wish I’d used PHP or used my time to learn how to do it in Perl.
For one, a hash of arrays doesn’t sit well in Bash script. I wanted to define a list of servers for each class. In PHP the code would have been as simple as:
$servers = array( 'web' => array('server1', 'server2', 'server3'), 'db' => array('server4', 'server5', 'server6') );
Fortunately I was able to work around this with separate arrays for each class. What I couldn’t get around was the pain I had to endure to pass an array as an argument to a function.
Passing an array involves loading the space-separated elements of the array into a variable with command substitution.
Taken from Chapter 33 of the Advanced Bash-Scripting Guide.
printarray () { local passed_array passed_array=( `echo "$1"` ) echo "${passed_array[@]}" } original_array=( element1 element2 element3 element4 element5 ) argument=`echo ${original_array[@]}` # command substitution printarray "$argument"
This is just clunky and showed me that for anything more than basic conditional logic I’m better off investing some time in learning Perl.
Tweet
comments
No comments for this post.