Wednesday, August 21, 2013

Conditional command IF ELSE

Here is a little different example. This script does one of two commands based on the presence of an argument. It displays a sorted list of installed packages using dpkg (for systems that use that particular type of package management). If an argument is passed, it will only include packages that contain that search argument. Otherwise it will include them all.
#!/bin/bash
criteria="$1"
# If criteria is not specified, show all
if [[ $criteria = "" ]]; then
dpkg --get-selections | grep -v deinstall | sort | less
else
dpkg --get-selections | grep -v deinstall | grep -i "$criteria" | sort | less
fi