Shell Scripting

A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. We use a combination of various terminal commands (technically programs) to create a script to do a specific task.

Shell scripting is usually used to automate any task for example it can be used to calculate attendance or salary, to install-uninstall software, also it can be used to create applications such as a CRUD application, etc.

Prerequisites are linux commands

Example -: I need to combine my 5 programs to create a text document.

MANUAL APPROACH

The manual Approach will be I will be open every file copy its content and put it to the text document and repeat the task 5 times, seems easy but what if there are 1000 source files? Now we need automation for this.

AUTOMATION

I should write a shell script that will do my task.

#createDocument.sh
a=`ls *.cpp`
touch document.txt
x=1
for i in $a
do
echo "Example $x" >>document.txt
x=`expr $x + 1`
cat $i >>document.txt
echo "">>document.txt
done
echo "Combined Document created as document.txt"

Suppose there are 5 CPP source files as eg1.cpp, eg2.cpp to eg5.cpp

eg1.cpp

//Program to find sum of vector elements.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int> a(n);
int sum=0;
for(int e=0;e<n;e++)
{
cin>>a[e];
sum+=a[e];
}
cout<<"Total of array is: "<<sum<<endl;
return 0;
}

eg2.cpp

//Program to find average of vector elements.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int> a(n);
int sum=0;
for(int e=0;e<n;e++)
{
cin>>a[e];
sum+=a[e];
}
cout<<"Average of array is: "<<sum/n<<endl;
return 0;
}

eg3.cpp

//Program to sort vector elements.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int> a(n);
for(int e=0;e<n;e++)
{
cin>>a[e];
}
cout<<"Sorted array is: ";
sort(a.begin(),a.end());
for(int e:a) cout<<e<<" ";
return 0;
}

eg4.cpp

//Program to find product of vector elements
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int> a(n);
int prd=1;
for(int e=0;e<n;e++)
{
cin>>a[e];
prd*=a[e];
}
cout<<"Product of array is: "<<prd<<endl;
return 0;
}

eg5.cpp

//Program to determine whether vector is an AP series or not #include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int> a(n);
int d;
for(int e=0;e<n;e++)
{
cin>>a[e];
}
d=a[1]-a[0];
for(int e=2;e<n;e++)
{
if(d!=a[e]-a[e-1])
{
cout<<"Given Array is not an AP Series";
return 0;
}
}
cout<<"Given Array is an AP Series";
return 0;
}

Now for the windows operating system how would you try this, so for that, you can use git bash as it is almost like a Linux terminal.

The directory containing 5 CPP source files

To execute the script there are 2 options [linux terminal]

1. sh createDocument.sh or bash createDocument.sh
2. chmod +x createDocument.sh
./createDocument.sh
Here you can see that document.txt has been created with proper separation and serial number
Git Bash directory list

to execute

sh createDocument.sh
or
bash createDocument.sh
You can see that file has been created on the Windows platform by using Git Bash for executing the shell script

In this way, using various combinations of available commands we can create a shell script for automating various tedious tasks.

--

--

--

I am a pre-final year CSE student. Ex SDE Intern at Cerebry (Singapore). I like to learn and explore new things.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Axelar Network. To developers with a problem of choosing a network for launching is devoted

DYNAMIC PROGRAMMING

10 Tips To Write Effective Code Reviews

Advanced UI testing

Hyllerydder — Shelf Cleaner

Building a Kubernetes Platform for Fun and Profit (Part Two)

Gremlin Traversals: Don’t get lost in the route #5

Cloud Foundry Schematic

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tanishq Rawat

Tanishq Rawat

I am a pre-final year CSE student. Ex SDE Intern at Cerebry (Singapore). I like to learn and explore new things.

More from Medium

Bash aliases

Bash shell

LINUX LOOPS

Linux Commands Part One: Navigation

Linux Fundamentals. Part 2