Scripting

From TheBestLinux.com
Jump to navigation Jump to search

Tips, tricks, and other cool stuff!

Here are some things I've learned over the years to help me in creating scripts to save me time so I don't have to keep reinventing the wheel, and automate those tasks that are boring and mundane. This will be an on-going section, where I will add more information as I have time, or as I encounter the need to create new scripts.


BASH Shell Scripting

The BASH(Bourne Again SHell) is the default shell on most Linux distributions, as it has all the features, bell and whisteles, and then some! It is very simple to use and learn, and can be used both in both what are known as "one-liners", those strings of commands typed from the command line to get immediate results, and complete scripts containg any number of shell commands, strung together to form a script that can be run/executed from the command line.


Here are some of the the commands I use to manage Linux servers within shell scripts

  • How to make a script pause and wait for a user to press "ENTER" to continue execution of the script.
    • Use the "read" command:
read -p "Press the ENTER key to Continue"



Similare to the above command, but allow the user to press any key, not just the ENTER key, to continue script execution:

read -n 1 -s -r -p "Press any key to continue"



The purposes of the switches used in the above command:

-n - Defines the required character count to stop reading.

-s - Hides the user's input.

-r - Makes the string to be interpreted "raw" (without considering backslash escapes).

-p - Forces script pause for user input.