Handling Non-existent Commands
Your team lead mentions that TechCorp used to use a custom build tool called techbuild
. Let's check if it's still installed:
which techbuild
You might not see any output (Bash Shell), or you might see an error message like techbuild not found
(Zsh Shell).
This is because which
doesn't return anything if it can't find the command in your PATH. Don't be alarmed by the lack of output - it's the command's way of telling you that techbuild
isn't found in any of the directories in your PATH.
To make this behavior more explicit, we can use a simple shell script. We'll introduce two new concepts here:
- The
echo
command: This is used to display a line of text/string on the terminal.
- The
>
operator: This is used to redirect output to a file.
Let's create a script to check for techbuild
:
echo '#!/bin/bash
if which techbuild >/dev/null 2>&1; then
echo "techbuild is installed"
else
echo "techbuild is not installed"
fi' > ~/check_techbuild.sh
This command does a few things:
- It uses
echo
to write a shell script.
- The script content is enclosed in single quotes.
- The
>
operator redirects the output of echo
to a new file named check_techbuild.sh
in your home directory.
Now, let's make this script executable and run it:
chmod +x ~/check_techbuild.sh
~/check_techbuild.sh
This script will print "techbuild is not installed" if the command isn't found.