
- Image via Wikipedia
A slightly modified post i wrote long time ago (when i was trying to add a feature to the code bellow)
i will not abuse bash
i will not abuse bash
i will not abuse bash
i will not abuse bash
i will not abuse bash
i will not abuse bash
i will not abuse bash
i will not abuse bash
i will not abuse bash
Why not? Find out by yourself trying to add a feature to one of my scripts
shopt -s extglob shopt -s nullglob for i in */; do [[ -f $i/photos.dat ]] && { IFS=$'\a' read title desc < <( tr -d '\r' < $i/album.dat | sed ':b;$!N; $!bb; $s/\n/\\\\\\\\n/g' | ssed -nR 's/.*?s:5:"title".*?"(.*?)".*?s:11:"description".*?"(.*?)";.*/\1'$'\a'\\2/p) title=${title//\'/\\\\\\\'} albums=`sed -rn 's/s:11:"isAlbumName";s:8:"([^"]*)"/\n\t\1\n/gp' "$i"/photos.dat | sed '/\t/!d'` printf "try:\n gallery = Gallery.objects.get(title='$title')\nexcept Gallery.DoesNotExist:\n gallery = Gallery.objects.create(title='$title', title_slug=slugify('$title'), description='$desc')\n" for album in $albums; do IFS=$'\a' read subtitle subdesc < <( tr -d '\r' < $album/album.dat | sed ':b;$!N; $!bb; $s/\n/\\\\n/g' | ssed -nR 's/.*?s:5:"title".*?"(.*?)".*?s:11:"description".*?"(.*?)";.*/\1'$'\a'\\2/p) subdesc=`echo -n "$subdesc" | sed ':b; $!N; $!bb; $s/\\n/\\\\\\\\n/g'` subtitle=${subtitle//\'/\\\\\\\'} printf "try:\n child = Gallery.objects.get(title='$subtitle')\n child.parent = gallery\n child.save()\nexcept Gallery.DoesNotExist:\n child = Gallery.objects.create(title='$subtitle', title_slug=slugify('$subtitle'), parent=gallery, description='%s')\n" "$subdesc" done for j in $i/!(*.thumb|*.sized|*.highlight).@(jpg|jpeg|png|gif); do filename=$j dirless=${j##*/} title=${dirless%.*} c=`tr -d '\n' < "$i"/photos.dat | ssed -nR 's/.*?"'"$title"'".*?caption";s:[0-9]+:"(.*?)".*/\1\n/p'` caption="${c//\'/\\\'}" if [[ $caption != $title && $caption ]]; then cap=", caption='$caption'"; fi printf "p=Photo.objects.create(image='$filename', title=title_wraper('$title', '${i%/}'), title_slug=photo_slug_wraper('$title', '$i')$cap)\np.save()\ngallery.photos.add(p)\n" done }; doneThis was my attempt to write a migration script from gallery v1 to photologue. Attempt failed, and i learned not to abuse bash for any serious code generation. I hope.
The history of this script went something like:
- hrm, i’m supposed to parse serialized php, i bet i can do it with sed, how hard can it be?
- a “perfect” solution (probably certain line in that code now) was born
- hrm, i have new problem, another “perfect” solution was made
- possibly repeat the above step few more times
- hrm, this doesn’t work, let’s add some ugly solutions/gluing, just for test
- possibly repeat the above step few more times
- it WORKS!!, sleep time
Don’t get me wrong, shell is a great tool, just remember to keep it in your pants cowboy
. It allows you to do simple tasks with a speed (and character count
) python can’t possibly rival, but if you do too complex problems with bash you can easily slip in the land of unmaintainable.
As for the python, i agree pretty much with Tomaz, python is boring, you miss (almost) all the fun of look for a “perfect” one liner, the trade off for this is that the code is syntactically maintainable and not having to look up the syntax often.
Fast forward to recent times and you see that i use shell just to generate simple python array (well, probably soon to be two arrays), but that is harmless and can’t possibly turn into an un-maintainable monster, right?
| ReTweet |
Tags: Bash, Languages, Programming, Shell, Unix
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=77b560ee-f23d-48fa-ad5c-6597e026415d)