Generic shell scripting tricks

Some might be POSIX/portable, some not. I am too old for that crap.

<< Back to wiki homepage

Table of contents:

Get last argument given

echo "${@: -1}"


Watch files created in a folder

inotifywait -m ${YOUR_FOLDER} -e create -e moved_to

And if you'd like to process them e.g. cat or copy etc.

inotifywait -m ${YOUR_FOLDER} -e create -e moved_to |  while read directory action file; do cat ${YOUR_FOLDER}/$file ; done


Check if stdin is empty or not

if [ ! -t 0 ]; then
    # there is stuff in stdin

Catch and use stdin

2 ways:

while IFS= read -r line; do
    printf '%s\n' "$line" >> /whatever/you/want/to/do

or let cat collect it for you:

stuff="`printf '%s\n' "$data"`"