

Next, execute the script and provide the first and second name as the arguments: # bash test.sh James Kiarieįrom the output, we can see that the first variable that is printed is the name of the shell script, in this case, test.sh. The $1 parameter takes the first variable that is passed on the terminal, $2 takes the second, $3 the third and so on. When executing the script, the first positional parameter which is $0 takes the name of the shell script. For loop in Shell ScriptĪ better way to achieve this is to define a range using the double curly braces … and so on. The for loop below iterates through 1 right through 10 and processes their values on the screen.
How to make a new file in linux code#
repeat code execution as many times as possible defined by the user. Like the while loop, a for loop is used to execute code iteratively. The line echo $counter prints all the numbers from 1 to 10. And while the variable is less than or equal to 10, the value of the counter will be incremented until the condition is satisfied. The variable counter is initialized to 1. The while loop below lists all the numbers from 1 to 10 when executed. This is one of the easiest loops to work with. In this section, we shall have a peek at some of the loops which you’d also find in other programming languages. This comes in handy in performing repetitive tasks.
How to make a new file in linux series#
If statement with OR logic Use Looping Constructsīash loops allow users to perform a series of tasks until a certain result is achieved. When using the OR logic, that is represented by || symbol, either one of the conditions needs to be satisfied with the script to give the expected results. The & operator is used to denote the AND logic. You can use the if statement alongside the AND logic operator to execute a task if two conditions are satisfied. if condition1įor example, we have a script for a lottery that checks if the number entered is either 90, 60 or 30. This statement takes the following format. In scenarios where there are multiple conditions and different outcomes, the if-elif-else statement is used. If-else statement in Shell Script Example of an if-elif-else Statement However, if the score falls below 70, the output ‘ You failed’ will be printed. If the score is greater than or equal to 70, you get a ‘ Great job, You passed!’ message. The script below reads the input score and checks whether it is greater than or equal to 70. If Statement in Shell Script Example of an if-else Statementįor situations where you have 2 possible outcomes: – whether this or that – the if-else statement comes in handy. 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.

