Other comparison operators you can use include:įor example, the if-statement block below prints out ‘ Work Harder’ if the input score is any value less than 50. The comparison operator = is used to test if the score entered, which is stored in the variable x, is equivalent to 100. If the score corresponds to 70, the script returns the output “ Good job!”. The above shell script prompts the user to provide a score that is then stored in a variable x. Let’s take a look at the shell script below. We will start off with the fundamental use of the if statement to test a single condition. The if statement can be used to test single or multiple conditions. We are going to cover the if, if-else, and elif conditional statements. Like other programming languages, conditional statements are used in bash scripting to make decisions, with only a slight variation in the syntax. Using Conditional Statements to Execute Code $ chmod +x hello.shįinally, run the shell script using either of the commands: $ bash hello.shĬreate Hello World Shell Script 2. The next step is to make the script executable by assigning execute permission using the chmod command as shown. The last line is the command that prints the ‘ Hello World’ message on the terminal.Comments are always preceded by the hash sign #. A comment is a statement that describes what a shell script does and is not executed when the script is run. There are other scripting languages such as Python which is denoted by #!/usr/bin/python3 and Perl whose shebang header is is denoted by #!/usr/bin/perl. In this case, this will be the bash shell indicated by /bin/bash. This is a special construct that indicates what program will be used to interpret the script. The first line – #!/bin/bash – is known as the shebang header.Let’s go over the shell script line by line.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |