Ask HN: What is the coolest thing you have seen done with a bash script?

36 points by quijoteuniv ↗ HN
Love to hear some examples to get some inspiration.

25 comments

[ 4.8 ms ] story [ 66.8 ms ] thread
Web server, made possible with /dev/tcp. Plenty of examples all over GH.
The first time I encountered a "shar"[0] I was amazed. It was very early in my career when I first encountered one and I had only seen shell scripts, archives, and packages, it was an interesting mash up.

https://en.wikipedia.org/wiki/Shar

This is a color thing I've done with a bash script / bash prompt. If there is a better way to do this that would be the coolest.

  ### long prompt (in color) only when 'user or hostname or path' changes, or after certain commands

  prompt_command () 
  { 
      local -a a;
      local last_cmd;
      IFS=' ' read -r -d '' -a a < <( history 1 );
      last_cmd="$(printf "%s" "${a[1]}"|xargs -0)";
      if [ "$USER_HOSTNAME_PWD" != "$USER@$HOSTNAME:$PWD" ] || [[ " cd src doc ssh sudo su login $(sed -e '/^#.*$/d' -e '/^$/d' -e 's@^.*/@@' /etc/shells|uniq|xargs) " == *" ${last_cmd} "* ]]; then
          USER_HOSTNAME_PWD="$USER@$HOSTNAME:$PWD";
          echo -e "\\e7\\e(B\\e[m- \\e[3$((($(id -u) != 0) + 1))m${USER}\\e(B\\e[m@\\e[35m${HOSTNAME%%.*}\\e(B\\e[m:\\e[36m${PWD/#$HOME/\~}\\e8";
      fi
  }

  -() { unset USER_HOSTNAME_PWD
  }
  PROMPT_COMMAND=prompt_command
  PS1='$ ';[ "$(id -u)" -eq 0 ] && PS1='# '
i once wrote a loadbalancer in bash during a prod outage that took about 3 months to remove ;)
I want to know more, please.
3-level only OO-like "virtual" funcs: for same name, aliases have priority over functions have priority over commands-from-path
In CS101 our final was to build a sudoku solver in C++. I think the best student time was a minute.

At the end of the semester I went to the professor’s office to ask about optimizations that could’ve been made and he showed me a bash one-liner that solved a sudoku in under a second. I wish I remembered what the one liner was!

./asm_sodku_solver.exe
The coolest thing I've seen done with a bash script was to see it replaced with a python script.
/shameless plug - In my defense I originally read the title as "..you have done.." vs "..you have seen done.."

Two of my OSS projects are Bash and I'm rather proud of them:

A pretty cool shell-centric template engine: * https://github.com/TekWizely/bash-tpl * Has full bats tests to confirm compatibility with Bash 3.2 onward * Makes heavy use of Bash regexes

A wrapper over update-alternatives that enables user-level configurations: * https://github.com/TekWizely/my-alternatives * Versions for Debian, SUSE, and RedHat * Supports per-shell level configurations as well

beautiful sort command:

du -sk ./* | sort -nr | awk 'BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x > 1024 ) { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x10)/10,pref[y],$2); } END { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total: %g%s\n",int(total10)/10,pref[y]); }'

It took me longer than I care to admit to figure out caddy + fastcgi but running a bash script remotely over SSL with basic auth is kinda sweet.
Sounds interesting, could you elaborate on the details? :)
Many years ago, as a relative newbie in the industry, working a 1099 gig for a big4 accounting firm, I automated a netbackup media server migration with a for loop. Ended up saving myself and the more senior contractor (who was doing this process manually) about a day's work. Best thing I ever programmed.