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.
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
to execute
sh createDocument.sh
or
bash createDocument.sh
In this way, using various combinations of available commands we can create a shell script for automating various tedious tasks.