{ads}

Using bash options to change the behavior of scripts



October 06, 2022 at 01:49AM

Bash provides a large number of options that can be used to control the behavior of bash scripts. This post examines some of the more useful ones and explains how to display which options are in use and which are not.

Exiting when an error occurs

If you want a bash script to exit soon as it encounters an error—any error at all—in your scripts, you can add the set -o errexit option. If your script contains a syntax error, tries to read a file that doesn’t exist, attempts to append to a file when you don’t have permission to do so, or misuses a command in some way, the script will end abruptly. Here is a simple example:

#!/bin/bash

set -o errexit

tail NoSuchFile
echo -n “Enter text to be appended> “
read txt
echo $txt >> NoSuchFile

Try to run this script, and you’ll see this:

To read this article in full, please click here



from Network World https://ift.tt/JuxTDdC

0 Response to "Using bash options to change the behavior of scripts"

Post a Comment

Article Top Ads

Central Ads Article 1

Middle Ads Article 2

Article Bottom Ads