#TIL : Internal Variables in BASH

$PWD

Your current working directory, so you don’t have to use CWD=$(pwd)

$OLDPWD

Your previous working directory

Note : You can jump directly to it by the command cd -

$SECONDS

The number of seconds the script has been running

You can use it for profiling or limiting timeout

1
2
3
4
5
6
7
8
TIME_LIMIT=60
START=$SECONDS
while [ $(($SECONDS - START)) -le "$TIME_LIMIT" ]
do
## Your work here
done

echo "It takes $SECONDS seconds to get here !"

$RANDOM

Get random integer number, for getting random name or just roll a dice ;)

lucky boy

#TIL : BASH tracing commands

Thank Hiro Ishii for teaching me this

set -x will print all running commands in your bash script

So I dove in and look for all set options of BASH.

And this is what I got , http://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html

#TIL : BASH exiting on first error

Setting a flag set -e to bash script will let the script exit on first error occurs, so if you want to ignore a command just adding || true to suffix

1
2
3
4
set -e

errorCmd $1 || true
echo "Run here !"

And opposite of set -e is set +e, haha of course !

1
2
3
4
set +e

errorCmd $1
echo "Still run here !"

#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 : Using BSD find util to find and exec command on file and folder

Simple syntax of find

1
$ find [find_path] -type [file_type] -exec [command] {} \;

Add filename matching pattern to filter the result

1
$ find [find_path] -name "*.php" -type [file_type] -exec [command] {} \;

Where file_type is :

  • b : block special
  • c : character special
  • d : directory
  • f : regular file
  • l : symbolic link
  • p : FIFO
  • s : socket

Examples:

Fix common file and directory permissions

1
2
$ find . -type f -exec chmod 644 {} \;
$ find . -type d -exec chmod 755 {} \;

Check syntax all PHP files

1
$ find . -type f -name "*.php" -exec php -l {} \; | grep -v 'No syntax errors detected'

Removed all log files

1
$ find . -type f -name "*.log" -exec rm -f {} \;

WANT MORE ???

1
$ man find

#TIL : wget Output flag

-O means output

1
2
$ # output file will be index.html or based on header filename
$ wget -O www.abc.xyz
1
2
$ # output file will be filename.html
$ wget -O filename.html www.abc.xyz
1
2
3
$ # output to stdout
$ wget -O- www.abc.xyz
$ wget -O- https://gist.githubusercontent.com/khanhicetea/4fa9f5103cd7fbc2d2270abce05c9c2b/raw/helloworld.sh | bash

#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