Difference between revisions of "Scripting"

From TheBestLinux.com
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
= '''Scripting - Tips, tricks, and other cool stuff!''' =
+
= '''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.
 
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.
  
<br /><br />
+
<br />
 
== '''BASH Shell Scripting''' ==
 
== '''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.
 
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.
  
<br /><br />
+
<br />
=== '''Here are some of the the commands I use to manage Linux servers 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.
 
*  How to make a script pause and wait for a user to press "ENTER" to continue execution of the script.
 
** Use the "read" command:
 
** Use the "read" command:
Line 18: Line 18:
 
read -n 1 -s -r -p "Press any key to continue"
 
read -n 1 -s -r -p "Press any key to continue"
 
</pre>
 
</pre>
 +
The purposes of the switches used in the above command:
 +
<pre style="color:blue">
 +
-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.
 +
</pre>
 +
 +
<br />
 +
 +
=== '''Modifying parts of a line of text''' ===
 +
*  See examples on this URL:  https://stackoverflow.com/questions/42219067/how-can-replace-a-specific-line-in-a-text-file-with-a-shell-script?newreg=54d4eb133de948cab223ccf63029dd38
 +
*  More here:  https://www.google.com/search?q=simple+linux+script+change+text+on+specific+lines&oq=simple+linux+script+change+text+on+specific+lines&gs_lcrp=EgZjaHJvbWUyBggAEEUYOdIBCTU4NDE4ajBqN6gCALACAA&sourceid=chrome&ie=UTF-8
 +
 +
 
<br /><br />
 
<br /><br />

Latest revision as of 22:35, 17 April 2024

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.


Modifying parts of a line of text