-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_script
More file actions
282 lines (221 loc) Β· 6.93 KB
/
Copy pathshell_script
File metadata and controls
282 lines (221 loc) Β· 6.93 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
π§Ύ Linux Environment Variables β One Page Summary
πΉ 1. Temporary Environment Variables
Scope: Current shell/session only
Command:
export MY_VAR="TemporaryValue"
echo $MY_VAR
Lost when: You close the terminal or reboot.
πΉ 2. Permanent (User-Specific) Variables
Scope: Only available to the current user, across sessions.
Files: ~/.bashrc, ~/.bash_profile, or ~/.profile (depending on shell)
π How to set:
Open .bashrc:
nano ~/.bashrc
Add at the end:
export MY_VAR="MyUserValue"
Apply changes:
source ~/.bashrc
πΉ 3. Permanent (All Users β System-Wide)
β
Option A: /etc/environment
Scope: All users, all sessions
Use for: Simple KEY="value" pairs
No export needed
sudo nano /etc/environment
# Example:
MY_VAR="GlobalValue"
Apply: Reboot or log out/in
β
Option B: /etc/profile or /etc/profile.d/*.sh
Scope: All users, interactive shells
Supports: Shell scripting, export, logic
sudo nano /etc/profile.d/myvars.sh
# Add:
export MY_VAR="AnotherGlobalValue"
sudo chmod +x /etc/profile.d/myvars.sh
source /etc/profile.d/myvars.sh
π§ͺ Check a Variable
echo $MY_VAR
printenv MY_VAR
π‘ Shell Scripting Special Variables
Variable Meaning Example Description
$1 First argument ./script.sh foo bar β $1 = foo Refers to the first argument passed to the script
$@ All arguments ./script.sh foo bar β $@ = foo bar Refers to all arguments as separate quoted strings ("$1", "$2"...)
$? Exit status of last command After a command: $? = 0 (success) or 1 (failure) Shows the exit code of the last executed command
$# Number of arguments ./script.sh foo bar β $# = 2 Returns the total number of arguments passed to the script
---
Its shows Vpcs id = aws ec2 describe-vpcs --region ap-south-1 | jq ".Vpcs[].VpcId"
aws ec2 describe-vpcs --region ap-south-1 | jq ".Vpcs[].VpcId" -r
---
#!/bin/bash
REGION='ap-south-1'
aws ec2 describe-vpcs --region ${REGION} | jq ".Vpcs[].VpcId" -r
#!/bin/bash
REGION=$1
aws ec2 describe-vpcs --region ${REGION} | jq ".Vpcs[].VpcId" -r
---
#!/bin/bash
awd --version 2> /dev/null
if [ $? -eq 0 ]; then
REGION=$1
aws ec2 describe-vpcs --region ${REGION} | jq ".Vpcs[].VpcId" -r
else
echo "incorrect command"
fi
---
#!/bin/bash
aws --version 2> /dev/null
if [ $? -eq 0 ]; then
REGION=$1
aws ec2 describe-vpcs --region ${REGION} | jq ".Vpcs[].VpcId" -r
else
echo "incorrect command"
fi
--------------------
error code in shell script
Exit Code | Meaning | Notes
0 | Success | Command executed successfully
1 | General error | Catch-all for general errors
2 | Misuse of shell builtins | Missing keyword, syntax error, etc.
126 | Command found but not executable | Permissions issue (e.g., no execute permission)
127 | Command not found | Typo or missing software
128 | Invalid argument to exit | E.g., exit 300 gives this since 300 > 255
130 | Script terminated by Ctrl+C | Signal interrupt (SIGINT)
137 | Killed by SIGKILL (kill -9) | Forcefully killed by system or user
139 | Segmentation fault | Crash due to memory access violation
-------------------------------------------------
$@ All arguments ./script.sh foo bar β $@ = foo bar Refers to all arguments as separate quoted strings ("$1", "$2"...)
#!/bin/bash
aws --version
if [ $? -eq 0 ]; then
REGIONS=$@
for REGION in $REGIONS; do
aws ec2 describe-vpcs --region ${REGION} | jq ".Vpcs[].VpcId" -r
done
else
echo "Incorrect command"
fi
------------------------------------------------------------------
$# Number of arguments ./script.sh foo bar β $# = 2 Returns the total number of arguments passed to the script
#!/bin/bash
# Check if at least one argument is provided
if [ $# -gt 0 ]; then
REGIONS=$@
echo "Fetching VPC IDs for regions: $REGIONS"
for REGION in $REGIONS; do
aws ec2 describe-vpcs --region ${REGION} | jq ".Vpcs[].VpcId" -r
done
else
echo "You have provided $# arguments. Please provide at least one region."
exit 1
fi
echo $#
------------------------------------------
#!/bin/bash
for I in {1..100}; do
if [ $((I % 2)) -eq 0 ]; then
echo "$I is an even number"
else
echo "$I is an odd number"
fi
done
~
~
~
~
$1 = single argument
$@ = multiple argument
$? = error code
$# = no. of argument show
-----------------------------------
#!/bin/bash
for I in {1..100}; do
if [ $((I % 2)) -eq 0 ]; then
echo "$I is an even number"
else
echo "$I is an odd number"
fi
done
------------------------------------------
π FOR Loop Examples
β
Example 1: Print numbers from 1 to 5
#!/bin/bash
for i in {1..5}; do
echo "Number: $i"
done
-----------------
π§Ύ Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
---------------
β
Example 2: Loop through a list of strings
#!/bin/bash
for fruit in apple banana cherry; do
echo "Fruit: $fruit"
done
-----------
π§Ύ Output:
Fruit: apple
Fruit: banana
Fruit: cherry
------------
π WHILE Loop Examples
β
Example 1: Print numbers from 1 to 5
#!/bin/bash
i=1
while [ $i -le 5 ]; do
echo "Count: $i"
((i++))
done
----------
π§Ύ Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
------------
β
Example 2: Countdown from 5 to 1
#!/bin/bash
i=5
while [ $i -ge 1 ]; do
echo "Countdown: $i"
((i--))
done
----------
π§Ύ Output:
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
---------------------------------------
Operator | Meaning
-eq | Equal to
-ne | Not equal to
-lt | Less than
-gt | Greater than
-le | Less than or equal
-ge | Greater than or equal
---------------------------------
aws_regions=(us-east-1 us-east-2 hyd-india-1 eu-north-1 ap-south-1 eu-west-3 eu-west-2 eu-west-1 ap-northeast-2)
echo "Running the function to list VPCs using the regions list"
for region in "${aws_regions[@]}"; do
echo "Getting VPCs in $region .. "
vpc_list=$(aws ec2 describe-vpcs --region "$region" | jq -r .Vpcs[].VpcId)
vpc_arr=(${vpc_list[@]})
if [ ${#vpc_arr[@]} -gt 0 ]; then
for vpc in "${vpc_list[@]}"; do
echo "The VPC-ID is: $vpc"
done
echo "##########"
else
echo "Invalid Region..!!"
echo "#######"
echo "# Breaking at $region #"
echo "################"
break
fi
done
---------------------------------------------