-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell Scripting
More file actions
297 lines (217 loc) · 10.1 KB
/
Shell Scripting
File metadata and controls
297 lines (217 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
Shell Scripting
---------------
1) Shell is responsible to read commands/applications provided by user.
2) Shell will check whether command is valid or not and whether it is properly used or not. If everything
is proper then shell interpretes (converts) that command into kernel understandable form that interpreted
command will be handover to kernel.
3) Kernel is responsible to execute that command with the help of hardware.
--> Shell acts as interface between user and kernel.
--> shell+kernel is nothing but operating system.
--> A sequence of commands saved to a file and this file is nothing but shell script.
--> Inside shell script, we can also use programming features like conditional statements, loops, functions
etc. Hence we can write scripts very easily for complex requirements also.
--> Best suitable for automated tasks.
Purpose:
--> Automation of repetitive tasks (e.g., backups, system monitoring).
--> System administration (e.g., managing user accounts, configuring systems).
--> Software development (e.g., automating builds, deployments)
Types of Shells(Manily):
------------------------
1. Bourne Shell - was developed by Stephens Bourne. by using sh we can execute our scrpts in Bourne Shell
2. BASH - Bourne Again SHell .BASH was created as a free software replacement for the Bourne Shell,
adding more features and improvements.
3.Korn Shell - developed by David Korn.Mostly used in IBM AIX OS. we are using ksh for korne shell
4. CShell - Developed by Bill joy from California University.Its syntax resembles the C programming language,
making it more familiar to C programmers at the time. csh command we are using accessing Cshell
5. TShell - Advanced Version of Cshell.Mostly Used in HP UNIX Systems. by using tcsh we are accessing TShell
6. ZShell - Developed by Paul.by using zsh command we are acess this shell
How To check default Shell for User:
------------------------------------
Method1: echo $0
Method2: echo $SHELL ( here SHELL is Predefined Variable it will give default Shell Value. by using env we
can see Predefined or System defined Variable )
Method3: cat /etc/passwd (ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash)
How to check available Shells in my System:
--------------------------------------------
cat /etc/shells
Ex:
ubuntu@ip-172-31-14-39:~$ cat /etc/shells
/bin/sh
/usr/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
Shabang:
-------
By using sha-bang, we can specify the interpreter which is responsible to execute the
script.
# --> Sharp
! --> Bang
#! --> Sharp Bang or Shabang or Shebang
#! /bin/bash --> It means the script should be executed by bash
#! /bin/sh --> It means the script should be executed by Bourne Shell
#! /usr/bin/python3 --> It means the script should be executed by Python3 interpreter
--> The shebang line must be the very first line of the script.
--> The path to the interpreter must be an absolute path (e.g., /usr/bin/python3, not just python3).
--> If the shebang line is missing, the OS usually tries to execute the script using the current shell,
which might not be the correct interpreter.
The shebang line always starts with #! followed by the absolute path to the interpreter.
Examples:
#!/bin/bash : Uses the Bash shell to execute the script.
#!/usr/bin/python3 : Uses Python 3.
#!/usr/bin/perl : Uses Perl.
#!/bin/sh : Uses the Bourne shell.
Note:
For Example in my script i have written Sha-Bang as ' #! /bin/sh ' and then i try to execute the script
using ' ./demo.sh ' (executing using Bourne Shell) . here Operating System (OS) will check the File file is there
Sha Bang is there are not. if it is availble then script will execute using thet Sha-Bang not by Default Bourne Shell.
If Sha-Bang is not there in file then script will execute by using default shell(Borune Shell).
Basic Shell Scripting:
----------------------
Ex1:
----
[ec2-user@ip-172-31-9-235 ~]$ vi demo.sh
echo " Hi Welcome to shell Scripting"
date
cal
[ec2-user@ip-172-31-9-235 ~]$ ls -l
-rw-r--r--. 1 ec2-user ec2-user 47 Apr 1 07:04 demo.sh
[ec2-user@ip-172-31-9-235 ~]$ chmod a+x demo.sh # giving execute permissions to all(users,groups and others as well)
[ec2-user@ip-172-31-9-235 ~]$ ls -l
-rwxr-xr-x. 1 ec2-user ec2-user 47 Apr 1 07:04 demo.sh
[ec2-user@ip-172-31-9-235 ~]$ sh demo.sh or /bin/bash /home/ec2-user/demo.sh or ./demo.sh
Hi Welcome to shell Scripting
Tue Apr 1 07:04:39 UTC 2025
April 2025
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
-----
[ec2-user@ip-172-31-9-235 ~]$ echo $PATH
/home/ec2-user/.local/bin:/home/ec2-user/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
[ec2-user@ip-172-31-9-235 ~]$ export PATH=$PATH:/home/ec2-user/
[ec2-user@ip-172-31-9-235 ~]$ echo $PATH
/home/ec2-user/.local/bin:/home/ec2-user/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/
[ec2-user@ip-172-31-9-235 ~]$ demo.sh
Note:
--> Now we can run our script from anywhere. We are not required to provide either absolute path (or) relative path.
--> This way of setting PATH variable is applicable only for current session. Once we close terminal then
automatically these changes will be lost.
--> In our user home directory there is a file named with .bashrc. This file will be executed every time
automatically whenever we open the terminal. If we define PATH variable in this file then that PATH value
will become permanent and we are not required to setevery time.
--> Add the following line at bottom of this file
Ex:
[ec2-user@ip-172-31-9-235 ~]$ export PATH=$PATH:/home/ec2-user/
Python script:
--------------
$ vi test.py
#! /usr/bin/python3
import random
name= input("Enter Your Name : ")
l=["sunny","nithya menon","trisha","deepika","anupama","Varalaxmi"]
print("Hello :", name)
print("congrats ... You are Going to marry : " ,random.choice(l))
$ chmod +x test.py
$ ./test.py
To Know Interpreters we are using which command in Linux. if required interpreter is not there then you need to install.
$ which python3 ===> /usr/bin/python3
$ which python or which python2
$ which perl
$ which sh
---------------------------
Not Work: (it won't give output)
---------
$ echo $pwd
$ echo $ls -l
$ echo $cal In Ubuntu ==> Before execute this command we need to install ncal.
==> sudo apt install ncal
***Command Substitution
--------------------
Methods to accessing the Commands
old style : `command` Ex: echo `pwd`
new style : $(command) Ex: echo $(pwd)
Ex:
---
Q1) Print Only Todays Date not time?
Ans: $echo "Todays date is : $(date +%D) " here +%D means display only Date not Time.
above command output ==> Todays date is : 04/07/25
$ echo "Time is : `date +%T` " here +%T means display Only Time not Date
==> Time is : 07:35:41
Q2) To Print Number Files in Present/Print Working Directory?
$ ls
Test demo.sh project.txt test.py
Old : $ echo "The Number Files: `ls|wc -l` " ==> The Number Files: 4
New : $ echo "The Number Files: $(ls |wc -l) " ==> The Number Files: 4
$ echo $(pwd)
/home/ec2-user
$ echo $(ls -l)
-rwxr-xr-x. 1 ec2-user ec2-user 47 Apr 1 07:04 demo.sh
$ echo $(cal)
2025 April Sunday Monday ...... Saturday 1 2 3 4 5 6 7 8 9 10 ...........30 31
****Variable Substitution
-----------------------
$ name="sai majji"
$ echo '$name' --> $name (output)
$ echo "$name" --> sai majji (output)
$ echo $name --> sai majji
$ echo name --> name
Note:
here i am taking string assigned to name variable.i am trying to access different possible ways i am getting the above output.
****don't use single Quotes while accessing the Varibales. Variable Substitution won't be happend for single Quotes.
Variables in Shell Scripting
-----------------------------
1) Pre-Defined Variables (or) Environment Variable
2) User defined Variables
1) Pre-Defined Variables (or) Environment Variable:
---------------------------------------------------
--> These are predefined variables and mostly used internally by the system. Hence these variables also
known as System variables.
--> We can get all environment variables information by using either "env" command
Ex: HISTCONTROL=ignoredups
HISTFILE=/home/ec2-user/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/home/ec2-user
HOSTNAME=ip-172-31-9-235.ap-south-1.compute.internal
PWD=/home/ec2-user
LOGNAME=ec2-user
--> /home/ec2-user/.bash_history -- In this hidden file we are storing our history. by default HISTSIZE is 1000
Ex: echo $HISTSIZE
export HISTSIZE=300
echo $HISTSIZE
Note: It is only for particular session, How to change permanently?
Ex:
~/.bash_profile
export HISTSIZE=300
[ec2-user@ip-172-31-9-235 ~]$ ls -la
-rw-------. 1 ec2-user ec2-user 1661 Apr 1 08:01 .bash_history
-rw-r--r--. 1 ec2-user ec2-user 18 Jan 28 2023 .bash_logout
-rw-r--r--. 1 ec2-user ec2-user 141 Jan 28 2023 .bash_profile
-rw-r--r--. 1 ec2-user ec2-user 492 Jan 28 2023 .bashrc
-rw-------. 1 ec2-user ec2-user 20 Mar 30 13:09 .lesshst
drwx------. 2 ec2-user ec2-user 29 Mar 28 17:05 .ssh
-rw-------. 1 ec2-user ec2-user 1148 Apr 1 07:04 .viminfo
How to Change Prompt:
--------------------
Internally PS1 Environment variable is responsible to display terminal prompt.By reassigning the value we can
change prompt.
Ex:
[ec2-user@ip-172-31-9-235 ~]$ PS1=sai
sai
sails -l
-rwxr-xr-x. 1 ec2-user ec2-user 47 Apr 1 07:04 demo.sh
sai$PS1=saimajji@ip172-31-9-235$
saimajji@ip172-31-9-235$
2) User defined Variables:
---------------------------
--> Based on our programming requirement, we can define our own variables also.
Ex:
[ec2-user@ip-172-31-9-235 ~]$ a=10
[ec2-user@ip-172-31-9-235 ~]$ echo "Hi A value is : $a"
Hi A value is : 10
---------------------------------------------------