├── Chapter01
├── script1.txt
├── script10.txt
├── script13.txt
├── script15.txt
├── script16.txt
├── script17.txt
├── script19.txt
├── script2.txt
├── script21.txt
├── script22.txt
├── script23.txt
├── script3.txt
├── script4.txt
├── script5.txt
├── script6.txt
├── script7.txt
├── script8.txt
└── script9.txt
├── Chapter02
├── backup.sh
├── script10.txt
├── script11.txt
├── script13.txt
├── script14.txt
└── script15.txt
├── Chapter03
├── remove_duplicates.txt
├── script16.txt
└── script5.txt
├── Chapter04
├── script1.txt
├── script2.txt
└── script3.txt
├── Chapter05
├── img_downloader.sh
├── script11.txt
├── script2.txt
├── script4.txt
├── script7.txt
└── script8.txt
├── Chapter07
└── script1.txt
├── Chapter08
├── pingTest.txt
├── script2.txt
└── script3.txt
├── Chapter09
├── intruder_detect.sh
├── pcpu.sh
├── script3.txt
└── script4.txt
├── Chapter10
├── script1.txt
├── script3.txt
├── script5.txt
├── script6.txt
├── script7.txt
├── script8.txt
└── script9.txt
├── LICENSE
└── README.md
/Chapter01/script1.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | #Filename: printf.sh
4 |
5 |
6 | printf "%-5s %-10s %-4s\n" No Name Mark
7 |
8 |
9 | printf "%-5s %-10s %-4.2f\n" 1 Sarath 80.3456
10 |
11 |
12 | printf "%-5s %-10s %-4.2f\n" 2 James 90.9989
13 |
14 | printf "%-5s %-10s %-4.2f\n" 3 Jeff 77.564
15 |
--------------------------------------------------------------------------------
/Chapter01/script10.txt:
--------------------------------------------------------------------------------
1 | fname()
2 |
3 | {
4 |
5 |
6 | echo $1, $2; #Accessing arg1 and arg2
7 |
8 |
9 | echo "$@"; # Printing all arguments as list at once
10 |
11 |
12 | echo "$*"; # Similar to $@, but arguments taken as single entity
13 |
14 |
15 | return 0; # Return value
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/Chapter01/script13.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | #Filename: success_test.sh
4 |
5 |
6 | # Evaluate the arguments on the command line - ie success_test.sh 'ls | grep txt'
7 |
8 |
9 | eval $@
10 |
11 |
12 | if [ $? -eq 0 ];
13 |
14 |
15 | then
16 |
17 |
18 | echo "$CMD executed successfully"
19 |
20 |
21 | else
22 |
23 |
24 | echo "$CMD terminated unsuccessfully"
25 |
26 |
27 | fi
28 |
--------------------------------------------------------------------------------
/Chapter01/script15.txt:
--------------------------------------------------------------------------------
1 | repeat()
2 |
3 | {
4 |
5 | while true
6 |
7 |
8 | do
9 |
10 |
11 | $@ && return
12 |
13 |
14 | done
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/Chapter01/script16.txt:
--------------------------------------------------------------------------------
1 | data="name,gender
2 | ,rollno,location"
3 |
4 | # To read each of the item in a variable, we can use IFS.
5 |
6 |
7 | oldIFS=$IFS
8 |
9 |
10 | IFS=, # IFS is now a ,
11 |
12 |
13 | for item in $data;
14 |
15 |
16 | do
17 |
18 |
19 | echo Item: $item
20 |
21 |
22 | done
23 |
24 | IFS=$oldIFS
25 |
--------------------------------------------------------------------------------
/Chapter01/script17.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | #Desc: Illustration of IFS
4 |
5 |
6 | line="root:x:0:0:root:/root:/bin/bash"
7 |
8 |
9 | oldIFS=$IFS;
10 |
11 |
12 | IFS=":"
13 |
14 |
15 | count=0
16 |
17 |
18 | for item in $line;
19 |
20 |
21 | do
22 |
23 |
24 | [ $count -eq 0 ] && user=$item;
25 |
26 |
27 | [ $count -eq 6 ] && shell=$item;
28 |
29 |
30 | let count++
31 |
32 |
33 | done;
34 |
35 |
36 | IFS=$oldIFS
37 |
38 | echo $user\'s shell is $shell;
39 |
--------------------------------------------------------------------------------
/Chapter01/script19.txt:
--------------------------------------------------------------------------------
1 | x=0;
2 |
3 | until [ $x -eq 9 ]; # [ $x -eq 9 ] is the condition
4 |
5 |
6 | do
7 |
8 |
9 | let x++; echo $x;
10 |
11 | done
12 |
--------------------------------------------------------------------------------
/Chapter01/script2.txt:
--------------------------------------------------------------------------------
1 | $ env
2 | PWD=/home/clif/ShellCookBook
3 |
4 | HOME=/home/clif
5 |
6 | SHELL=/bin/bash
7 | # -- And many more lines
8 |
--------------------------------------------------------------------------------
/Chapter01/script21.txt:
--------------------------------------------------------------------------------
1 | fpath="/etc/passwd"
2 |
3 | if [ -e $fpath ]; then
4 |
5 |
6 | echo File exists;
7 |
8 |
9 | else
10 |
11 |
12 | echo Does not exist;
13 |
14 | fi
15 |
--------------------------------------------------------------------------------
/Chapter01/script22.txt:
--------------------------------------------------------------------------------
1 | str1="Not empty "
2 |
3 | str2=""
4 |
5 |
6 | if [[ -n $str1 ]] && [[ -z $str2 ]];
7 |
8 |
9 | then
10 |
11 |
12 | echo str1 is nonempty and str2 is empty string.
13 |
14 | fi
15 |
--------------------------------------------------------------------------------
/Chapter01/script23.txt:
--------------------------------------------------------------------------------
1 | # Define my colors for ls
2 |
3 | LS_COLORS='no=00:di=01;46:ln=00;36:pi=40;33:so=00;35:bd=40;33;01'
4 |
5 | export LS_COLORS
6 |
7 | # My primary prompt
8 |
9 | PS1='Hello $USER'; export PS1
10 |
11 | # Applications I install outside the normal distro paths
12 |
13 | PATH=$PATH:/opt/MySpecialApplication/bin; export PATH
14 |
15 | # Shorthand for commands I use frequently
16 |
17 | function lc
18 | () { /bin/ls -C $* ; }
19 |
--------------------------------------------------------------------------------
/Chapter01/script3.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | #Filename :variables.sh
4 |
5 |
6 | fruit=apple
7 |
8 |
9 | count=5
10 | echo "We have $count ${fruit}(s)"
11 |
--------------------------------------------------------------------------------
/Chapter01/script4.txt:
--------------------------------------------------------------------------------
1 | if [ $UID -ne 0 ]; then
2 |
3 | echo Non root user. Please run as root.
4 |
5 |
6 | else
7 |
8 |
9 | echo Root user
10 |
11 | fi
12 |
--------------------------------------------------------------------------------
/Chapter01/script5.txt:
--------------------------------------------------------------------------------
1 | if test $UID -ne 0
2 |
3 | then
4 |
5 |
6 | echo Non root user. Please run as root
7 |
8 |
9 | else
10 |
11 |
12 | echo Root User
13 |
14 | fi
15 |
--------------------------------------------------------------------------------
/Chapter01/script6.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | #Filename: sleep.sh
4 |
5 |
6 | echo Count:
7 |
8 |
9 | tput sc
10 |
11 |
12 |
13 |
14 | # Loop for 40 seconds
15 |
16 |
17 | for count in `seq 0 40`
18 |
19 |
20 | do
21 |
22 |
23 | tput rc
24 |
25 |
26 | tput ed
27 |
28 |
29 | echo -n $count
30 |
31 |
32 | sleep 1
33 |
34 |
35 | done
36 |
--------------------------------------------------------------------------------
/Chapter01/script7.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | #Filename: debug.sh
4 |
5 |
6 | for i in {1..6};
7 |
8 |
9 | do
10 |
11 |
12 | set -x
13 |
14 |
15 | echo $i
16 |
17 |
18 | set +x
19 |
20 |
21 | done
22 |
23 | echo "Script executed"
24 |
--------------------------------------------------------------------------------
/Chapter01/script8.txt:
--------------------------------------------------------------------------------
1 | construct to iterate from a start to end value, instead of the
2 | seq
3 | command used in the previous example.
4 | This construct is slightly faster than invoking the
5 | seq
6 | command.
7 | 19.
8 | The aforementioned debugging methods
9 | are provided by Bash built-ins. They produce debugging information in a fixed format.
10 | In many cases, we need debugging information in our own format. We can define a _DEBUG environment variable to enable and disable debugging and generate messages
11 | in our own debugging style.
12 |
--------------------------------------------------------------------------------
/Chapter01/script9.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | function DEBUG()
4 |
5 |
6 | {
7 |
8 |
9 | [ "$_DEBUG" == "on" ] && $@ || :
10 |
11 |
12 | }
13 |
14 |
15 | for i in {1..10}
16 |
17 |
18 | do
19 |
20 |
21 | DEBUG echo "I is $i"
22 |
23 | done
24 |
--------------------------------------------------------------------------------
/Chapter02/backup.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # backup.sh
3 | # Backup files with suffix. Do not backup temp files that start with ~
4 | read -p " What folder should be backed up:" folder
5 | read -p " What type of files should be backed up: " suffix
6 | find $folder -name "*.$suffix" -a ! -name '~*' \
7 | -exec cp {} $BACKUP/$LOGNAME/$folder
8 | echo "Backed up files from $folder to $BACKUP/$LOGNAME/$folder"
9 |
--------------------------------------------------------------------------------
/Chapter02/script10.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | #Filename: checkword.sh
4 |
5 | word=$1
6 |
7 | grep "^$1$" /usr/share/dict/british-english -q
8 |
9 | if [ $? -eq 0 ]; then
10 |
11 | echo $word is a dictionary word;
12 |
13 | else
14 |
15 | echo $word is not a dictionary word;
16 |
17 | fi
18 |
--------------------------------------------------------------------------------
/Chapter02/script11.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | #Filename: aspellcheck.sh
4 |
5 | word=$1
6 |
7 | output=`echo \"$word\" | aspell list`
8 |
9 | if [ -z $output ]; then
10 |
11 | echo $word is a dictionary word;
12 |
13 | else
14 |
15 | echo $word is not a dictionary word;
16 |
17 | fi
18 |
--------------------------------------------------------------------------------
/Chapter02/script13.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # backup.sh
4 |
5 | # Backup files with suffix.
6 | Do not backup temp files that start with ~
7 | read -p " What folder should be backed up:" folder
8 |
9 | read -p " What type of files should be backed up: " suffix
10 |
11 | find $folder -name "*.$suffix" -a ! -name '~*' \
12 | -exec cp {} \
13 | $BACKUP/$LOGNAME/$folder
14 |
15 | echo "Backed up files from $folder to $BACKUP/$LOGNAME/$folder"
16 |
--------------------------------------------------------------------------------
/Chapter02/script14.txt:
--------------------------------------------------------------------------------
1 | #Filename: automate_expect.tcl
2 |
3 | spawn ./backup.sh
4 |
5 | expect {
6 |
7 | "*folder*" {
8 |
9 | send "notes\n"
10 |
11 | exp_continue
12 |
13 | }
14 |
15 | "*type*" {
16 |
17 | send "docx\n"
18 |
19 | exp_continue
20 |
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/Chapter02/script15.txt:
--------------------------------------------------------------------------------
1 | #/bin/bash
2 |
3 | #filename: generate_checksums.sh
4 |
5 | PIDARRAY=()
6 |
7 | for file in File1.iso File2.iso
8 |
9 | do
10 |
11 | md5sum $file &
12 |
13 | PIDARRAY+=("$!")
14 |
15 | done
16 |
17 | wait ${PIDARRAY[@]}
18 |
--------------------------------------------------------------------------------
/Chapter03/remove_duplicates.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | #Filename: remove_duplicates.sh
4 |
5 | #Description: Find and remove duplicate files and
6 |
7 | # keep one sample of each file.
8 |
9 | ls -lS --time-style=long-iso | awk 'BEGIN {
10 |
11 | getline; getline;
12 |
13 | name1=$8; size=$5
14 |
15 | }
16 |
17 | {
18 |
19 | name2=$8;
20 |
21 | if (size==$5)
22 |
23 | {
24 |
25 | "md5sum "name1 | getline; csum1=$1;
26 |
27 | "md5sum "name2 | getline; csum2=$1;
28 |
29 | if ( csum1==csum2 )
30 |
31 | {
32 |
33 | print name1; print name2
34 |
35 | }
36 |
37 | };
38 |
39 | size=$5; name1=name2;
40 |
41 | }' | sort -u > duplicate_files
42 |
43 |
44 | cat duplicate_files | xargs -I {} md5sum {} | \
45 |
46 | sort | uniq -w 32 | awk '{ print $2 }' | \
47 |
48 | sort -u > unique_files
49 |
50 | echo Removing..
51 |
52 | comm duplicate_files unique_files -3 | tee /dev/stderr | \
53 | xargs rm
54 |
55 | echo Removed duplicates files successfully.
56 |
57 |
--------------------------------------------------------------------------------
/Chapter03/script16.txt:
--------------------------------------------------------------------------------
1 | $> cat makePan.sh
2 |
3 | # Invoke as: sh makePan.sh OriginalImage.jpg prefix width height xoffset yoffset
4 |
5 | # Clean out any old data
6 |
7 | rm -f tmpFiles
8 |
9 | # Create 200 still images, stepping through the original xoffset and yoffset
10 |
11 | # pixels at a time
12 |
13 | for o in `seq 1 200`
14 |
15 | do
16 |
17 | x=$[ $o+$5 ]
18 |
19 | convert -extract $3x$4+$x+$6 $1 $2_$x.jpg
20 | echo $2_$x.jpg
21 | >> tmpFiles
22 | done
23 |
24 | #Stitch together the image files into a mpg video file
25 |
26 | mencoder mf://@tmpFiles -mf fps=30 -ovc lavc -lavcopts \
27 |
28 | vcodec=msmpeg4v2 -noskip -o $2.mpg
29 |
--------------------------------------------------------------------------------
/Chapter03/script5.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # Filename: filestat.sh
4 |
5 | if [ $# -ne 1 ];
6 |
7 | then
8 |
9 | echo "Usage is $0 basepath";
10 |
11 | exit
12 |
13 | fi
14 |
15 | path=$1
16 |
17 | declare -A statarray;
18 |
19 | while read line;
20 |
21 | do
22 |
23 | ftype=`file -b "$line" | cut -d, -f1`
24 |
25 | let statarray["$ftype"]++;
26 |
27 | done < <(find $path -type f -print)
28 |
29 | echo ============ File types and counts =============
30 |
31 | for ftype in "${!statarray[@]}";
32 |
33 | do
34 |
35 | echo $ftype : ${statarray["$ftype"]}
36 |
37 | done
38 |
39 |
40 | 18.
41 |
42 |
--------------------------------------------------------------------------------
/Chapter04/script1.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | #Filename: silent_grep.sh
4 |
5 | #Desc: Testing whether a file contain a text or not
6 |
7 | if [ $# -ne 2 ]; then
8 |
9 | echo "Usage: $0 match_text filename"
10 |
11 | exit 1
12 |
13 | fi
14 |
15 |
16 |
17 | match_text=$1
18 | filename=$2
19 |
20 | grep -q "$match_text" $filename
21 |
22 |
23 | if [ $? -eq 0 ]; then
24 |
25 | echo "The text exists in the file"
26 |
27 | else
28 |
29 | echo "Text does not exist in the file"
30 |
31 | fi
32 |
--------------------------------------------------------------------------------
/Chapter04/script2.txt:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | #Name: word_freq.sh
4 |
5 | #Desc: Find out frequency of words in a file
6 |
7 | if [ $# -ne 1 ];
8 |
9 | then
10 |
11 | echo "Usage: $0 filename";
12 |
13 | exit -1
14 |
15 | fi
16 |
17 | filename=$1
18 |
19 | egrep -o "\b[[:alpha:]]+\b" $filename | \
20 |
21 | awk '{ count[$0]++ }
22 |
23 | END{ printf("%-14s%s\n","Word","Count") ;
24 |
25 | for(ind in count)
26 |
27 | { printf("%-14s%d\n",ind,count[ind]);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Chapter04/script3.txt:
--------------------------------------------------------------------------------
1 | egrep -o "\b[[:alpha:]]+\b" $filename | tr [A=Z] [a-z] | \
2 |
3 | awk '{ count[$0]++ }
4 |
5 | END{ printf("%-14s%s\n","Word","Count") ;
6 |
7 | for(ind in count)
8 |
9 | { printf("%-14s%d\n",ind,count[ind]);
10 | }
11 | }' | sort
12 |
13 |
--------------------------------------------------------------------------------
/Chapter05/img_downloader.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | #Filename: img_downloader.sh
3 |
4 | if [ $# -ne 3 ];
5 | then
6 | echo "Usage: $0 URL -d DIRECTORY"
7 | exit -1
8 | fi
9 |
10 | while [ $# -gt 0 ]
11 | do
12 | case $1 in
13 | -d) shift; directory=$1; shift;;
14 | *) url=$1; shift;;
15 | esac
16 | done
17 |
18 | echo "URL: $url"
19 | echo "DIR: $directory"
20 |
21 | mkdir -p $directory;
22 | baseurl=$(echo $url | egrep -o "https?://[a-z.\-]+")
23 | echo Downloading $url
24 | curl -s $url | egrep -o "]*>" |
25 | sed 's/
/tmp/$$.list
27 | cd $directory;
28 | while read filename;
29 | do
30 | echo Downloading $filename
31 | curl -s -O "$filename" --silent
32 | done < /tmp/$$.list
33 |
--------------------------------------------------------------------------------
/Chapter05/script11.txt:
--------------------------------------------------------------------------------
1 |
69 |
70 |
71 | EOF1
72 |
73 |
74 | for img in *.jpg;
75 |
76 | do
77 |
78 |
79 | convert "$img" -resize "100x" "thumbs/$img"
80 |
81 |
82 | echo "
83 | " >>index.html
84 |
85 | echo "
86 | " >> index.html
87 |
88 |
89 | done
90 |
91 | cat <