How to use a parameter or standard input inside the shell script

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

Initial notes

    There are two possible cases:
  1. Message is provided as parameter
  2. Parameter is missing and the shell script is executed in 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 terminal.

Look at the following shell script for an example.

#!/bin/bash

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

Shell script

The complete shell script.

#!/bin/bash
# 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
    "m")
      message=${OPTARG}
      ;;
    \?|:)
      usage
      exit
      ;;
    esac
done

# special case if no arguments are passed
if [ "$#" = "0" ];then
  if [ ! -t 0 ] && [ -p /dev/stdin ]; then
    message=$(cat /dev/stdin)
  fi
fi

if [ -z "$message" ]; then
  echo "Error: Message is not provided or empty"
  exit 1
fi

# display message or perform other action
echo "Message:"
echo "$message"

Examples

Sample standalone usage.

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

Sample usage inside pipeline.

$ awk 'BEGIN{RS="\n\n"} END{print}' /var/log/apt/history.log | message.sh
Message:
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
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu