Open In App

Batch Script – Return code

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.
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
90090×2331 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:

Output

Loops:

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

Article Tags :