Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Batch Script - Logging
Next article icon

Batch Script - Return code

Last Updated : 27 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 CodeDescription
0Successful completion of the program.
1 This error indicates that the Windows command prompt has attempted to execute an unrecognized action
2An error indicating that the file could not be found in the specified location
3An error message indicated that the specified path could not be found.
5An indication that the user is not authorized to access the resource
90090x2331This error occurs when you misspell the command, application name, or path when configuring an Action.
2212254950xC0000017-1073741801The 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 NoLoopsDescription
1While Statement ImplementationThere is no direct while statement in Batch Script, although labels and an if statement can be used to implement this loop.
2For Statement - List ImplementationsBatch files can loop using the "FOR" construct. In order to work with a list of values, the 'for' statement requires the following construct.
3Looping through Ranges'For' statements can also move through ranges of values. A general version is presented below.
4Classic for Loop ImplementationIt has the classic 'for' statement found in many programming languages.
5Break Statement ImplementationWithin 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

Next Article
Batch Script - Logging
author
muthuannamalai2002
Improve
Article Tags :
  • Linux-Unix
  • Batch-script

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences