Bash if Function Breaks Run Again
Y'all may have encountered many situations when you accept to quit your bash script upon some inconvenience. There are many methods to quit the fustigate script, i.e., quit while writing a bash script, while execution, or at run time. One of the many known methods to exit a fustigate script while writing is the simple shortcut central, i.e., "Ctrl+10". While at run time, you can leave the lawmaking using "Ctrl+Z". This guide will show how the batch script can be quitted using the different exit clauses while executing. Let'southward get started by logging in from the Ubuntu 20.04 system kickoff and opening the shell using "Ctrl+Alt+T".
Case 01: Using Exit 0
The first method we have been utilizing in this example is to use the "exit" statement in the bash script. Create a new file in the shell with the help of a "touch" command and open up it in whatsoever editor.
The read statement is widely known to go input from the user. Here information technology volition have integer values at run time and save them to the variable "10". The "if" statement has been checking a condition. If the value of "x" entered past a user is equaled to 5, it volition display that the number is matched via the echo statement. The "go out 0" clause has been used hither. After executing the "repeat" argument, the bash script will exist quitted, and no more execution volition be performed due to "leave 0". Otherwise, if the condition doesn't satisfy, the "repeat" statement outside of the "if" argument will be executed.
Run your bash file with the help of a bash query in the shell. The user added 4 every bit input. Every bit 4 is not equal to 5, it doesn't run the "then" part of the "if" statement. So, no sudden exit volition happen. On the other hand, the echo argument outside of the "if" argument executed states that "Number doesn't match.." and the program ends here.
Run the same code once again with the bash command. The user added 5 this time. As 5 satisfies the condition, the "echo" statement inside the "then" clause was executed. Afterwards that, the program stops quickly due to the use of "exit 0".
Example 02: Using Exit
Instead of using "exit 0", you can simply use "go out" in your bash script to exit the code. So, open the same file and update your lawmaking. Only the "leave" clause has been changed hither, i.eastward., replaced by "exit". The whole file remained unchanged. Let'due south save the code first using the "Ctrl+South" and quit using "Crl+Ten". Let'south execute information technology to see if it works the same as the "go out ane" clause does or not.
Run the fustigate file "bash.sh" in the final past utilizing the command shown in the attached screenshot. The user entered the value "half dozen" and information technology didn't satisfy the condition. Therefore, the compiler ignores the "then" clause of the "if" statement and executes the echo clause outside of the "if" statement.
Run the aforementioned file once again. This time the user added 5 as satisfying the status. Thus the fustigate script exits right after executing the "echo" clause within the "if" argument.
Example 03: Using Exit i
Y'all can also utilize the "exit" clause to leave the fustigate script while stating 1 with information technology at run time. So, open up the same file and update your code as nosotros have done before. The only modify is "exit one" instead of "exit" or "exit 0". Save your code and quit the editor via "Ctrl+S" and "Ctrl+Ten".
At offset execution, the user added 6 as input. The condition doesn't satisfy and commands within the "if" statement won't be executed. So, no sudden exit happened.
On the second attempt, the user added five to satisfy the condition. So, the commands within the "if" statement become executed, and the program exits after running the "echo" clause.
Example 04
Allow'due south make use of the "exit 1" clause in the fustigate script upon checking different situations. So, we take updated the code of the same file. Afterward the fustigate back up, the "if" statement has been initialized to check if the currently logged-in user, i.due east., "Linux" is not the root user. If the status satisfies, the echo statement within the "then" clause will be executed, and the program volition leave right here. If the currently logged-in account is a root user, it will continue to execute the statements exterior of the "if" statement. The plan will go on to get ii inputs from a user and compute the sum of both integers. The calculated "sum" will be displayed, and then the plan will exit.
As the "Linux" account is not a root user of our Ubuntu 20.04, the execution of this code has only executed the "if" statement and clauses betwixt it. The plan quits after this.
Example 05: Using "gear up -e" Built-in
The "set up –e" congenital-in is widely known to exit the plan upon encountering the non-zero status. And so, we have added 3 twin-named functions with 1 repeat argument and a render status clause in each. The "set +e" is initialized before calling the kickoff two methods, and "set –e" is used after that, and 2 functions are called after that.
Upon execution, both show1 and show2 function's repeat statements will run, and the program will not quit. While after "prepare –e" the program quits after the execution of the show2() method's echo statement as it encounters "return 1". The method show3 volition not exist called after that.
Upon running this code, we got the output as expected. Upon encountering the return one status, the program stopped without executing the "show3()" method.
Decision
This guide covers all the possible ways to leave any bash script while writing, executing, or running. Thus, try to implement each example covered in this article to get a more than clear understanding.
Source: https://linuxhint.com/exit-bash-script/
Postar um comentário for "Bash if Function Breaks Run Again"