Create a simple shell script that will read user message as a parameter or directly from standard input when used in the pipeline.

Initial notes

    1. There are two possible cases:
    1. The message is provided as a parameter
    2. The parameter is missing and the shell script is executed in the pipeline

    The first case is simple, but the second one is more interesting. The idea is to check whether standard input exists but is not associated with the terminal.

    Look at the following shell script for an example.

    if [ ! -t 0 ] && [ -p /dev/stdin ]; then
      input=$(cat /dev/stdin)
      echo "Message: ${input}"
      echo "Error: Input missing" >&2
      exit 1

    Shell script

    The complete shell script.

    # Read user message as parameter or from a standard input when used in pipeline
    # define usage function
    function usage {
      echo "Read message from standard input in pipeline or use (-m) parameter"
    # parse arguments
    while getopts "m:" option; do
      case $option in
    # special case if no arguments are passed
    if [ "$#" = "0" ];then
      if [ ! -t 0 ] && [ -p /dev/stdin ]; then
        message=$(cat /dev/stdin)
    if [ -z "$message" ]; then
      echo "Error: Message is not provided or empty"
      exit 1
    # display message or perform other action
    echo "Message:"
    echo "$message"


    Sample standalone usage.

    $ -m "Logs size at $(date +%d/%m/%Y) is $(du -hs /var/log/ | cut -f 1)"
    Logs size at 16/04/2017 is 4,5M

    Sample usage inside the pipeline.

    $ awk 'BEGIN{RS="\n\n"} END{print}' /var/log/apt/history.log |
    Start-Date: 2017-04-15  20:50:07
    Commandline: apt-get upgrade
    Requested-By: milosz (1000)
    Upgrade: libgles2-mesa:amd64 (11.2.0-1ubuntu2.2, 12.0.6-0ubuntu0.16.04.1), xserver-xorg-video-amdgpu:amd64 (1.1.0-1, 1.1.2-0ubuntu0.16.04.1), linux-libc-dev:amd64 (4.4.0-71.92, 4.4.0-72.93), libgles1-mesa:amd64 (11.2.0-1ubuntu2.2, 12.0.6-0ubuntu0.16.04.1), virtualbox-guest-utils:amd64 (5.0.32-dfsg-0ubuntu1.16.04.2, 5.0.36-dfsg-0ubuntu1.16.04.2), grub-common:amd64 (2.02~beta2-36ubuntu3.8, 2.02~beta2-36ubuntu3.9), libglapi-mesa:i386 (11.2.0-1ubuntu2.2, 12.0.6-0ubuntu0.16.04.1), libglapi-mesa:amd64 (11.2.0-1ubuntu2.2, 12.0.6-0ubuntu0.16.04.1), virtualbox-guest-dkms:amd64 (5.0.32-dfsg-0ubuntu1.16.04.2, 5.0.36-dfsg-0ubuntu1.16.04.2), dnsmasq-base:amd64 (2.75-1ubuntu0.16.04.1, 2.75-1ubuntu0.16.04.2), libxatracker2:amd64 (11.2.0-1ubuntu2.2, 12.0.6-0ubuntu0.16.04.1), grub2-common:amd64 (2.02~beta2-36ubuntu3.8, 2.02~beta2-36ubuntu3.9), libegl1-mesa-drivers:amd64 (11.2.0-1ubuntu2.2, 12.0.6-0ubuntu0.16.04.1), libegl1-mesa:amd64 (11.2.0-1ubuntu2.2, 12.0.6-0ubuntu0.16.04.1), vivaldi-stable:amd64 (1.8.770.50-1, 1.8.770.56-1), libgbm1:amd64 (11.2.0-1ubuntu2.2, 12.0.6-0ubuntu0.16.04.1), adobe-flashplugin:amd64 (1:20170314.1-0ubuntu0.16.04.1, 1:20170411.1-0ubuntu0.16.04.1), ntp:amd64 (1:4.2.8p4+dfsg-3ubuntu5.3, 1:4.2.8p4+dfsg-3ubuntu5.4), ntpdate:amd64 (1:4.2.8p4+dfsg-3ubuntu5.3, 1:4.2.8p4+dfsg-3ubuntu5.4), grub-efi-amd64-bin:amd64 (2.02~beta2-36ubuntu3.8, 2.02~beta2-36ubuntu3.9)
    End-Date: 2017-04-15  20:53:49