-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshellstuff
More file actions
311 lines (218 loc) · 6.16 KB
/
shellstuff
File metadata and controls
311 lines (218 loc) · 6.16 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
case "$1" in
(runApp1)
runApp1
exit 1
;;
(runApp)
runApp
exit 0
;;
(*)
echo "Usage: $0 {runApp1|runApp}"
exit 2
;;
esac
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/bin/bash
# file.sh: a sample shell script to demonstrate the concept of Bash shell functions
# define usage function
usage(){
echo "Usage: $0 filename"
exit 1
}
# define is_file_exists function
# $f -> store argument passed to the script
is_file_exists(){
local f="$1"
[[ -f "$f" ]] && return 0 || return 1
}
# invoke usage
# call usage() function if filename not supplied
[[ $# -eq 0 ]] && usage
# Invoke is_file_exits
if ( is_file_exists "$1" )
then
echo "File found: $1"
else
echo "File not found: $1"
fi
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/bin/bash
# ref-params.sh: Dereferencing a parameter passed to a function.
# (Complex Example)
ITERATIONS=3 # How many times to get input.
icount=1
my_read () {
# Called with my_read varname,
#+ outputs the previous value between brackets as the default value,
#+ then asks for a new value.
local local_var
echo -n "Enter a value "
eval 'echo -n "[$'$1'] "' # Previous value.
# eval echo -n "[\$$1] " # Easier to understand,
#+ but loses trailing space in user prompt.
read local_var
[ -n "$local_var" ] && eval $1=\$local_var
# "And-list": if "local_var" then set "$1" to its value.
}
echo
while [ "$icount" -le "$ITERATIONS" ]
do
my_read var
echo "Entry #$icount = $var"
let "icount += 1"
echo
done
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/bin/bash
# Arabic number to Roman numeral conversion
# Range: 0 - 200
# It's crude, but it works.
# Extending the range and otherwise improving the script is left as an exercise.
# Usage: roman number-to-convert
LIMIT=200
E_ARG_ERR=65
E_OUT_OF_RANGE=66
if [ -z "$1" ]
then
echo "Usage: `basename $0` number-to-convert"
exit $E_ARG_ERR
fi
num=$1
if [ "$num" -gt $LIMIT ]
then
echo "Out of range!"
exit $E_OUT_OF_RANGE
fi
to_roman () # Must declare function before first call to it.
{
number=$1
factor=$2
rchar=$3
let "remainder = number - factor"
while [ "$remainder" -ge 0 ]
do
echo -n $rchar
let "number -= factor"
let "remainder = number - factor"
done
return $number
# Exercises:
# ---------
# 1) Explain how this function works.
# Hint: division by successive subtraction.
# 2) Extend to range of the function.
# Hint: use "echo" and command-substitution capture.
}
to_roman $num 100 C
num=$?
to_roman $num 90 LXXXX
num=$?
to_roman $num 50 L
num=$?
to_roman $num 40 XL
num=$?
to_roman $num 10 X
num=$?
to_roman $num 9 IX
num=$?
to_roman $num 5 V
num=$?
to_roman $num 4 IV
num=$?
to_roman $num 1 I
# Successive calls to conversion function!
# Is this really necessary??? Can it be simplified?
echo
exit
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
count_lines_in_etc_passwd()
{
[[ -r /etc/passwd ]] && REPLY=$(echo $(wc -l < /etc/passwd))
}
if count_lines_in_etc_passwd
then
echo "There are $REPLY lines in /etc/passwd."
else
echo "Cannot count lines in /etc/passwd."
fi
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function lsext()
{
find . -type f -iname '*.'${1}'' -exec ls -l {} \; ;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$ function batchexec()
{
find . -type f -iname '*.'${1}'' -exec ${@:2} {} \; ;
}
$ cd ~
$ batchexec sh ls
$ batchexec sh chmod 755
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$ function rpass() {
cat /dev/urandom | tr -cd '[:graph:]' | head -c ${1:-12}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$ function getip()
{
/sbin/ifconfig ${1:-eth0} | awk '/inet addr/ {print $2}' | awk -F: '{print $2}';
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$ function mach()
{
echo -e "\nMachine information:" ; uname -a
echo -e "\nUsers logged on:" ; w -h
echo -e "\nCurrent date :" ; date
echo -e "\nMachine status :" ; uptime
echo -e "\nMemory status :" ; free
echo -e "\nFilesystem status :"; df -h
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function ll ()
{
clear;
tput cup 0 0;
ls --color=auto -F --color=always -lhFrt;
tput cup 40 0;
}
$ type ll
ll is a function
ll ()
{
clear;
tput cup 0 0;
ls --color=auto -F --color=always -lhFrt;
tput cup 40 0;
alias ls="ls --color=auto -F"
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
find -mindepth 3 -maxdepth 5 -name passwd
find / -iname "passwd" -exec md5sum {} \;
find . -perm -g=r -type f -exec ls -l {} \;
find . -perm g=r -type f -exec ls -l {} \;
Finding the Top 5 Big Files
find . -type f -exec ls -s {} \; | sort -n -r | head -5
Finding the Top 5 Small Files
find . -type f -exec ls -s {} \; | sort -n | head -5
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Create Alias for Frequent Find Operations
If you find some thing as pretty useful, then you can make it as an alias. And execute it whenever you want.
Remove the files named a.out frequently.
# alias rmao="find . -iname a.out -exec rm {} \;"
# rmao
Remove the core files generated by c program.
# alias rmc="find . -iname core -exec rm {} \;"
# rmc
15. Remove big archive files using find command
The following command removes *.zip files that are over 100M.
# find / -type f -name *.zip -size +100M -exec rm -i {} \;"
Remove all *.tar file that are over 100M using the alias rm100m (Remove 100M). Use the similar concepts and create alias like rm1g, rm2g, rm5g to remove file size greater than 1G, 2G and 5G respectively.
# alias rm100m="find / -type f -name *.tar -size +100M -exec rm -i {} \;"
# alias rm1g="find / -type f -name *.tar -size +1G -exec rm -i {} \;"
# alias rm2g="find / -type f -name *.tar -size +2G -exec rm -i {} \;"
# alias rm5g="find / -type f -name *.tar -size +5G -exec rm -i {} \;"
# rm100m
# rm1g
# rm2g
# rm5g