The Complete Steps to Create and Run a Linux Script to Automatically Run One or More Linux Commands
Linux Commands Training Tips: To demonstrate the benefits of using Linux commands and reasons NOT to use Linux GUI utilities, here's a "sneak preview" of how to create and run a Linux script.
Creating Linux scripts, creating Linux text files and using the Linux scripting programming language is described in much more detail in an upcoming section.
You can easily automate the process of running several Linux commands by creating and "running" a Linux script.
This allows you to take a series of routinely run ("executed") Linux commands, put them in a file and then
run all of them by simply typing in the name of the file and pressing Enter.
The Complete Steps to Create and Run a Linux Script for Linux System Administration
- Including a Linux Scripting Example of Creating and Running a Linux Script
Here are the steps to create and run a Linux script, just:
1. Run a Linux text editor
There are lots of Linux text editors that are included in a Linux distribution and all of these can be used to create a Linux text / script file.
2. Create a text file (by giving it a name)
Do the steps to save your Linux text file (even if it's empty) so that you can give the file a name. The name of the Linux text file is the name of the Linux script file. To run the Linux commands in the script, just type in the name of the script and press Enter!
For example, if you want to list all the files in the current directory, call the file: listall
3. Put a bit of text at the top of the Linux commands you need to run in the text file.
This text indicates (to the Linux shell, a.k.a. bash shell) that the file is a script file. You will learn much more on the Linux shell, bash shell and "shell scripting" later.
Put the following text at the top left of the text file: #!/bin/bash
This makes the Linux text file a Linux script file that can be run like a Linux command.
Put a blank line below the above (first) line in the Linux script (Linux text) file.
4. Put the Linux command(s) and Linux programming logic in the Linux script (Linux text) file.
Type in any Linux command or series of commands, and use Linux (bash shell) programming logic if needed.
Here's an example of a Linux command to put in the file named listall (the Linux script file):
ls -la / | less
The Linux command above ls -la / | less displays all the files in the / (root) directory and "pipes" the output of the command (by using the | "pipe" symbol) to the less command - so that the output of the command stops after each "screenful" of output, so you can read it. You will learn more about the less command later :).
The (single) Linux command in the Linux script above could be much more complex than this - and the Linux script could contain hundreds of lines of text if necessary - with complex bash shell scripting included in the Linux script file.
5. Run the chmod command and make the Linux text file "executable".
In our example, the file is named: listall
The $ (dollar sign) below is the Linux command line prompt. It appears below to indicate that the text at the right of the prompt is a Linux command.
Below is a Linux command example for running the chmod command to change the permissions of the Linux script file - and to make the listall text / script file "executable", so that you can run the Linux script file in the same way as you run a Linux command. (Don't type in the $ (dolar sign) prompt, type in the Linux command that appears at the right of the prompt).
$ chmod u+x listall
The Linux command above is chmod. The chmod command is being used to assign the x (executable) permission to the u (user) of the file with: u+x and the Linux script file name is listall.
To run a Linux script (that is in the "current" directory), such as the listall script, simply type in a period (dot) and a space and then the name of the file and press Enter.
$ . listall
If there is more than one "screenful" in the output (more than a "screenful" of directories and files in the / (root) directoryof the hard disk), press the Spacebar to see the next screenful.
The listall script above runs the ls command with the less command, so after this Linux script has run, the less command is still running. Press the letter q to quite out of the less command.
Linux Commands Training Tips: To be able to run a Linux script from any directory on the system, put the Linux script file in a directory that is in the Linux PATH for all users, such as /bin.
To run the command after it is in a directory (that is in the PATH), just type in the name of the Linux script file and press Enter (you don't need a period and then a space in front of the Linux script file name.
Much more on the PATH and Linux concepts related to the PATH later.
Learn Linux Commands Now with Linux Commands Videos - Click here to Order
Click Here to See Free Sample Linux Commands Training Videos
Click Here to Watch General Linux Training Videos - Featuring Ubuntu Linux
Get Updates to this Linux Commands Book Blog. FREE! |
| Privacy Notice: We will not distribute your email address to anyone. Period! |
What do you need to learn about Linux system administration and using the Linux command line interface (a.k.a. Linux CLI) to run Linux commands (with lots of Linux command options)?
Do you need to learn Linux Commands so you can do Linux command scripting to create Linux scripts.
Or do you need to know how to edit Linux system configuration text files with Linux text editors?
To get more info on Linux commands - and on what YOU need to know, click "Comment" (link further below) and make a Linux commands book suggestion, ask Linux Commands training questions, or post a comment to let us know what you think so far.
Thanks for visiting this "Linux Commands Book Blog"!
Clyde Boom, http://www.iLearnLinux.com





Leave a Comment