Batch Script - Return code
Last Updated : 27 Jan, 2022
Return codes are the codes returned by programs when they are executed. If the command line is successful, it should return zero; if it is not successful, it should return non-zero. If the test fails, a non-zero value indicates the error number, and the user can attempt to resolve it by navigating to the error message.
The test may also return an exit code. A program's or utility's exit code usually appears when it finishes or terminates.
The list below includes some of the non-zero exit codes (with their respective errors) that programs may return
Error Code | Description |
---|
0 | Successful completion of the program. |
1 | This error indicates that the Windows command prompt has attempted to execute an unrecognized action |
2 | An error indicating that the file could not be found in the specified location |
3 | An error message indicated that the specified path could not be found. |
5 | An indication that the user is not authorized to access the resource |
90090x2331 | This error occurs when you misspell the command, application name, or path when configuring an Action. |
2212254950xC0000017-1073741801 | The error message tells you that Windows has run out of memory. |
32212257860xC000013A-1073741510 | This indicates that the user terminated the application |
32212257940xC0000142-1073741502 | The message indicating that the application was launched on a desktop to which the current user doesn't have access |
Batch file error level:
%ERRORLEVEL% is an environment variable that contains the last error level or return code in the batch file - that is, the last error code of the last command executed. Error levels may be checked by using the %ERRORLEVEL% variable as follows:
IF %ERRORLEVEL% NEQ 0 ( DO_Something )
A common method of returning error codes from batch files is to use the command EXIT /B %ERRORLEVEL%.
For custom return codes, use the EXIT /B <exitcodes> command.
Example:
In the below example, if the condition is met, the script will terminate with the exit code 0. If the condition isn’t met, then the exit code will be 1.
if [[ "$(whoami)" != root ]]; then echo "Not root user." exit 1 fi echo "root user" exit 0
Output:
OutputLoops:
There have been statements enacted sequentially in the decision-making chapter. Alternatively, Batch Script can also be used to alter the flow of control in a program's logic. These statements are then organized into flow control statements.
Serial No | Loops | Description |
---|
1 | While Statement Implementation | There is no direct while statement in Batch Script, although labels and an if statement can be used to implement this loop. |
2 | For Statement - List Implementations | Batch files can loop using the "FOR" construct. In order to work with a list of values, the 'for' statement requires the following construct. |
3 | Looping through Ranges | 'For' statements can also move through ranges of values. A general version is presented below. |
4 | Classic for Loop Implementation | It has the classic 'for' statement found in many programming languages. |
5 | Break Statement Implementation | Within any programming language, the break statement is used to alter the flow of control inside a loop. As part of looping constructs, the break statement causes the innermost enclosing loop to terminate immediately |
Looping through Command Line Arguments
For checking command-line arguments, you can use the for statement. Here is an example of how to loop through the arguments of a command line using the 'for' statement.
for ((c=1; c<=7; c++)) do echo "Welcome $c times" done
Output:
Output Similar Reads
Batch Script - Debugging
Batch Script is a text file that includes a definite amount of operations or commands that are performed in order. It is used in system networks as well as in system administration. It also eliminates a specific repetitive piece of work in various Operating Systems like DOS(Disk Operating System). D
3 min read
Batch Script - Device Console
A batch script is a text file that includes various sequential commands. It is used to evaluate repetition routines in Windows, Â OS/2, and DOS operating systems and is used in network and system administration. A batch file refers to a file that stores command in a serial order. Â The library has bee
3 min read
Batch Script - Remove
Bash is a command language interpreter. It is used in most of the UNIX/LINUX-based distros. Bash support scripting means we can automate the Bash commands by writing some scripts with different logic, looping, and decision making. This benefits us to automate some time-consuming and tedious jobs. He
3 min read
Batch Script - Printing / NETPrint Command
A Bash script is similar to a simple text file that contains a number of commands that the programmer can write in a command line. In Unix-based systems, these commands are used for performing repetitive tasks. A Bash script consists of a bunch of commands, or it may contain elements like loops, fun
3 min read
Batch Script - Logging
Batch Script is a file that consists of various commands which need to be sequentially executed. It is not like coding and is not frequently used, in order to communicate with the OS through a command prompt, the user can use Batch Script. The commands are known as Batch commands. Logging refers to
2 min read
Batch Script - Create String
In this article, we are going to learn how to create a String using Batch Script. Batch Script :@echo off set str=Geeks for Geeks echo %str% pauseBy using ' set ' we are getting input of any string.Ex: set str=input stringIn the next line, we are using ' echo %str% ' for printing our input string.Th
1 min read
Batch Script - Remove Both Ends
In this article, we are going to learn how to remove characters from both ends of any given string using Batch Script. Code :@echo off set str=Geeks for Geeks echo %str% set str=%str:~1,-1% echo %str% pauseBy using ' set ' we are getting input of any stringset str=input stringIn the next line using
1 min read
How To Embed Python Code In Batch Script
Embedding Python code in a batch script can be very helpful for automating tasks that require both shell commands and Python scripting. In this article, we will discuss how to embed Python code within a batch script. What are Batch Scripts and Python Code?Batch scripts are text files containing a se
4 min read
Batch Script - Input / Output
In this article, we are going to learn how to take input from users using Batch Script. Taking User Input:@echo off echo Batch Script to take input. set /p input= Type any input echo Input is: %input% pauseExplanation :The first 'echo' command is used to print a string.In the next line, 'set /p' com
1 min read
Batch Script - String Concatenation
String Concatenation is combining two or more strings to create a new string. For instance, let a string "Good" and take another string "Morning" now by string concatenation i.e. "Good" + "Morning" and we got new string i.e. "Good Morning". This is string concatenation. Let see some examples of stri
2 min read