Skip to main content
  1. Data Science Blog/

Exploring Shell Scripting and Batch Files

·791 words·4 mins· loading · ·
Programming System Administration Development Environment & Tools Scripting Operating Systems Linux

On This Page

Table of Contents
Share with :

Exploring Shell Scripting and Batch Files

Exploring Shell Scripting and Batch Files
#

Batch files are scripts that contain a series of commands to be executed by the command-line interpreter. These files can automate repetitive tasks and enhance productivity. In this article, we’ll explore creating batch files to set environment variables and execute shell commands in Windows, PowerShell, and Ubuntu, along with additional scripting nuances.

The Audience
#

The audience for this article is

  • those are working with Windows, PowerShell, and Ubuntu.
  • who are not exposed to any scripting language
  • who learned in the the college but out of touch
  • want to automate tasks at shell level

Windows
#

Setting Environment Variables
#

In Windows, you can create a batch file (.bat) to set environment variables and execute shell commands. Here’s an example:

@echo off
rem Setting an environment variable
set MY_VAR=my_value

rem Displaying the value of the environment variable
echo %MY_VAR%

rem Executing a shell command
dir

To execute this batch file you need to type of name of batch file like mybatch.bat on cmd terminal. You can modularize these batch files and create a main batch files to call all these based on the need or condition.

Looping
#

You can use a for loop to iterate over a set of values:

@echo off
for %%i in (1 2 3 4 5) do (
    echo Iteration %%i
)

Conditionals
#

You can use if statements for conditional execution:

@echo off
set MY_VAR=my_value

if "%MY_VAR%" == "my_value" (
    echo MY_VAR is set to my_value
) else (
    echo MY_VAR is not set to my_value
)

Variables
#

  1. Local Variables: Variables set within the script that are accessible only within the current script or session.
  2. Global Variables: Environment variables that are available system-wide.

PowerShell
#

Setting Environment Variables
#

In PowerShell, you can create a script (.ps1) to set environment variables and execute commands. Here’s an example:

# Setting an environment variable
$env:MY_VAR = "my_value"

# Displaying the value of the environment variable
Write-Output $env:MY_VAR

# Executing a shell command
Get-ChildItem

Looping
#

You can use foreach loops for iteration:

foreach ($i in 1..5) {
    Write-Output "Iteration $i"
}

Conditionals
#

You can use if statements for conditional execution:

$MY_VAR = "my_value"

if ($MY_VAR -eq "my_value") {
    Write-Output "MY_VAR is set to my_value"
} else {
    Write-Output "MY_VAR is not set to my_value"
}

Variables
#

  1. Local Variables: Variables declared within a script or function.
  2. Global Variables: Variables that are available throughout the PowerShell session.

Ubuntu (Linux)
#

Setting Environment Variables
#

In Ubuntu, you can create a shell script (.sh) to set environment variables and execute commands. Here’s an example:

#!/bin/bash
# Setting an environment variable
export MY_VAR="my_value"

# Displaying the value of the environment variable
echo $MY_VAR

# Executing a shell command
ls

.bashrc
#

To set persistent environment variables and commands, you can add them to your .bashrc file:

  1. Open the .bashrc file:

    nano ~/.bashrc
    
  2. Add the variables and commands:

    export MY_VAR="my_value"
    echo "MY_VAR is set to $MY_VAR"
    ls
    
  3. Apply the changes:

    source ~/.bashrc
    

    By using “source”, it will affect the current session. Otherwise it will start a new session. ~/. is to ensure execute bash script stored in home directory. This .bashrc file must stored only in the home directory.

Looping
#

You can use for loops for iteration:

#!/bin/bash
for i in {1..5}
do
   echo "Iteration $i"
done

Conditionals
#

You can use if statements for conditional execution:

#!/bin/bash
MY_VAR="my_value"

if [ "$MY_VAR" == "my_value" ]; then
    echo "MY_VAR is set to my_value"
else
    echo "MY_VAR is not set to my_value"
fi

Variables
#

  1. Local Variables: Variables declared and used within a script or function.
  2. Global Variables: Environment variables that are available system-wide.

Purpose of .bashrc file
#

Startup Configuration: The .bashrc file is executed whenever an interactive non-login shell is started. This means it configures the shell environment with user-defined settings, such as environment variables, aliases, and functions.

Customization: It allows users to customize their shell experience by adding configurations that are automatically applied every time a new terminal session is started.

Persistent Settings: It ensures that certain settings and environment variables persist across all shell sessions without the need for manual reconfiguration.

How It Differs from Regular Scripts?
Automatic Execution: Unlike regular scripts, which must be explicitly executed, the .bashrc file is automatically sourced when a new interactive shell session begins.

Environment Changes: Changes made in .bashrc affect the current shell session, making it ideal for setting environment variables and aliases that you want available every time you open a terminal.

Conclusion
#

Creating batch files to set environment variables and execute shell commands can significantly streamline your workflow. Whether you’re using Windows, PowerShell, or Ubuntu, understanding these basics and additional scripting nuances will empower you to automate tasks effectively.

Dr. Hari Thapliyaal's avatar

Dr. Hari Thapliyaal

Dr. Hari Thapliyal is a seasoned professional and prolific blogger with a multifaceted background that spans the realms of Data Science, Project Management, and Advait-Vedanta Philosophy. Holding a Doctorate in AI/NLP from SSBM (Geneva, Switzerland), Hari has earned Master's degrees in Computers, Business Management, Data Science, and Economics, reflecting his dedication to continuous learning and a diverse skill set. With over three decades of experience in management and leadership, Hari has proven expertise in training, consulting, and coaching within the technology sector. His extensive 16+ years in all phases of software product development are complemented by a decade-long focus on course design, training, coaching, and consulting in Project Management. In the dynamic field of Data Science, Hari stands out with more than three years of hands-on experience in software development, training course development, training, and mentoring professionals. His areas of specialization include Data Science, AI, Computer Vision, NLP, complex machine learning algorithms, statistical modeling, pattern identification, and extraction of valuable insights. Hari's professional journey showcases his diverse experience in planning and executing multiple types of projects. He excels in driving stakeholders to identify and resolve business problems, consistently delivering excellent results. Beyond the professional sphere, Hari finds solace in long meditation, often seeking secluded places or immersing himself in the embrace of nature.

Comments:

Share with :

Related

What is a Digital Twin?
·805 words·4 mins· loading
Industry Applications Technology Trends & Future Computer Vision (CV) Digital Twin Internet of Things (IoT) Manufacturing Technology Artificial Intelligence (AI) Graphics
What is a digital twin? # A digital twin is a virtual representation of a real-world entity or …
Frequencies in Time and Space: Understanding Nyquist Theorem & its Applications
·4103 words·20 mins· loading
Data Analysis & Visualization Computer Vision (CV) Mathematics Signal Processing Space Exploration Statistics
Applications of Nyquists theorem # Can the Nyquist-Shannon sampling theorem applies to light …
The Real Story of Nyquist, Shannon, and the Science of Sampling
·1146 words·6 mins· loading
Technology Trends & Future Interdisciplinary Topics Signal Processing Remove Statistics Technology Concepts
The Story of Nyquist, Shannon, and the Science of Sampling # In the early days of the 20th century, …
BitNet b1.58-2B4T: Revolutionary Binary Neural Network for Efficient AI
·2637 words·13 mins· loading
AI/ML Models Artificial Intelligence (AI) AI Hardware & Infrastructure Neural Network Architectures AI Model Optimization Language Models (LLMs) Business Concepts Data Privacy Remove
Archive Paper Link BitNet b1.58-2B4T: The Future of Efficient AI Processing # A History of 1 bit …
Ollama Setup and Running Models
·1753 words·9 mins· loading
AI and NLP Ollama Models Ollama Large Language Models Local Models Cost Effective AI Models
Ollama: Running Large Language Models Locally # The landscape of Artificial Intelligence (AI) and …