#TIL : BASH return a value in function

Creating function is a good way to refactor your bash script. But BASH doesn’t support returning a value in true way, so it makes a bit of challenge to handle that.

You can use this trick

1
2
3
4
5
6
hello() {
echo "Hello $1"
}

hw=$(hello "KhanhIceTea")
echo $hw

But what if you want to echo log message in hello function, it will be merged to returned value.

1
2
3
4
5
6
7
8
hello() {
echo "System is weaking up and brush its teeth :))"
echo "Hello $1"
}

hw=$(hello "KhanhIceTea")
echo "This is returned value of hello function :"
echo $hw

This is a how to resolve it, forwarding log message to stderr instead of stdout by default

1
2
3
4
5
6
7
8
hello() {
echo "System is weaking up and brush its teeth :))" >&2
echo "Hello $1"
}

hw=$(hello "KhanhIceTea")
echo "This is returned value of hello function :"
echo $hw

Where there is a shell, there is a way !

#TIL : zcat : decompressing pipe tool

zcat is a tool that creates a pipe from gz file. It makes command cleaner and faster (maybe). You don’t have to decompress gz file before using next tool.

Examples :

Finding string in gzip text file

1
$ zcat secret.gz | grep '42'

Importing SQL backup file

1
2
$ mysqldump -u root -p db_name1 | gzip > db_name.sql.gz
$ zcat db_name.sql.gz | mysql -u root -p db_name_2

#TIL : Checking forced push conflicts on source code in auto testing

Using automated CI solution likes Travis, Jenkins, DroneCI, … is good solution to ensure quality of software and no breaks in deployment.

Sometimes, developers force push conflicts part to production branch of source code. If the CI tests only backend (python, ruby, php, go, ..) and forget about frontend code, then your application will be exploded !

So checking the conflicts code is required step before testing backend and deployment.

I used grep tool to checking conflicts code in current dir

Create a file name conflict_detector.sh in root dir of source code

1
2
3
#!/bin/bash

grep -rli --exclude=conflict_detector.sh --exclude-dir={.git,vendor,venv,node_modules} "<<<<<<< HEAD" .

Then mini tool print list of conflicted files. If exit code not equal 0 then testing will be failed !

Kick start Docker environment on ubuntu

What does script do :

  • Install latest Docker Engine
  • Install latest Docker Compose tool
  • AND last but not least, a congratuation message !

Here is the script :

My docker containers are building

Hm.. it’s exactly docker images are building instead


Ref:

Kick start a LEMP stack on Ubuntu 16.04

LEMP = Linux + EngineX(NginX) + MySQL + PHP

Too tired for doing same tasks over and over again. I made this script to help me (and you) on kicking start a new LEMP stack on production server. Hope it useful !

What does script do :

  • Disable IPv6 (it’s not ready for production at the moment)
  • Configure firewall (only accepts port 22, 80 and 443)
  • Disable password authentication to SSH service
  • Upgrade system
  • Install useful tools (git, screen, vim, curl and lego for LetsEncrypt ACME)
  • NginX
  • MySQL 5.7 (default root password is passwd, should be changed ASAP)
  • PHP 7.1 (with production .ini)
  • Composer (PHP package manager)
  • AND last but not least, a congratuation message !
  • Fully and updated script : check my repo here

Here is the script :

STOP DOING SAME THING BUT EXPECTING DIFFERENT RESULTS

#TIL : Bash shell shortcuts

  • Ctrl + e : jump cursor to EOL
  • Ctrl + a : jump cursor to BOL (beginning of line)
  • Ctrl + u : delete all from cursor to BOL
  • Ctrl + k : delete all from cursor to EOL
  • Ctrl + r : search history, press again for next search
  • Ctrl + l : clear shell screen
  • Ctrl + c : terminate the command (sometimes have to press twice)
  • Ctrl + z : suspend the command, back to shell. Run fg to resume the command

#TIL : Stats your top-10 frequently commands

Run this command, it will show top-10 frequently commands, explain shell

1
$ history | awk '{print $2}' | sort | uniq -c | sort -nr | head

Example result

1
2
3
4
5
6
7
8
9
10
2064 git
1284 ls
826 cd
700 ssh
602 clear
491 python
473 exit
341 vagrant
242 export
167 ping