LINUX BASH SHELL SCRIPT BASICS Part 2:
STDIN, STDOUT, STDERR AND REDIRECTION
Redirection in Linux is when executing a command you can change the standard input/output devices. The basic workflow of any Linux command is that it takes an input and gives an output.
* The standard input (stdin) device is the keyboard.
* The standard output (stdout) device is the screen.
* The standard error (stderr) is for error messages.
With redirection, the above standard input/output can be changed.
You can use pipes to take the input from one program and send it to another:
ls -l really-big-directory | less
You can send output to files:
ls -l really-gig-directory > ls-output.txt
You can throw output away:
ls -l really-big-directory > /dev/null 2>&1
You can send output to STDERR:
echo “Script Error: This is an error message!” >&2
A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables. You can list shell variables with the env command.
Any input a user adds after a command is placed in special variables called arguments. Each argument is defined by a space between them. If we had a mythical command called twiddle, and we launched it with a command like this:
twiddle file1 file2 file3
What we typed would be put into these shell variables:
$0 = twiddle
$1 = file1
$2 = file2
$3 = file3
If you would type:
$0 = twiddle
$1 = –help
A special shell variable called $@ represents all of the arguments in numerical order. This can be used by a script to loop through several arguments that contain information like file names.
The test command looks for some string of data and returns an error in its exit code if it cannot find it. Using tests is how you can make a clever script that adapts to changes and catches errors.
test -f “.bashrc” && echo “.bashrc is here.” || echo “.bashrc is not here.”
dpkg -l | grep -qw ffmpeg || sudo apt-get install -yyq ffmpeg
You can use the if command to test for files and directories, the exit status of other commands and data you put into variables.
if [ test fir something ]; then
run commands if the answer is yes
run commands if the answer is no
Using if to test for errros:
The exit code of commands that run in bash are placed in a variable called $?. Usually, a 0 means everything went alright, any other number indicates an error.
which ifconfig >/dev/null
if [ “$?” != “0” ]; then
echo “SyncAll Error: The ifconfig utility is not installed!” >&2
echo “Ubuntu and Linux Mint: Run ‘sudo apt install net-tools'” >&2
The exit 1 command will exit the running script with an error code of 1.
ADVANCED SCRIPTING WITH FUNCTIONS
So far, we have been talking about scripts that run one command after another. There is another way to write scripts that makes them more like real computer programs. Functions are sets of commands that get loaded into memory when the script is started and can be called on at any time and in any order as long as the script is running. They are most often used with options entered by the user and/or menus.
A scrips with functions looks like this:
# This is an example script using functions.
# Declare Variables:
local VAR1=1 # Declared as local variable.
Return # Optional command exits a function but not script.
local VAR1=2 # Declared as local variable.
exit 1 # exits script with an error.
# Test for options.
# Coammnds to call functions.
exit # Optional coammnd to exit script with exit staus of 0.