Repository navigation
Expand file tree
/
Copy pathbash_scripting
More file actions
228 lines (143 loc) · 6.23 KB
/
Copy pathbash_scripting
File metadata and controls
228 lines (143 loc) · 6.23 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
0. Write comment inside bash script
#This is comment written by Mr. Pranav.
1. Print 'hello world'
#!/bin/bash
echo "hello world"
`````````````````````
2. Use time delay to print
#!/bin/bash
echo "Hi, Pranav, how are you?"
sleep 1
echo "I'm good. What about you?"
`````````````````````````````````````````````````
3. Use variable to print something
#!/bin/bash
name="Pranav"
echo "Hello $name"
[Don't leave blank space while defining a varabile. It will throw an error or warnings.]
``````````````````````````````````````````````````
4. Take user input
#!/bin/bash
echo "What is your name?"
read name
echo "Hi, $name"
````````````````````````````````````````````````````
5. Example of postitional prameter
#!/bin/bash
name=$1
place=$2
echo "What is your name?"
sleep 1
echo "My name is $name."
sleep 2
echo "Where you from?"
sleep 1
echo "I'm from $place"
sleep 2
echo "So $name, you came all the way from $place looking for job. Am I right?"
sleep 1
echo "yes"
[Run this file like: ./scriptname.sh pranav parasi]
[We are using position of parameter to call it. $1 denote the first position and so on.]
[Note: Script can run regardless of providing parameter. In such case, it will print empty space.]
```````````````````````````````````````````````````````````````````````````````````````````````````
6. How to run Linux command inside bash script? (tips: using variables)
#!/bin/bash
echo "What time and date today is?"
time=$(date)
sleep 1
echo "Today is $time"
sleep 2
echo "Which shell do you mostly use in linux?"
shell=$(which $SHELL)
sleep 1
echo "I use $shell in linux."
[here, linux commands written are inside parenthesis]
```````````````````````````````````````````````````````````````
7. How to change user-defined variable to system variable?
Windows="Bill Gates" //create user defined variable
$Windows //will print variable value
export Windows //will convert user-defined variable to system variable
[There are two types of environment variables: i. System Environment Variable ii. User Environment Variable]
```````````````````````````````````````````````````````````````````````````````````````````````````````````````
8. How to make user-defined system variable permanent so that it doesn't disapper after reboot/shutdown)?
nano .bashrc
export Windows="Bill Gates"
[Save the bashrc file. Now the system variable will be available ready to use anytime.]
```````````````````````````````````````````````````````````````````````````````````````
9. How to do simple math calculation?
echo $((2+2)) //ans: 4
echo $((4/2)) //ans: 2
echo $((3*3)) //ans: 9
echo $((7%3)) //ans: 1 //prints out remainder after division of 7 by 3
[Double parenthesis in mandatory after dollar sign. Bash by default, doesn't print floating no.]
````````````````````````````````````````````````````````````````````````````````````````````````
10. How to print random number?
echo $RANDOM //print random number
echo $(($RANDOM%4)) //print randomly from 0 to 3; excluding 4.
```````````````````````````````````````````````````````````````````
11. How to write if-else loop?
#!/bin/bash
echo "Hey, do you like this repository? (yes/no)"
read reply
if [[ $reply == "yes" ]]; then
echo "hire me"
else
echo "What else can I do to improve my skill?"
fi
[The word "fi" is used to close the program. All the white spaces which you see in if statement is mandatory.]
```````````````````````````````````````````````````````````````````````````````````````````````````````````````
12. Why we need exit numbers?!
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````
13. Tricks of semicolon. Multiple command inside 1 command.
echo what is your name?; echo my name is bond, james bond
[Don't use single or double quotion marks. Like: echo what's your name?]
````````````````````````````````````````````````````````````````````````
14. What is the difference is ; and && in bash script?
; -----> will run all command, regardless of previous command failure
&& -----> will only run only when previous command works properly
Example:
cat GitHub/Linux-101/bash_scripting ; echo "This is how I learn bash." // works fine because do we have 'bash_scripting' file
cat GitHub/Linux-101/python_scripting ; echo "This is how I learn python" // throws an error in first command but still runs fine
cat GitHub/Linux-101/bash_scripting && echo "This is how I learn bash." // works fine like the first command
cat GitHub/Linux-101/python_scripting && echo "This is how I learn python" // program will halt since python_scripting don't exist
[So its a good idea to prefer "&&" over semicolon.]
```````````````````````````````````````````````````````````````````````````
15. What is the difference in && and || in bash script?
&& --------> will only run if previous command run properly
|| --------> will only run if previous command faild.
Example:
cat GitHub/Linux-101/bash_scripting || echo "This command doesn't run." //Since we do have file named bash_scripting, second command won't be executed.
cat GitHub/Linux-101/python_scripting || echo "This file doesn't exit." //The first command fails because there is no file named python_scripting. So second command is executed.
````````````````````````````````````````````````````````````````````````
16. Usage of Nested Loops, Or condition in loops.
#!/bin/bash
echo "What is your age?"
read age
#here, saying "adult" acts as an cheetsheet for teenagers. -gt means greater than
if [[ $age -gt 17 || $age == "adult" ]]; then
echo "Welcome to club!"
#If condition of previus if-statement is not satisfied, the nested loop written below is used.
elif [[ $USER == "pranav" ]]; then
echo "You are the VIP member of this club. Welcome sir."
else
echo "Sorry, you are not mature enough to join the club."
fi
````````````````````````````````````````````````````````````````
17. While loops to print mantra 5 times
#!/bin/bash
x=1
while [[ $x -le 5 ]]
do
echo "1) OM Namah Shiwaya"
done
```````````````````````````````````````````
18. Using while loop asking for your user input.
#A program which ask all question of exam are solved or not
x=1
while [[ $x -le 15 ]]
do
read -p "Have you solved question $x?"
(( x ++ ))
done
echo "Congratulation, I think you will pass the exam"