Exit file command in linux

Exit Command in Linux

In Linux, the exit command is used to terminate the shell (the current login session) in which it is run. Also, similar to a C program, the exit command is used in shell scripts to terminate the script. Moreover, it returns a value to the script’s parent process.

What Will We Cover?

In this guide, we will see an overview of the exit command in Linux with some use cases.

The Exit Command and Exit Status

You can also use the exit command to specify the exit status of the shell. The exit status is a numeric value that is returned to the parent process when the shell terminates.

The exit status can be used to indicate the success or failure of the command. For example, a shell script might use the exit status to determine whether to perform certain actions based on the success or failure of a command.

To specify the exit status, you can use the exit command followed by a number. For example, exit 0 indicates a successful exit, while exit 1 indicates a failure.

The exit command accepts a single, optional argument which becomes the script’s exit status.

When no argument is passed, the exit status defaults to the exit status of the last command that is executed. In order to see the exit status, use the echo “$?” command.

Examples of Exit Codes (Exit Status)

The exit command is crucial to indicate that a severe error has occurred since the calling processes typically consider the exit codes other than zero as failure cases. If you need a general error trap in your scripts, try using the exit code snippet.

Let’s see some examples of using the exit code in a script and on a terminal:

Example 1: Using the Exit Command with File Expression

Let’s first take an example of a script:

#!/bin/bash
# test-file: Evaluate the status of a file
FILE = / proc / uptime
if [ -e » $FILE » ] ; then # Check the existence of file.
if [ -f » $FILE » ] ; then # Check if it is a regular file.
echo » $FILE is a regular file.»
fi
if [ -b » $FILE » ] ; then # Checks if it is a block file
echo » $FILE is a block device»
fi
else
echo » $FILE is neither a regular nor a block file or may not exist»
exit 1
fi
exit

The script performs an evaluation on the file that is allocated to the constant “FILE” and outputs the results as it runs.

First, it checks if it is a regular file or a block file. If the condition is not satisfied, it prints the message in the else block.

The first exit command sets the exit status of the script in case of failure (exit value as 1). The second exit command is a formal expression. This means that when the script completes its execution to the end, it has the exit status of the last command which, in this case, is the exit command. This last exit command will result in success (exit value as 0):

Читайте также:  Подключить интернет linux windows

Example 2: Using True and False Commands with the Exit Command

Additionally, entering a simple true or false argument causes the terminal to quit with 0 status code and shows 0 as true. If we instead provide a false statement, the exit status will be 1 since false exits with code 1. Let’s see this in a practical manner. First, issue the true command and check the exit status:

Now, issue the false command and again check the exit status:

Example 3: Interrupting a Script with Ctrl+C

When a user hits Control-C, a special signal known as SIGINT is sent. This interrupts the script or running program, causing it to exit with the code 130. Let’s take an example: suppose you started searching a file with the find command:

The search is still going on. Then, you suddenly interrupted it with “Ctrl+C”. This causes the command to stop with the exit code 130.

Example 4: The Shell Variable SHLVL and the Exit Command

With the shell-specific variable, SHLVL, you can see how many levels deep in the Bash shell you are. Each new shell session that is opened in a terminal increases the session count, starting at 1 for the initial session. If SHLVL is > 1, the exit command closes the current session. In the same way, if the SHLVL is = 1, the exit command logs you out of the system.

To simply put it, when you type “exit” at the command prompt and press Enter, the shell closes (close the current login session) and you will be returned to the shell or command prompt from which you launched it.

Conclusion

We have seen the different use cases of the exit command and the interpretation of the exit status in each case. There are many different exit code numbers for different purposes. Note that these exit codes are Bash specific. If you tend to use other shells such as C-shell or tcsh, the results may differ.

About the author

Ali Imran Nagori

Ali imran is a technical writer and Linux enthusiast who loves to write about Linux system administration and related technologies. You can connect with him on LinkedIn
.

Источник

How to Exit a File in Vi / Vim Editor in Linux

In this article, we will learn how to exit Vi/Vim (after referred to as Vim) text editor using simple commands. In a previous article, we explained a simple tip on how to save a file in Vi or Vim after making changes to a file.

Before we move any further, if you are new to Vim, then we recommend reading through these 10 reasons why you should stick to using Vi/Vim text editor in Linux.

To open or create a new file using Vi/Vim, simply type the commands below, then press i to switch to insert mode (insert text):

$ vim file.txt OR $ vi file.txt

Press

After making changes to a file, press [Esc] to shift to the command mode and press :w and hit [Enter] to save a file.

Save File in Vim

To exit Vi/Vim, use the :q command and hit [Enter] .

Exit File in Vi Editor

To save a file and exit Vi/Vim simultaneously, use the :wq command and hit [Enter] or 😡 command.

Читайте также:  Change python path in linux

Save and Exit File in Vi

If you make changes to a file but try to quite Vi/Vim using ESC and q key, you’ll receive an error as shown in the scrrenshot below.

Vi Error Quitting File

To force this action, use ESC and :q! .

Force Quit Vi File in Linux

Additionally, you can use shortcut methods. Press the [Esc] key and type Shift + Z Z to save and exit or type Shift+ Z Q to exit without saving the changes made to the file.

Having learned the above commands, you can now proceed to learn advanced Vim commands from the links provided below:

In this article, we learned how to exit Vim text editor using simple commands. Do you have any questions to ask or any thoughts to share? Please, use the feedback form below.

Источник

How do I Exit a Bash Script?

You may have encountered many situations when you have to quit your bash script upon some inconvenience. There are many methods to quit the bash script, i.e., quit while writing a bash script, while execution, or at run time. One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”. This guide will show how the batch script can be quitted using the different exit clauses while executing. Let’s get started by logging in from the Ubuntu 20.04 system first and opening the shell using “Ctrl+Alt+T”.

Example 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 it in any editor.

The read statement is widely known to get input from the user. Here it will take integer values at run time and save them to the variable “x”. The “if” statement has been checking a condition. If the value of “x” entered by a user is equaled to 5, it will display that the number is matched via the echo statement. The “exit 0” clause has been used here. After executing the “echo” statement, the bash script will be quitted, and no more execution will be performed due to “exit 0”. Otherwise, if the condition doesn’t satisfy, the “echo” statement outside of the “if” statement will be executed.

Run your bash file with the help of a bash query in the shell. The user added 4 as input. As 4 is not equal to 5, it doesn’t run the “then” part of the “if” statement. So, no sudden exit will happen. On the other hand, the echo statement outside of the “if” statement 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. After 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 “exit” in your bash script to exit the code. So, open the same file and update your code. Only the “exit” clause has been changed here, i.e., replaced by “exit”. The whole file remained unchanged. Let’s save the code first using the “Ctrl+S” and quit using “Crl+X”. Let’s execute it to see if it works the same as the “exit 1” clause does or not.

Читайте также:  Загрузка linux при systemd

Run the bash file “bash.sh” in the terminal by utilizing the command shown in the attached screenshot. The user entered the value “6” and it 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 same file once again. This time the user added 5 as satisfying the condition. Thus the bash script exits right after executing the “echo” clause inside the “if” statement.

Example 03: Using Exit 1

You can also use the “exit” clause to exit the bash script while stating 1 with it at run time. So, open the same file and update your code as we have done before. The only change is “exit 1” instead of “exit” or “exit 0”. Save your code and quit the editor via “Ctrl+S” and “Ctrl+X”.

At first 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 5 to satisfy the condition. So, the commands within the “if” statement get executed, and the program exits after running the “echo” clause.

Example 04

Let’s make use of the “exit 1” clause in the bash script upon checking different situations. So, we have updated the code of the same file. After the bash support, the “if” statement has been initialized to check if the currently logged-in user, i.e., “Linux” is not the root user. If the condition satisfies, the echo statement within the “then” clause will be executed, and the program will exit right here. If the currently logged-in account is a root user, it will continue to execute the statements outside of the “if” statement. The program will continue to get two inputs from a user and compute the sum of both integers. The calculated “sum” will be displayed, and then the program 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 between it. The program quits after this.

Example 05: Using “set -e” Built-in

The “set –e” built-in is widely known to exit the program upon encountering the non-zero status. So, we have added 3 twin-named functions with 1 echo statement and a return status clause in each. The “set +e” is initialized before calling the first two methods, and “set –e” is used after that, and two functions are called after that.

Upon execution, both show1 and show2 function’s echo statements will run, and the program will not quit. While after “set –e” the program quits after the execution of the show2() method’s echo statement as it encounters “return 1”. The method show3 will not be called after that.

Upon running this code, we got the output as expected. Upon encountering the return 1 status, the program stopped without executing the “show3()” method.

Conclusion

This guide covers all the possible ways to exit any bash script while writing, executing, or running. Thus, try to implement each example covered in this article to get a more clear understanding.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.

Источник

Оцените статью
Adblock
detector