Write shell scripts
Let’s write a simple shell script. This script will execute a single echo command:
To run the script, we need to make it executable. We will use the chmod command to do this:
Now we can run the script:
We can execute more than one command in a script. Consider the following script:
This script will display the current date after the text. Let’s run the script:
You can use variables in a script. For example, consider the following script:
The script above defines the variable name. The variable is then used in the echo command. This is the result:
You can also ask a user to enter a value for the variable. You do this using the read method, like in this example:
The code in the example above asks the user to enter his or her name. It then assigns the text that the user has entered to the variable name. The variable is then used in the echo statement. Here is the result: