Skip to content

Commit 0aeb3fa

Browse files
author
sudev
committed
Restructuring
1 parent ac7a582 commit 0aeb3fa

16 files changed

+322
-24
lines changed

README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
Bash Workshop
22
=============
3-
> Learn bash using examples
43

5-
[Handout for participants](https://github.com/fosscell/bashworkshop/blob/master/Bashworkshop.md)
6-
7-
[Finding five longest ping response time](https://github.com/fosscell/bashworkshop/blob/master/five-longest-ping-data.md)
4+
We are trying to make a proper document roadmap for bash scripting so expect for improvements
5+
in the following document.
86

9-
[A simple Dictionary Script](https://github.com/fosscell/bashworkshop/blob/master/dictionary.sh)
7+
All the scripts in this repo will be discussed in coming session.
108

11-
[Implementaion of uniq using bash](https://github.com/fosscell/bashworkshop/blob/master/uniq/uniq.sh)
9+
10+
[Handout for participants](https://github.com/fosscell/bashworkshop/blob/master/Bashworkshop.md)
1211

Scripts/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Scripts
2+
=======
3+
4+
Scripts which should have been discussed during SFD, we will be discussing the same scripts in
5+
the coming session.
6+
7+
You are free to try and understand these scripts.
8+
9+
Documentation will be improved soon.
10+

Scripts/array

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash -xv
2+
array=(`ls`)
3+
len=${#array[*]}
4+
echo "The array has $len items and the sh files in this folder are:"
5+
k=1
6+
i=0
7+
while [ $i -lt $len ]; do
8+
if [[ ${array[$i]} == *."$1" ]]
9+
then
10+
echo "$k: ${array[$i]}"
11+
let k++
12+
fi
13+
let i++
14+
done
15+
echo -e "\nThere are $k files with the extension $1"
16+
17+
array=(`ls`)

Scripts/debug/debug.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Resource
2+
3+
http://binfalse.de/2012/09/howto-debug-bash-scripts/

Scripts/dictionary.sh

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
#A very simple dictionary inside terminal
4+
5+
#Creating a temp folder
6+
dir=~/.dict
7+
8+
#Check for the existence of directory, if not present create one
9+
[[ -d $dir ]] || mkdir $dir
10+
11+
12+
#download respective file from dictionary dot com
13+
# -q => do it quietly ie nothing @ screen
14+
# -O save it as mean
15+
wget -q -O $dir/mean http://dictionary.reference.com/browse/$1
16+
17+
#Please DON'T hard code the value, give it to variable and then use it
18+
file=$dir/mean
19+
20+
#grepping out result
21+
m=$(cat $file | grep description | grep -Po 'content=.*.*See more' | grep -Po '\,.*.\.')
22+
23+
#saving the error code
24+
k=$(echo $?)
25+
26+
#echoing
27+
echo "Meaning of the word "$1" is"$m
28+
29+
#checks if the word was actually available else throws an error
30+
if [[ $k -gt 0 ]];
31+
then
32+
echo ".........oops, cant find word "$1;
33+
fi
34+

Scripts/five-longest-ping-data.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## Find the five longest ping response time from google.com
2+
3+
Lets do it in some steps
4+
5+
Creating a new file
6+
7+
touch tmp
8+
creates a file named tmp in your present working directory
9+
10+
Using ping command
11+
12+
ping google.com
13+
ping command is used to ping a server using ICMP ECHO_REQUESTS to network hosts
14+
15+
Redirecting the ping data and running it in background
16+
17+
ping google.com > tmp &
18+
> is used to redirect the ping data into the text file tmp
19+
& makes the entire process to run in background
20+
21+
Extracting the required data
22+
23+
24+
using awk and cut,
25+
26+
awk '{print $8}' tmp
27+
http://www.grymoire.com/Unix/Awk.html
28+
awk is rather a programing language specilized in manupilating text files
29+
here awk is used to printout only the coloumn no 8 of the text file named 'tmp'
30+
31+
awk '{print $8}' tmp | cut -d “=” -f2
32+
cut - remove sections from each line of files
33+
here we trying to extarct only the numbers
34+
delimiter option -d
35+
field option -f
36+
37+
using grep and cut,
38+
39+
cat tmp | grep -Po 'time.*.ms'
40+
grep is a command used to search for a given pattern inside a text file
41+
text content of the temp file is piped to grep command
42+
-P enabling regex (perl regex)
43+
-o print only matching
44+
45+
cat tmp | grep -Po 'time.*.ms' | cut -d "=" -f 2
46+
47+
48+
cat tmp | grep -Po 'time.*.ms' | cut -d "=" -f 2 | cut -d " " -f 1
49+
50+
51+
Finding the longest
52+
53+
awk '{print $8}' tmp | cut -d “=” -f2 | sort
54+
55+
sort - sort lines of text
56+
57+
awk '{print $8}' tmp | cut -d “=” -f2 | sort | uniq
58+
59+
uniq - removes adjacent duplictes after sort
60+
61+
awk '{print $8}' tmp | cut -d “=” -f2 | sort | uniq | tail -n 5
62+
63+
tail - used to output the last part of file
64+
last n lines using -n option
65+
66+
Or using grep
67+
68+
cat tmp | grep -Po 'time.*.ms' | cut -d "=" -f 2 | cut -d " " -f 1 | sort | uniq | tail -n 5
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+

Scripts/licenser.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#/bin/bash
2+
3+
//A script to add license to top part of all the php files inside a folder
4+
//This script is helpful to a add a part of to large no of files inside a folder
5+
[ -f license ] || echo "can't find the license file required to append license "
6+
7+
for a in *php
8+
do
9+
echo "adding license to $a"
10+
cat license $a > temp
11+
mv temp $a
12+
done
13+

Scripts/pron.sh

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
#A very simple pronunciation tool l
3+
4+
#Creating a temp folder
5+
dir=~/.dict
6+
7+
#Check for the existence if not create one
8+
[[ -d $dir ]] || mkdir $dir
9+
10+
11+
#download respective file from dictionary dot com
12+
# -q => do it quietly ie nothing @ screen
13+
# -O save it as mean
14+
wget -q -O $dir/pron http://en.wiktionary.org/wiki/$1
15+
16+
#Please DON'T hard code the value, give it to variable and then use it
17+
file=$dir/pron
18+
19+
#grepping out result
20+
m=$(cat $file | grep -Po '//upload.*.ogg' | grep -v type)
21+
22+
#saving the error code
23+
k=$(echo $?)
24+
25+
#checks if the word was actually available else throws an error
26+
if [[ $k -gt 0 ]];
27+
then
28+
echo ".........oops, cant find word "$1;
29+
return
30+
fi
31+
32+
#download the file
33+
wget -q -O $dir/$1.ogg http:$m
34+
35+
ans="y"
36+
37+
#while loop
38+
#while [[ $ans =~ [yY].* ]]
39+
while [[ $ans == y ]]
40+
do
41+
#mplayer is one of the default audio player in Linux
42+
mplayer $dir/$1.ogg 2>/dev/null 1>/dev/null
43+
44+
echo "do you want me to play it again [y] / [n] ? "
45+
46+
#read user input
47+
read ans
48+
done
49+
50+
#remove the file from temp folder
51+
rm $dir/$1.ogg
52+
53+
54+
#Exit
55+
echo -e "\n Exiting ...\n"

Scripts/timer.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local N=$(($1*60)); shift
2+
(sleep $N && zenity --info --title="Time's Up" --text="${*:- Times up boy :( $1 minute over}") &
3+
echo "timer set for $1 minutes"
4+
5+

Scripts/uniq/notes

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
start
2+
* test the uniq command
3+
* redirect the test input to uniq
4+
* analyze the output that only the adjacent repeating values are
5+
processed
6+
7+
8+
next step
9+
* show the source code ==> make them write it
10+
* tell them about the basics of looping and conditional statements
11+
* file redirection
12+
* command line arguments ( $1 $@ $2 ...)

Scripts/uniq/test

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
1
2+
2
3+
2
4+
3
5+
4
6+
1
7+
4

Scripts/uniq/uniq.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## bash script to simulate uniq command also to introduce the concept of loops, conditional statements etc.
2+
3+
#!/bin/bash
4+
5+
## to keep track of the previous input line
6+
prev=""
7+
8+
## while loop continues till the whole input is read line by line
9+
while read line
10+
do
11+
## checks if the previous input is same as the current input
12+
if [ "$prev" != "$line" ] && [ "$prev" != "" ]
13+
then
14+
## print the uniq input to standard output
15+
echo $prev
16+
fi
17+
prev=$ine
18+
## updates the prev variable
19+
done < $1 ## giving the file as input
20+
21+
echo $prev ## echo the last uniq line

Scripts/zipo.sh

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash -xv
2+
3+
#Checking if the user has passed any argument
4+
# $# variable outputs the total no of arguments given by the user
5+
(($#)) || { echo -e "Please check your argument \nUsage ./zipo [-x | -c ] <zip-file-name>" >&2; exit 1;}
6+
7+
if [ -f $1 ]; then
8+
9+
#case in bash
10+
case $1 in
11+
*.tar.bz2)
12+
tar xjf $1
13+
;;
14+
*.tar.gz)
15+
tar xzf $1
16+
;;
17+
*.bz2)
18+
bunzip2 $1
19+
;;
20+
*.rar)
21+
rar x $1
22+
;;
23+
*.gz)
24+
gunzip $1
25+
;;
26+
*.tar)
27+
tar xf $1
28+
;;
29+
*.tbz2)
30+
tar xjf $1
31+
;;
32+
*.tgz)
33+
tar xzf $1
34+
;;
35+
*.zip)
36+
unzip $1
37+
;;
38+
*.Z)
39+
uncompress $1
40+
;;
41+
*.7z)
42+
7z x $1
43+
;;
44+
*)
45+
echo "'$1' cannot be extracted via zipo()"
46+
;;
47+
48+
esac;
49+
else
50+
echo "I can't help you with this format";
51+
fi

masterDoc.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ PART I
44
What is a Shell ?
55
=================
66

7-
- its an interpretter to pass commands given by user to the kernel
8-
- commands can be excuted line by line
7+
- its an interpreter to pass commands given by user to the kernel
8+
- commands can be executed line by line
99
- there are different types of shells
1010

1111
Types of Shells
@@ -38,7 +38,7 @@ PART II
3838
where / when / what scripting ?
3939
===============================
4040

41-
- many single line commands can be grouped togather and put into a single file called script
41+
- many single line commands can be grouped together and put into a single file called script
4242
- when the script is an input to a shell its called shell script and here specifically its called bash scripting as the shell is bash
4343
- mostly in interpreted language
4444
- small scale programs/automation
@@ -57,14 +57,14 @@ Why to Write Shell Script ?
5757
Variables:
5858
==========
5959
60-
System Variables and Enviornment
60+
System Variables and Environment
6161
Very commonly used variables maintained by shell, represented with capital letters
6262
env command to get a list of all system variables.
6363
Example:
6464
$HOME
6565
$RANDOM
6666

67-
Loosly typed
67+
Loosely typed
6868
- need not specify data type during variable declaration
6969
Example :
7070
i=5 // i will hold an integer value now
@@ -189,7 +189,7 @@ Shell Arithmetic
189189
operations -gt -lt -eq -ne -ge -le
190190
191191

192-
Commandline Arguements
192+
Command line Arguments
193193
======================
194194
195195
input is given as <script name> arg1 arg2 arg3....

0 commit comments

Comments
 (0)