-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathBash Cookbook=Ron Brash; Note=Erxin.txt
1135 lines (854 loc) · 34.3 KB
/
Bash Cookbook=Ron Brash; Note=Erxin.txt
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Bash cookbook=Ron Brash; Note=Erxin
# Preface
- handle variable assignment
```
#!/bin/bash
PI=3.14
VAR_A=10
VAR_B=$VAR_A
VAR_C=${VAR_B}
echo "Let's print 3 variables:"
echo $VAR_A
echo $VAR_B
echo $VAR_C
echo "We know this will break:"
echo "0. The value of PI is $PIabc" # since PIabc is not declared, it will be empty string
echo "And these will work:"
echo "1. The value of PI is $PI"
echo "2. The value of PI is ${PI}"
echo "3. The value of PI is" $PI
echo "And we can make a new string"
STR_A="Bob"
STR_B="Jane"
echo "${STR_A} + ${STR_B} equals Bob + Jane"
STR_C=${STR_A}" + "${STR_B}
echo "${STR_C} is the same as Bob + Jane too!"
echo "${STR_C} + ${PI}"
```
- global context and environment variables
```
$ env
XDG_VTNR=7
XDG_SESSION_ID=c2
CLUTTER_IM_MODULE=xim
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/rbrash
SESSION=ubuntu
SHELL=/bin/bash
TERM=xterm-256color
XDG_MENU_PREFIX=gnome-
VTE_VERSION=4205
QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1
WINDOWID=81788934
UPSTART_SESSION=unix:abstract=/com/ubuntu/upstart-session/1000/1598
GNOME_KEYRING_CONTROL=
GTK_MODULES=gail:atk-bridge:unity-gtk-module
USER=rbrash
....
```
- reserved keywords programming/scripting languages.
if, elif, else, fi
while, do, for, done, continue, break
case, select, time
function
&, |, >, <, !, =
#, $, (, ), ;, {, }, [, ], \
\ is very special because it is an escape character. Escape characters are used to escape or stop the interpreter
- conditional logic using if, else, and elseif
```
#!/bin/bash
AGE=40
if [ ${AGE} -lt 18 ] then
echo "You must be 18 or older to see this movie"
elif [ "${MY_NAME}" != ${NAME_1}" && "${MY_NAME}" != ${NAME_2}" then
echo ...
else
echo "You may see the movie!"
exit 1
fi
```
operators:
-gt (greater than >)
-ge (greater or equal to >=)
-lt (less than <)
-le (less than or equal to <=)
-eq (equal to)
-nq (not equal to)
operators: &&, ||, ==, and !=
&& (means and)
|| (means or)
== (is equal to)
!= (not equal to)
-n (is not null or is not set)
-z (is null and zero length)
- case statement
```
case $THING_I_AM_TO_EVALUATE in
1) # Condition to evaluate is number 1 (could be "a" for a string too!)
echo "THING_I_AM_TO_EVALUATE equals 1"
;; # Notice that this is used to close this evaluation
*) # * Signified the catchall (when THING_I_AM_TO_EVALUATE does not equal values in the switch)
echo "FALLTHOUGH or default condition"
esac # Close case statement
VAR=10 # Edit to 1 or 2 and re-run, after running the script as is.
case $VAR in
1)
echo "1"
;;
2)
echo "2"
;;
*)
echo "What is this var?"
exit 1
esac
```
- loop
```
#!/bin/bash
FILES=( "file1" "file2" "file3" )
for ELEMENT in ${FILES[@]}
do
echo "${ELEMENT}"
done
echo "Echo\'d all the files"
#!/bin/bash
CTR=1
while [ ${CTR} -lt 9 ]
do
echo "CTR var: ${CTR}"
((CTR++)) # Increment the CTR variable by 1
done
echo "Finished"
#!/bin/bash
CTR=1
until [ ${CTR} -gt 9 ]
do
echo "CTR var: ${CTR}"
((CTR++)) # Increment the CTR variable by 1
done
echo "Finished"
```
- function and parameters
```
#!/bin/bash
function my_function() {
local PARAM_1="$1"
local PARAM_2="$2"
local PARAM_3="$3"
echo "${PARAM_1} ${PARAM_2} ${PARAM_3}"
}
my_function "a" "b" "c"
```
Parameters are referred to systematically like this: $1 for parameter 1, $2 for parameter 2, $3 for parameter 3, and so on
The local keyword refers to the fact that variables declared with this keyword remain accessible only within this function
We can call functions merely by name and use parameters simply by adding them, as in the preceding example
- return code
```
$ ls ~/this.file.no.exist
ls: cannot access '/home/rbrash/this.file.no.exist': No such file or directory
$ echo $?
```
- linking commands and pipe and input/output
``
iwconfig command:
$ iwconfig
wlp3s0 IEEE 802.11abgn ESSID:"127.0.0.1-2.4ghz"
Mode:Managed Frequency:2.412 GHz Access Point: 18:D6:C7:FA:26:B1
Bit Rate=144.4 Mb/s Tx-Power=22 dBm
wireless interface information:
$ iw dev # This will give list of wireless interfaces
$ iw dev wlp3s0 link # This will give detailed information about particular wireless interface
```
three commands to get information from one place to another:
stdin (standard in)
stdout (standard out)
stderr (standard error)
```
$ ls /filethatdoesntexist.txt 2> err.txt
$ ls ~/ > stdout.txt
$ ls ~/ > everything.txt 2>&1 # Gets stderr and stdout
$ ls ~/ >> everything.txt 2>&1 # Gets stderr and stdout
$ cat err.txt
```
f >, 2>, and 2>&1. With the arrows we can redirect the output to any file or even to other programs!
- redirection and pipe bonzanza
Neato.txt will contain the same information as the console
ls -la ~/fakefile.txt ~/ 2>&1 | tee neato.txt
- passing your program flags
code leverages a piece of functionality called getopts. Getopts allows us to grab the program parameter flags for use within our program.
```
#!/bin/bash
HELP_STR="usage: $0 [-h] [-f] [-l] [--firstname[=]<value>] [--lastname[=]<value] [--help]"
# Notice hidden variables and other built-in Bash functionality
optspec=":flh-:"
while getopts "$optspec" optchar; do
case "${optchar}" in
-)
case "${OPTARG}" in
firstname)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
FIRSTNAME="${val}"
;;
lastname)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
LASTNAME="${val}"
;;
help)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
;;
*)
if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" != ":" ]; then
echo "Found an unknown option --${OPTARG}" >&2
fi
;;
esac;;
f)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
FIRSTNAME="${val}"
;;
l)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
LASTNAME="${val}"
;;
h)
echo "${HELP_STR}" >&2
exit 2
;;
*)
if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then
echo "Error parsing short flag: '-${OPTARG}'" >&2
exit 1
fi
;;
esac
done
# Do we have even one argument?
if [ -z "$1" ]; then
echo "${HELP_STR}" >&2
exit 2
fi
# Sanity check for both Firstname and Lastname
if [ -z "${FIRSTNAME}" ] || [ -z "${LASTNAME}" ]; then
echo "Both firstname and lastname are required!"
exit 3
fi
echo "Welcome ${FIRSTNAME} ${LASTNAME}!"
exit 0
```
- get help with man command
# Acting like a typewriter
- logic to modify strings such as the following:
Removing trailing characters
Replacing sections of words (substrings)
Searching for strings in files
Finding files
Testing file types (directory, file, empty, and so on)
Performing small calculations
Limiting the scope of searches or data (filtering)
Modifying the contents of variables (strings inside of string variables)
- ASCII or UTF. ASCII is a commonly used format in the *NIX world on the console. There is also the UTF encoding scheme, which is an improvement upon ASCII
- core commands such as grep, ls, mkdir, touch, traceroute, strings, wget, xargs, and find.
```
#!/bin/bash
# Let's find all the files with the string "Packt"
DIRECTORY="www.packtpub.com/"
SEARCH_TERM="Packt"
# Can we use grep?
grep "${SEARCH_TERM}" ~/* > result1.txt 2&> /dev/null
# Recursive check
grep -r "${SEARCH_TERM}" "${DIRECTORY}" > result2.txt
# What if we want to check for multiple terms?
grep -r -e "${SEARCH_TERM}" -e "Publishing" "${DIRECTORY}" > result3.txt
# What about find?
find "${DIRECTORY}" -type f -print | xargs grep "${SEARCH_TERM}" > result4.txt
# What about find and looking for the string inside of a specific type of content?
find "${DIRECTORY}" -type f -name "*.xml" ! -name "*.css" -print | xargs grep "${SEARCH_TERM}" > result5.txt
# Can this also be achieved with wildcards and subshell?
grep "${SEARCH_TERM}" $(ls -R "${DIRECTORY}"*.{html,txt}) > result6.txt
RES=$?
if [ ${RES} -eq 0 ]; then
echo "We found results!"
else
echo "It broke - it shouldn't happen (Packt is everywhere)!"
fi
# Or for bonus points - a personal favorite
history | grep "ls" # This is really handy to find commands you ran yesterday!
# grep can be used with multiple user-supplied arguments:
$ grep -e "Packt" -e "Publishing" -r ~/www.packtpub.com/
```
$ xargs --help
Usage: xargs [OPTION]... COMMAND [INITIAL-ARGS]...
Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.
$ find --help
Usage: /usr/bin/find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...] [expression]
Default path is the current directory; default expression is -print.
Expression may consist of: operators, options, tests, and actions.
- whildcards and regexes our searches. In short:
A wildcard can be: *, {*.ooh,*.ahh}, /home/*/path/*.txt, [0-10], [!a], ?, [a,p] m
A regex can be: $, ^, *, [], [!], | (be careful to escape this)
enhance grep (or another command), we could use any of the following:
[:alpha:]: Alphabetic (case-insensitive)
[:lower:]: Lowercase printable characters
[:upper:]: Uppercase printable characters
[:digit:]: Numbers in decimal 0 to 9
[:alnum:]: Alphanumeric (all digits and alphabetic characters)
[:space:]: White space meaning spaces, tabs, and newlines
[:graph:]: Printable characters excluding spaces
[:print:]: Printable characters including spaces
[:punct:]: Punctuation (for example, a period)
[:cntrl:]: Control characters (non-printable characters like when a signal is generated when you use Ctrl + C)
[:xdigit:]: Hexadecimal characters
```
#!/bin/bash
STR1='123 is a number, ABC is alphabetic & aBC123 is alphanumeric.'
echo "-------------------------------------------------"
# Want to find all of the files beginning with an uppercase character and end with .pdf?
ls * | grep [[:upper:]]*.pdf
echo "-------------------------------------------------"
# Just all of the directories in your current directory?
ls -l [[:upper:]]*
echo "-------------------------------------------------"
# How about all of the files we created with an expansion using the { } brackets?
ls [:lower:].test .
echo "-------------------------------------------------"
# Files with a specific extension OR two?
echo ${STR1} > test.txt
ls *.{test,txt}
echo "-------------------------------------------------"
# How about looking for specific punctuation and output on the same line
echo "${STR1}" | grep -o [[:punct:]] | xargs echo
echo "-------------------------------------------------"
# How about using groups and single character wildcards (only 5 results)
ls | grep -E "([[:upper:]])([[:digit:]])?.test?" | tail -n 5
```
tr --help
Usage: tr [OPTION]... SET1 [SET2]
Translate, squeeze, and/or delete characters from standard input,
writing to standard output.
- math and calculations in script
$ echo $((1*5))
$ sudo apt-get install -y bc tar
install GCC, which is short for the GNU Compiler Collection. This sounds terribly complex and we assure you that we did all the hard work:
$ sudo apt-get install -y gcc
use gcc and -lm (this refers to libmath) as follows:
$ gcc -Wall -02 -o mhelper main.c -lmath
- Striping/altering/sorting/deleting/searching strings with bash only
```
# ${VARIABLE:startingPosition:optionalLength}
echo ${VARIABLE:3:4}
```
${var} use value of var; braces are optional if var is separated from the following text
${var:-value}, use var if set otherwise use value
${var:=value}, use var if set otherwise use value and assigne value to var
${var:?value}, use var if set otherwise print value and exit
${var:+value}, use value if var is set otherwise using nothing
${#var}, use the length of var
#{#*}, ${#@} use the number of positional parameters
${var#pattern} use value of var after removing text matching pattern
${var##pattern}, same as #pattern but remove the longest matching piece
#{var%pattern}, use value of var after removing text matching pattern from the right, remove the shortest matching piece
${var%%pattern}, same as %pattern, but remove the longest matching piece
${var^pattern}, convert the case of var to uppercase
${var^^pattern}, same as ^pattern but apply the match to every letter in the string
${var, pattern}, same as ^pattern, but convert matching characters to lowercase
${var,, pattern}, but apply the match to every lettern in string
${var@a}, use the flag values representing var's attributes var may be an array subscripted with @ or *
${var@A}, a string in the orm of a command or assignement statement that if evaluated recreates var and its attributes
${var@E}, the value of var wth $'...' escape sequences evaluated
${var@P}, the value of var wth prompt string escape sequences
${var@Q}, the value of var quoted in a way that allows entering the values as input
${!prefix*}, list of variables whose names begin with prefix
${!prefix@}
${var:pos}, starting at position pos(0-based) in variable var
${var:pos:len}, extract len characters, or extract rest of string if no len pos
${var/pat/repl}, use value of var, with first match of pat replaced with repl
${var/pat}, use value of var, with first match of pat deleted
${var//pat/repl}, use value of var, with every match of pat replaced with repl
${var/#pat/repl}, use value o fvar, with match of pat replaced with repl, match must occur at the beginning
${var/%pat/repl}, use valeu of var with match of pat replaced with repl, match must be occur at the end
${!var}, use value f var as name of variable whose value should be used (indirect reference)
$#, number of command-line arguments
$-, options currently ineffect
$?, exit value of last executed command
$$, process number of the shell
$!, process number of last background command
$0, first word the command name. this will have the full pathname if the command was found via PATH search
$n, individual arguments on the command line
$*, $@, all arguments on the command line, $1, $2, ...
"$*", all arguments on the command line as one string
"$@", all arguments on the command line with individual quoted ("$1" "$2")
+ bash automatically set the following addtional variables
$_, temporary variable initialiated to the pathname of the script or program being executed, later stores the last argument of the previous command, also stores the name of the matching mail file
- environment variables
BASH the full path name used to inovke this instance of bash
BASHOPTS a read-only colon-separated list of shell options that are curretly enabled
BASHPID the process id of the current bash process. in some cases this can differ from $$
BASH_AIASES, associative array variable, each element holds an alias defined with the alias command
BASH_ARGC, array variable, each element holds the number of arguments for the corresponding function or dot-script invocation. set only in extended debug mode, with shopt -s extdebug
BASH_ARGV, an array variable similar to BASH_ARGC, each element is noe of te arguments passed to a function or dot-script
BASH_CMDS, associative array variable, command in the internal hash table maintained by the hash command
BASH_COMMAND the command currently executing or about to be executed
BASH_EXECUTION_STRING, the string argument passed tothe -c option
BASH_LINENO, array varialbe, corresponding to BASH_SOURCE and FUNCNAME
BASH_REMATCH, array variable assigned by the =~ operator of the [[]] construct the index zero is the text that matched the entire pattern
BASH_SOURCE, array varialbe containing source filenames
BASH_SUBSHELL
BASH_VERSINFO[0-5]
BASH_VERSION
COMP_CWORD, for programmable completion, index into COMP_WORDS
COMP_KEY, for programmable completion, the key or final key in a sequence
COMP_...
COPROC, array holds the file descriptor used for communicating with an unnamed coprocess
DIRSTACK, array variable, containg the contents of the directory stack as displayed by dirs
EUID, read-only variable with the numeric effective UID
FUNCNAME, array variable, containg function names
FUNCNEST, a value greater than zero defined the maximum function call nesting level
GROUPS, array variable containing the list of numeric group ids
HISTCMD, the history number of the current command
HOSTNAME, the name of the current host
HOSTTYPE, a strig that describes the host system
LINENO, current line number within the script for function
MACHTYPE, a string that describes the host system in the GNU cpu-compay-system format
MAPFILE, default array for the mapfile and readarray commands
OLDPWD, previous working dir
OPTARG, value of argument to last option processed by getopts
OPTIND, numerical index of OPTARG
OSTYPE
PIPESTATUS, array variable, containing the exit status of the commands in the most recent foreground
PPID, process nubmer of this shell's parent
PWD, current working directory
RANDOM[=n], generate a new random number with each reference;start with integer n
READLINE_LINE, for use with bind -x, the contents of the editing buffer are available in this variable
READLINE_POINT, for use with bind -x the index in $READLINE_LINE of the insert point
REPLY default reply used by select and read
SECONDS[=n], number of seconds since the shell was started
SHELLOPTS, a read-only colon-seperated list of shell options
SHLVL, incremented by one every time a new bash
- using sed and awk to remove/replace substrings
awk 'BEGIN { FS=","; OFS="," } {$5="";gsub(",+",",",$0)}1' OFS=, ${EM_CSV}
sed -i 's/Bob/Robert/' ${EM_CSV}
sed -i 's/^/#/' ${EM_CSV} # In place, instead of on the data in the array
```
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-n, --quiet, --silent
suppress automatic printing of pattern space
--debug
annotate program execution
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
--follow-symlinks
follow symlinks when processing in place
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
-b, --binary
open files in binary mode (CR+LFs are not processed specially)
-l N, --line-length=N
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-E, -r, --regexp-extended
use extended regular expressions in the script
(for portability use POSIX -E).
-s, --separate
consider files as separate rather than as a single,
continuous long stream.
--sandbox
operate in sandbox mode (disable e/r/w commands).
-u, --unbuffered
load minimal amounts of data from the input files and flush
the output buffers more often
-z, --null-data
separate lines by NUL characters
--help display this help and exit
--version output version information and exit
If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret. All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.
GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <[email protected]>.
```
sed 's/.$//', and then pipe the output to convert everything to uppercase using sed -e 's/.*/\U&/'.
```
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options: (standard)
-f progfile --file=progfile
-F fs --field-separator=fs
-v var=val --assign=var=val
Short options: GNU long options: (extensions)
-b --characters-as-bytes
-c --traditional
-C --copyright
-d[file] --dump-variables[=file]
-D[file] --debug[=file]
-e 'program-text' --source='program-text'
-E file --exec=file
-g --gen-pot
-h --help
-i includefile --include=includefile
-I --trace
-l library --load=library
-L[fatal|invalid|no-ext] --lint[=fatal|invalid|no-ext]
-M --bignum
-N --use-lc-numeric
-n --non-decimal-data
-o[file] --pretty-print[=file]
-O --optimize
-p[file] --profile[=file]
-P --posix
-r --re-interval
-s --no-optimize
-S --sandbox
-t --lint-old
-V --version
To report bugs, see node `Bugs' in `gawk.info'
which is section `Reporting Problems and Bugs' in the
printed version. This same information may be found at
https://www.gnu.org/software/gawk/manual/html_node/Bugs.html.
PLEASE do NOT try to report bugs by posting in comp.lang.awk,
or by using a web forum such as Stack Overflow.
gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.
Examples:
awk '{ sum += $1 }; END { print sum }' file
awk -F: '{ print $1 }' /etc/passwd
```
- Echo is far more "straightforward", printf can provide the same and more functionality using C style parameters.
```
DECIMAL=10.0
FLOAT=3.333333
FLOAT2=6.6666 # On purpose two missing values
printf "%s %.2f\n\n" "This is two decimal places: " ${DECIMAL}
printf "shall we align: \n\n %.3f %-.6f\n" ${FLOAT} ${FLOAT2}
```
- local will display the current system local language
$ sudo apt-get install -y gettext
xgettext to generate the appropriate strings. We will not use the results, but this is how you can generate a minimalist
>xgettext --help
Usage: xgettext [OPTION] [INPUTFILE]...
Extract translatable strings from given input files.
Mandatory arguments to long options are mandatory for short options too.
Similarly for optional arguments.
- word count
$ wc -c <<< "1234567890"
Using other programs like sort and uniq (or a combination of the two), we can also sort the contents of a file and reduce duplicates
The cut command is a useful command for trimming strings, which may be delimited or merely using hard-coded values such as remove X
$ wc -c testdata/duplicates.txt | cut -d ' ' -f1
- using file attributes with conditional logic
-e: The file exists
-f: This is a regular file and not a directory or device file
-s: The file is not empty or zero in size
-d: This is a directory
-r: This has read permissions
-w: This has write permissions
-x:This has execute permissions
-O: This is the owner of the file the current user
-G: This executes the user if they have the same group as yours
f1 (- nt, -ot, -ef) f2: Refers to if f1 is newer than f2, older than f2, or are hard-linked to the same file
```
function file_attributes() {
if [ ! -s $1 ]; then
echo "\"$1\" is empty"
else
FSIZE=$(stat --printf="%s" $1 2> /dev/null)
RES=$?
if [ $RES -eq 1 ]; then
return
else
echo "\"$1\" file size is: ${FSIZE}\""
fi
fi
if [ ! -O $1 ]; then
echo -e "${USER} is not the owner of \"$1\"\n"
fi
if [ ! -G $1 ]; then
echo -e "${USER} is not among the owning group(s) for \"$1\"\n"
fi
permissions $1 "file"
}
```
- reading delimited data and altered output format
```
# Enter content
echo ${ELM} | \
sed '{:q;N;s/\n/\\n/g;t q}'| \
awk \
'{ print "awk \x27 BEGIN{FS=\"'${DELIMITER}'\"}{print "$0"}\x27 '${INPUT_FILE}'"}' | \
sh >> ${OUTPUT_FILE}
```
- viewing files from various angles
Head: Can be used to output the beginning lines of a file
Tail: Can be used to output the end or tail of a file (continuously as well)
More: A tool used as a pager to view large files page by page/line by line
Less: Is the same as more, but it has more features, including backwards scrolling
$ tail -F /var/log/kern.log
locate (also a sibling of the updatedb command): Used to find files more efficiently using an index of files
find: Used to find files with specific attributes, extensions, and even names within a specific directory
find command are as follows:
-type: This is used to specify the type of file, which can be either file or directory
-delete: This is used to delete files, but may not be present, which means that exec will be required
-name: This is used to specify searching by name functionality
-exec: This is used to specify what else to do upon match
-{a,c,m}time: This is used to search for things such as time of access, creation, and modification
-d, -depth: This is used to specify the depth searching may delve recursively into
-maxdepth: This is used to specify the maximum depth per recursion
-mindepth: This is used to specify the minimum depth when recursively searching
-L, -H, -P: In order, -L follow symbolic links, -H does not follow symbolic links except in specific cases, and -P never follows symbolic links
-print, -print0: These commands are used to print the name of the current file on a standard output
!, -not: This is used to specify logical operations such as match everything, but not on this criteria
-i: This is used to specify user interaction on a match such as -iname test
- create a diff of two files
Diffs in a unified format typically look like this:
$ sudo apt-get install patch diff
$ diff -urN fileA.txt fileB.txt
apply patch
$ patch --verbose --dry-run /etc/updatedb.conf < 001-myfirst-patch-for-updatedb.patch
- creating symbolic links and using them effectively
- tools such as SHA512sum and MD5sum to generate a unique hash
file-splitter.sh
```
#!/bin/bash
FNAME=""
LEN=10
TYPE="line"
OPT_ERROR=0
set -f
function determine_type_of_file() {
local FILE="$1"
file -b "${FILE}" | grep "ASCII text" > /dev/null
RES=$?
if [ $RES -eq 0 ]; then
echo "ASCII file - continuing"
else
echo "Not an ASCII file, perhaps it is Binary?"
fi
}
```
- generating dataset and random files of various size
dd if="inputFile" of="outputFile" bs=1M count=10. From this command, we can see:
if=: Stands for input file
of=: Stands for output file
bs=: Stands for block size
count=: Stands for numbers of blocks to be copied
- using the hexdump command. The hexdump command created a simplified "dump" of all of the bytes inside of the garbage0.bin
# Make a script behave like daemon
- Keeping programs/scripts running after logoff
- Running a program continuously (forever) using looping constructs or recursion
```
recursive_read_input.sh script:
#!/bin/bash
function recursive_func() {
echo -n "Press anything to continue loop "
read input
recursive_func
}
recursive_func
exit 0
#!/bin/bash
for (( ; ; ))
do
echo "Shall run for ever"
sleep 1
done
exit 0
#!/bin/bash
EXIT_PLEASE=0
while : # Notice no conditions?
do
echo "Pres CTRL+C to stop..."
sleep 1
if [ $EXIT_PLEASE != 0 ]; then
break
fi
done
exit 0
```
- Invoking commands when they require permissions
Sudo doesn't activate a root shell or allow you access to other user accounts, which is unlike the su command.
```
# Cmnd alias specification
Cmnd_Alias READ_CMDS = /sbin/halt, /sbin/shutdown
# User privilege specification
root ALL=(ALL:ALL) ALL
bob ALL=(ALL:ALL) NOPASSWD: READ_CMDS
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
```
- Sanitizing user input and for repeatable results
- Making a simple multi-level user menu using select
- Generating and trapping signals for cleanup
SIGHUP (1), SIGINT (2), SIGKILL(9), SIGTERM(15), SIGSTOP(17,18,23), SIGSEGV(12), and SIGUSR1(10)/SIGUSR2(12).
kill command can be used easily as follows:
$ kill -s SIGUSR1 <processID>
$ kill -9 <processID>
$ kill -9 `pidof myprogram.sh`
$$: Which returns the PID of the current script
$?: Which returns the PID of the last job that was sent to the background
$@ :Which returns the array of input variables (for example, $!, $2)
a script itself. Using trap, kill, and signals, we can set timers or alarms (ALRM) to perform clean exits from runaway functions
- network interfaces (NICs) on their computer.
```
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.4 LTS"
NAME="Ubuntu"
VERSION="16.04.4 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=Debian
PRETTY_NAME="Ubuntu 16.04.4 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
```
- gathering network information
```
if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then
echo "IPv4 is up"
else
echo "IPv4 is down"
fi
if ping -q -c 1 -W 1 google.com >/dev/null
then
echo "The network is up"
else
echo "The network is down"
fi
case "$(curl -s --max-time 2 -I http://google.com | sed 's/^[^ ]* *\([0-9]\).*/\1/; 1q')" in
[23]) echo "HTTP connectivity is up";;
5) echo "The web proxy won't let us through";;
*) echo "The network is down or very slow";;
esac
```
- Compressing tools
bzip2, bzip2 is used to compress the files and folder at the same time.
zip, zip compress the files individually and then collects them in a single file.
TAR, file is a collection of different files and directories, which combines them into one file
compress and run the following command:
$ bzip2 filename
compressed individually:
$ zip -r file_name.zip files_dir
two archiving modes:
```
-x: extract an archive
-c: create an archive
-f: FILE name of the archive—you must specify this unless using tape drive for archive
-v: Be verbose, list all files being archived/extracted
-z: Create/extract archive with gzip/gunzip
-j: Create/extract archive with bzip2/bunzip2
-J: Create/extract archive with XZ
```
create a TAR file, use the following command:
$ tar -cvf filename.tar directory/file
$ cat /etc/logrotate.conf
- adding configuration to /etc/logrotate.d
configuration to /etc/logrotate.d/, first open up a new file there:
$ sudo nano /etc/logrotate.d/example-app
run the following command:
$ sudo logrotate /etc/logrotate.conf --debug
create database
$ sqlite3 testdb
ssh-keygen command is used to create a SSH key. Run the command as follows:
$ ssh-keygen
- shell script to run daily, place it in the cron.daily folder
cron folders:
/etc/cron.hourly
/etc/cron.daily
/etc/cron.weekly
/etc/cron.monthly
add the following line in your script:
* * * * * /etc/name_of_cron_folder/script.sh
You can list the existing jobs by running the following command:
$ crontab -l
Run the following command:
$ crontab -e
- The dd command is mainly used for converting and copying files
- mount command. To mount a file system onto the file system tree, use the mount command. This command will instruct the kernel to mount the file system found on a particular device
$ mount -t ext4 /directorytobemounted /directoryinwhichitismounted -o ro,noexec