├── Chapter01 ├── hello.sh └── hello1.sh ├── Chapter03 ├── file1 ├── file2 ├── file_1 ├── file_2 ├── love.txt ├── one.txt ├── sample.txt └── two.txt ├── Chapter05 ├── 02_script.sh ├── 03_script.sh ├── array_01.sh ├── array_02.sh ├── default_argument_1.sh ├── default_argument_2.sh ├── export1.sh ├── export2.sh ├── getopt.sh ├── param.sh ├── set_01.sh ├── set_02.sh ├── set_03.sh ├── shift_01.sh └── shift_02.sh ├── Chapter06 ├── debug_01.sh ├── debug_02.sh ├── debug_03.sh ├── debug_04.sh ├── file_01.sh ├── file_02.sh ├── file_03.sh ├── file_04.sh ├── file_05.sh ├── file_06.sh ├── file_07.sh ├── file_08.sh ├── flowers.txt ├── hello.sh ├── here_01.sh ├── here_02.sh ├── here_03.sh ├── here_04.sh ├── here_05.sh ├── here_06.sh ├── here_07.sh ├── here_no.sh ├── here_string01.sh ├── hosts2 ├── hosts3 ├── hostsfinal ├── in_file.txt ├── out_file.txt ├── output.txt ├── read_01.sh ├── read_02.sh ├── read_03.sh ├── read_04.sh └── sample_out.txt ├── Chapter07 ├── arithmetic_01.sh ├── arithmetic_02.sh ├── arithmetic_03.sh ├── arithmetic_04.sh ├── arithmetic_05.sh ├── arithmetic_06.sh ├── arithmetic_07.sh ├── arithmetic_08.sh ├── arithmetic_09.sh └── arithmetic_10.sh ├── Chapter08 ├── File1 ├── File2 ├── case_01.sh ├── case_02.sh ├── case_03.sh ├── case_04.sh ├── case_05.sh ├── file_one ├── file_two ├── for_01.sh ├── for_02.sh ├── for_03.sh ├── for_04.sh ├── for_05.sh ├── for_06.sh ├── for_07.sh ├── for_08.sh ├── for_09.sh ├── for_10.sh ├── for_11.sh ├── for_12.sh ├── for_13.sh ├── for_14.sh ├── for_15.sh ├── for_16.sh ├── if_01.sh ├── if_02.sh ├── if_03.sh ├── if_04.sh ├── if_05.sh ├── if_08.sh ├── if_09.sh ├── if_10.sh ├── if_11.sh ├── if_12.sh ├── if_13.sh ├── if_14.sh ├── if_15.sh ├── if_16.sh ├── if_18.sh ├── if_19.sh ├── log.txt ├── sample.txt ├── select_01.sh ├── select_02.sh ├── select_03.sh ├── select_04.sh ├── select_05.sh ├── test01.sh ├── test02.sh ├── test03.sh ├── test04.sh ├── test05.sh ├── test06.sh ├── test07.sh ├── test08.sh ├── until_01.sh ├── until_02.sh ├── until_03.sh ├── while_01.sh ├── while_02.sh ├── while_03.sh ├── while_04.sh └── while_05.sh ├── Chapter09 ├── function_01.sh ├── function_02.sh ├── function_03.sh ├── function_04.sh ├── function_05.sh ├── function_06.sh ├── function_07.sh ├── function_08.sh ├── function_09.sh ├── function_10.sh ├── function_11.sh ├── function_12.sh ├── function_13.sh ├── function_14.sh ├── function_15.sh ├── function_16.sh └── function_17.sh ├── Chapter11 ├── dialog_01.sh ├── dialog_02.sh ├── dialog_03.sh ├── dialog_04.sh ├── dialog_05.sh ├── trap_01.sh └── trap_02.sh ├── Chapter13 ├── awk_script ├── country.txt ├── new.txt ├── people.txt ├── sample.txt ├── sed_example.txt ├── shopping.txt └── shopping1.txt ├── Chapter14 ├── embed_01.sh ├── embed_02.sh └── embed_03.sh ├── Chapter15 ├── MySQL_Scripts │ ├── mysql_01.sh │ ├── mysql_02.sh │ ├── mysql_03.sh │ ├── mysql_04.sh │ ├── mysql_05.sh │ ├── mysql_06.sh │ ├── mysql_07.sh │ ├── mysql_08.sh │ ├── mysql_09.sh │ ├── mysql_10.sh │ ├── mysql_11.sh │ ├── mysql_12.sh │ ├── mysql_13.sh │ └── mysql_14.sh └── Oracle_shell_scripts │ ├── oracle_01.sh │ ├── oracle_02.sh │ ├── oracle_03.sh │ ├── oracle_04.sh │ ├── oracle_05.sh │ ├── oracle_06.sh │ ├── oracle_07.sh │ └── oracle_08.sh ├── LICENSE └── README.md /Chapter01/hello.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This is comment line 3 | echo "Hello World" 4 | ls 5 | date 6 | -------------------------------------------------------------------------------- /Chapter01/hello1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This is the first Bash shell 3 | # Scriptname : Hello1.sh 4 | # Written by: Ganesh Naik 5 | echo "Hello $LOGNAME, Have a nice day !" 6 | echo "Your are working in directory `pwd`." 7 | echo "You are working on a machine called `uname -n`." 8 | echo "List of files in your directory is." 9 | ls # List files in the present working directory 10 | echo "Bye for now $LOGNAME. The time is `date +%T`!" 11 | -------------------------------------------------------------------------------- /Chapter03/file1: -------------------------------------------------------------------------------- 1 | I go for shopping on Saturday 2 | I rest completely on Sunday 3 | I use Facebook & Tweeter for social networking 4 | -------------------------------------------------------------------------------- /Chapter03/file2: -------------------------------------------------------------------------------- 1 | Today is Monday. 2 | I go for shopping on Saturday 3 | I rest completely on Sunday 4 | I use Facebook & Tweeter for social networking 5 | -------------------------------------------------------------------------------- /Chapter03/file_1: -------------------------------------------------------------------------------- 1 | Barack Obama 2 | David Cameron 3 | Narendra Modi 4 | -------------------------------------------------------------------------------- /Chapter03/file_2: -------------------------------------------------------------------------------- 1 | Barack Obama 2 | Engela Merkel 3 | Vladimir Putin 4 | -------------------------------------------------------------------------------- /Chapter03/love.txt: -------------------------------------------------------------------------------- 1 | Man has love for Art 2 | World is full of love 3 | Love makes world wonderful 4 | love looove lve 5 | love 6 | Love love lover loves 7 | I like "Unix" more than DOS 8 | I love "Unix"/ 9 | I said I love "Unix" 10 | I love "unix" a lot 11 | -------------------------------------------------------------------------------- /Chapter03/one.txt: -------------------------------------------------------------------------------- 1 | 1 India 2 | 2 UK 3 | 3 Canada 4 | 4 US 5 | 5 Ireland 6 | -------------------------------------------------------------------------------- /Chapter03/sample.txt: -------------------------------------------------------------------------------- 1 | Apple Fruit 5 4.5 2 | Potato Vegetable 4 .5 3 | Onion Vegetable .3 8 4 | Guava Fruit 5 1.5 5 | Almonds Nuts 1 16 6 | Tomato Vegetable 3 6 7 | Cashew Nuts 2 12 8 | Mango Fruit 6 6 9 | Watermelon Fruit 5 1 10 | -------------------------------------------------------------------------------- /Chapter03/two.txt: -------------------------------------------------------------------------------- 1 | 1 New Delhi 2 | 2 London 3 | 3 Toronto 4 | 4 Washington 5 | 5 Dublin 6 | -------------------------------------------------------------------------------- /Chapter05/02_script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | planet="Earth" 3 | echo $planet 4 | echo "$planet" 5 | echo '$planet' 6 | echo \$planet 7 | echo Enter some text 8 | read planet 9 | echo '$planet' now equals $planet 10 | exit 0 11 | -------------------------------------------------------------------------------- /Chapter05/03_script.sh: -------------------------------------------------------------------------------- 1 | # Ubuntu Timezone files location : /usr/share/zoneinfo/ 2 | # redhat "/etc/localtime" instead of "/etc/timezone" 3 | # In Redhat 4 | # ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime 5 | export TZ=America/Los_Angeles 6 | echo "Your Timezone is = $TZ" 7 | date 8 | export TZ=Asia/Tokyo 9 | echo "Your Timezone is = $TZ" 10 | date 11 | unset TZ 12 | echo "Your Timezone is = $(cat /etc/timezone)" 13 | # For Redhat or Fedora /etc/localtime 14 | date 15 | -------------------------------------------------------------------------------- /Chapter05/array_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | FRUIT[0]="Pears" 3 | FRUIT[1]="Apple" 4 | FRUIT[2]="Mango" 5 | FRUIT[3]="Banana" 6 | FRUIT[4]="Papaya" 7 | echo "First Index: ${FRUIT[0]}" 8 | echo "Second Index: ${FRUIT[1]}" 9 | -------------------------------------------------------------------------------- /Chapter05/array_02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | FRUIT[0]="Pears" 3 | FRUIT[1]="Apple" 4 | FRUIT[2]="Mango" 5 | FRUIT[3]="Banana" 6 | FRUIT[4]="Papaya" 7 | echo "Method One : ${FRUIT[*]}" 8 | echo "Method Two : ${FRUIT[@]}" 9 | -------------------------------------------------------------------------------- /Chapter05/default_argument_1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | MY_PARAM=${1:-default} 3 | echo $MY_PARAM 4 | -------------------------------------------------------------------------------- /Chapter05/default_argument_2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | variable1=$1 3 | variable2=${2:-$variable1} 4 | echo $variable1 5 | echo $variable2 6 | -------------------------------------------------------------------------------- /Chapter05/export1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | foo="The first variable foo" 3 | export bar="The second variable bar" 4 | ./export2.sh 5 | 6 | -------------------------------------------------------------------------------- /Chapter05/export2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "$foo" 3 | echo "$bar" 4 | -------------------------------------------------------------------------------- /Chapter05/getopt.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | USAGE="usage: $0 -x -y" 3 | while getopts :xy: opt_char 4 | do 5 | case $opt_char in 6 | x) 7 | echo "Option x was called." 8 | ;; 9 | y) 10 | echo "Option y was called. Argument called is $OPTARG" 11 | ;; 12 | \?) 13 | echo "$OPTARG is not a valid option." 14 | echo "$USAGE" 15 | ;; 16 | esac 17 | done 18 | -------------------------------------------------------------------------------- /Chapter05/param.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Total number of parameters are = $#" 3 | echo "Script name = $0" 4 | echo "First Parameter is $1" 5 | echo "Second Parameter is $2" 6 | echo "Third Parameter is $3" 7 | echo "Fourth Parameter is $4" 8 | echo "Fifth Parameter is $5" 9 | echo "All parameters are = $*" 10 | -------------------------------------------------------------------------------- /Chapter05/set_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set USA Canada UK France 3 | echo $1 4 | echo $2 5 | echo $3 6 | echo $4 7 | -------------------------------------------------------------------------------- /Chapter05/set_02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo The date is $(date) 3 | set $(date) 4 | echo The month is $2 5 | exit 0 6 | -------------------------------------------------------------------------------- /Chapter05/set_03.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Executing script $0" 3 | echo $1 $2 $3 4 | set eins zwei drei 5 | echo "One two three in German are:" 6 | echo "$1" 7 | echo "$2" 8 | echo "$3" 9 | textline="name phone address birthdate salary" 10 | set $textline 11 | echo "$*" 12 | echo 'At this time $1 = ' $1 'and $4 = ' $4 13 | -------------------------------------------------------------------------------- /Chapter05/shift_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "All Arguments Passed are as follow : " 3 | echo $* 4 | echo "Shift By one Position :" 5 | shift 6 | echo "Value of Positional Parameter $ 1 after shift :" 7 | echo $1 8 | echo "Shift by Two Positions :" 9 | shift 2 10 | echo "Value of Positional Parameter $ 1 After two Shifts :" 11 | echo $1 12 | -------------------------------------------------------------------------------- /Chapter05/shift_02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo '$#: ' $# 3 | echo '$@: ' $@ 4 | echo '$*: ' $* 5 | echo 6 | echo '$1 $2 $9 $10 are: ' $1 $2 $9 $10 7 | echo 8 | shift 9 | echo '$#: ' $# 10 | echo '$@: ' $@ 11 | echo '$*: ' $* 12 | echo 13 | echo '$1 $2 $9 are: ' $1 $2 $9 14 | shift 2 15 | echo '$#: ' $# 16 | echo '$@: ' $@ 17 | echo '$*: ' $* 18 | echo 19 | echo '$1 $2 $9 are: ' $1 $2 $9 20 | echo '${10}: ' ${10} 21 | -------------------------------------------------------------------------------- /Chapter06/debug_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo -n "Commands in bin directory are : $var" 3 | for var in $(ls ) 4 | do 5 | echo -n -e "$var " 6 | do 7 | # no error if "done" is typed instead of "do" 8 | -------------------------------------------------------------------------------- /Chapter06/debug_02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Hello $LOGNAME" 3 | echo "Today is `date` 4 | echo "Your present working directory is $PWD 5 | echo Good-bye $LOGNAME 6 | -------------------------------------------------------------------------------- /Chapter06/debug_03.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Total number of parameters are = $#" 3 | echo "Script name = $0" 4 | echo "First Parameter is $1" 5 | echo "Second Parameter is $2" 6 | echo "All parameters are = $*" 7 | echo "File names starting with f* in current folder are :" 8 | ls f* 9 | -------------------------------------------------------------------------------- /Chapter06/debug_04.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Commands in bin directory are : $var" 3 | for var in $(ls ) 4 | do 5 | echo -n -e "$var " 6 | do 7 | -------------------------------------------------------------------------------- /Chapter06/file_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # We will open file for writing purpose 3 | # We are assigning descriptor number 3 for file sample_out.txt 4 | exec 3> sample_out.txt 5 | # We are sending output of command "echo" to sample_out.txt file 6 | echo "This is a test message for sample_out.txt file" >&3 7 | # Run command date & store output in file sample_out.txt 8 | date >&3 9 | # Closing file with file descriptor 3 10 | exec 3<&- 11 | -------------------------------------------------------------------------------- /Chapter06/file_02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # We will open file sample_input.txt for reading purpose. 3 | # We are assigning descriptor 3 to the file. 4 | exec 3< sample_input.txt 5 | cat <&3 6 | # Closing file 7 | exec 3<&- 8 | -------------------------------------------------------------------------------- /Chapter06/file_03.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | file_name="sample_out.txt" 3 | # We are assing fd number 3 to file. 4 | # We will be doing read and write operations on file 5 | exec 3<> $file_name 6 | # Writing to file 7 | echo """ 8 | Do not dwell in the past, 9 | do not dream of the future, 10 | concentrate the mind on the present moment. - Buddha 11 | """ >&3 12 | # closing file with fd number 3 13 | exec 3>&- 14 | -------------------------------------------------------------------------------- /Chapter06/file_04.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # We are assigning descriptor 3 to in_file.txt 3 | exec 3< in_file.txt 4 | # We are assigning descriptor 4 to out_file.txt 5 | exec 4> out_file.txt 6 | # We are reading first line of input.txt 7 | read -u 3 line 8 | echo $line 9 | echo "Writing content of in_file.txt to out_file.txt" 10 | echo "Line 1 - $line " >&4 11 | # Closing both the files 12 | exec 3<&- 13 | exec 4<&- 14 | -------------------------------------------------------------------------------- /Chapter06/file_05.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | cp /etc/hosts hosts2 3 | grep -v '^#' hosts2 > hosts3 4 | exec 3< hosts3 # opening hosts3 as input file 5 | exec 4> hostsfinal # opening hostsfinal as output file 6 | read <& 3 address1 name_1 extra_info 7 | read <& 3 address2 name_2 extra_info 8 | echo $name_1 $address1 >& 4 9 | echo $name_2 $address2 >& 4 10 | exec 3<&- # Closing hosts3 11 | exec 4<&- # Closing hostsfinal 12 | -------------------------------------------------------------------------------- /Chapter06/file_06.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # we are assigning file descriptor 3 to input file test.txt 3 | exec 3< test.txt 4 | # we are assigning file descriptor 4 to output.txt 5 | exec 4> output.txt 6 | # we are using read command to read line from file 7 | read -u 3 line 8 | echo "Process id of current process is $$" 9 | my_pid=$$ 10 | echo "Currently following files are opened by $0 script :" 11 | ls -l /proc/$my_pid/fd 12 | # We are closing both files test.txt and output.txt 13 | exec 3<&- 14 | exec 4>&- 15 | -------------------------------------------------------------------------------- /Chapter06/file_07.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Enter the name of file for reading" 3 | read file_name 4 | exec<$file_name 5 | while read var_line 6 | do 7 | echo $var_line 8 | done 9 | -------------------------------------------------------------------------------- /Chapter06/file_08.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | exec 4> output.txt 3 | cat /proc/cpuinfo >&4 4 | exec 3<&- 5 | -------------------------------------------------------------------------------- /Chapter06/flowers.txt: -------------------------------------------------------------------------------- 1 | Rose 2 | Lily 3 | Lotus 4 | Jasmine 5 | -------------------------------------------------------------------------------- /Chapter06/hello.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -n 2 | We have modified shebang line. 3 | -------------------------------------------------------------------------------- /Chapter06/here_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cat << quit 3 | Command is $0 4 | First Argument is $1 5 | Second Argument is $2 6 | quit 7 | -------------------------------------------------------------------------------- /Chapter06/here_02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | sort << EOF 3 | cherry 4 | mango 5 | apple 6 | banana 7 | EOF 8 | -------------------------------------------------------------------------------- /Chapter06/here_03.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wc -w << EOF 3 | There was major earthquake 4 | On April 25, 2015 5 | in Nepal. 6 | There was huge loss of human life in this tragic event. 7 | EOF 8 | -------------------------------------------------------------------------------- /Chapter06/here_04.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # We have used tar utility for archiving home folder on tape 3 | tar -cvf /dev/st0 /home/student 2>/dev/null 4 | # store status of tar operation in variable status 5 | [ $? -eq 0 ] && status="Success" || status="Failed" 6 | # Send email to administrator 7 | mail -s 'Backup status' ganesh@levanatech.com << End_Of_Message 8 | The backup job finished. 9 | End date: $(date) 10 | Status : $status 11 | End_Of_Message 12 | -------------------------------------------------------------------------------- /Chapter06/here_05.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # flowers.txt contains the name of flowers 3 | cat flowers.txt 4 | ed flowers.txt << quit 5 | ,s/Rose/Lily/g 6 | w q 7 | quit 8 | cat flowers.txt 9 | -------------------------------------------------------------------------------- /Chapter06/here_06.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # wall utility is used for sending message to all logged in users 3 | wall << End_Of_Message 4 | Tomorrow, on Friday evening, we will be celebrating 5 | Birthday of few of our colleagues. 6 | All are requested to be present in cafeteria by 3.30 PM. 7 | John 8 | End_Of_Message 9 | echo "Message sent" 10 | -------------------------------------------------------------------------------- /Chapter06/here_07.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Checking number of arguments passed along with command 3 | if [ $# -lt 2 ] 4 | then 5 | echo "Error, usage is:" 6 | echo "ftpget hostname filename [directory]." 7 | exit -1 8 | fi 9 | hostname=$1 10 | filename=$2 11 | directory="." # Default value 12 | if [ $# -ge 3 ] 13 | then 14 | directory=$3 15 | fi 16 | ftp < Apple Mango Grapes Pears Banana Orange Pineapple 3 | for var in `cat fruits.txt` 4 | do 5 | echo "var contains: $var" 6 | done 7 | -------------------------------------------------------------------------------- /Chapter08/for_07.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo -n "Commands in bin directory are : $var" 3 | for var in $(ls /bin/*) 4 | do 5 | echo -n -e "$var \t" 6 | done 7 | -------------------------------------------------------------------------------- /Chapter08/for_08.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for filename in *.c 3 | do 4 | echo "Copying $filename to $filename.bak" 5 | cp $filename $filename.bak 6 | done 7 | -------------------------------------------------------------------------------- /Chapter08/for_09.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for x in 1 2 3 3 | do 4 | echo before $x 5 | continue 6 | echo after $x 7 | done 8 | exit 0 9 | -------------------------------------------------------------------------------- /Chapter08/for_10.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | rm -rf sample* 3 | echo > sample_1 4 | echo > sample_2 5 | mkdir sample_3 6 | echo > sample_4 7 | for file in sample* 8 | do 9 | if [ -d "$file" ] 10 | then 11 | echo "skipping directory $file" 12 | continue 13 | fi 14 | echo file is $file 15 | done 16 | rm -rf sample* 17 | exit 0 18 | -------------------------------------------------------------------------------- /Chapter08/for_11.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for FILE in 'ls *.mp3' 3 | do 4 | if test -e /MP3/$FILE 5 | then 6 | echo "The file $FILE exists." 7 | continue 8 | fi 9 | cp $FILE /MP3 10 | done 11 | -------------------------------------------------------------------------------- /Chapter08/for_12.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | rm -rf sample* 3 | echo > sample_1 4 | echo > sample_2 5 | mkdir sample_3 6 | echo > sample_4 7 | for file in sample* 8 | do 9 | if [ -d "$file" ]; then 10 | break; 11 | fi 12 | done 13 | echo The first directory is $file 14 | rm -rf sample* 15 | exit 0 16 | -------------------------------------------------------------------------------- /Chapter08/for_13.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | typeset -i num=0 3 | while true 4 | do 5 | echo -n "Enter any number (0 to exit): " 6 | read num junk 7 | if (( num == 0 )) 8 | then 9 | break 10 | else 11 | echo "Square of $num is $(( num * num ))." 12 | fi 13 | done 14 | echo "script has ended" 15 | -------------------------------------------------------------------------------- /Chapter08/for_14.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for value in 10 5 27 33 14 25 3 | do 4 | echo $value 5 | done | sort -n 6 | -------------------------------------------------------------------------------- /Chapter08/for_15.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for animal in Tiger Lion Cat Dog 3 | do 4 | echo $animal 5 | sleep 1 6 | done & 7 | -------------------------------------------------------------------------------- /Chapter08/for_16.sh: -------------------------------------------------------------------------------- 1 | #/bin/bash 2 | cities=Delhi:Chennai:Bangaluru:Kolkata 3 | old_ifs="$IFS" # Saving original value of IFS 4 | IFS=":" 5 | for place in $cities 6 | do 7 | echo The name of city is $place 8 | done 9 | -------------------------------------------------------------------------------- /Chapter08/if_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | a=100 3 | if [ $a -eq 100 ] 4 | then 5 | echo "a is equal to $a" 6 | else 7 | echo "a is not equal" 8 | fi 9 | -------------------------------------------------------------------------------- /Chapter08/if_02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Enter the cost of product a" 3 | read a 4 | echo "Enter the cost of product b" 5 | read b 6 | if [ $a -gt $b ] 7 | then 8 | echo " a is greater" 9 | else 10 | echo " b is greater" 11 | fi 12 | -------------------------------------------------------------------------------- /Chapter08/if_03.sh: -------------------------------------------------------------------------------- 1 | echo "Enter the first string to compare" 2 | read name1 3 | echo "Enter the Second string to compare" 4 | read name2 5 | if [ $name1 == $name2 ] 6 | then 7 | echo "First string is equal to Second string" 8 | else 9 | echo "Strings are not same" 10 | fi 11 | -------------------------------------------------------------------------------- /Chapter08/if_04.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | str1="Ganesh" 3 | str2="Naik" 4 | if [ $str1 = $str2 ] 5 | then 6 | echo "Two Strings Are Equal" 7 | fi 8 | if [ $str1 != $str2 ] 9 | then 10 | echo "Two Strings are not equal" 11 | fi 12 | if [ $str1 ] 13 | then 14 | echo "String One Has Size Greater Than Zero" 15 | fi 16 | if [ $str2 ] 17 | then 18 | echo "String Two Has Size Greater Than Zero" 19 | fi 20 | -------------------------------------------------------------------------------- /Chapter08/if_05.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | stty --echo # password will not be printed on screen 3 | read -p "Please enter a password :" password 4 | if test "$password" == "Abrakadabra" 5 | then 6 | echo "Password is matching" 7 | fi 8 | stty echo 9 | -------------------------------------------------------------------------------- /Chapter08/if_08.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | read -p "Enter a user name : " user_name 3 | # try to locate username in in /etc/passwd 4 | # 5 | grep "^$user_name" /etc/passwd > /dev/null 6 | status=$? 7 | if test $status -eq 0 8 | then 9 | echo "User '$user_name' is found in /etc/passwd." 10 | else 11 | echo "User '$user_name' is not found in /etc/passwd." 12 | fi 13 | -------------------------------------------------------------------------------- /Chapter08/if_09.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | df -h | grep /dev/sda1 | cut -c 35-36 > log.txt 3 | read usage < log.txt 4 | if [ $usage -gt 80 ] 5 | then 6 | echo "Warning – Disk file system has exceeded 80% !" 7 | echo "Please move extra data to backup device." 8 | else 9 | echo "Good - You have enough disk space to continue working !" 10 | fi 11 | -------------------------------------------------------------------------------- /Chapter08/if_10.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | read filename 3 | if test -e $filename 4 | then 5 | echo "file exists" 6 | else 7 | echo " file does not exist" 8 | fi 9 | -------------------------------------------------------------------------------- /Chapter08/if_11.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "$1 is: " 3 | if ! [ -e $1 ] 4 | then 5 | echo "..Do not exists" 6 | exit 7 | else 8 | echo "file is present" 9 | fi 10 | if [ -x $1 ] 11 | then 12 | echo "..Executable" 13 | fi 14 | if [ -r $1 ] 15 | then 16 | echo "..Readable" 17 | fi 18 | if [ -w $1 ] 19 | then 20 | echo "..Writable" 21 | fi 22 | -------------------------------------------------------------------------------- /Chapter08/if_12.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | file1="File1" 3 | file2="File2" 4 | if cp $file1 $file2 5 | then 6 | echo "Copy Command Executed Successfully" 7 | echo "Content of file named Fil1 copied in another file named File2" 8 | else 9 | echo "Some problem in command execution" 10 | fi 11 | -------------------------------------------------------------------------------- /Chapter08/if_13.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Enter the first number" 3 | read val_a 4 | echo "Enter the Second number" 5 | read val_b 6 | if [ $val_a == 1 ] && [ $val_b == 10 ] 7 | then 8 | echo "testing is successful" 9 | else 10 | echo "testing is not successful" 11 | fi 12 | -------------------------------------------------------------------------------- /Chapter08/if_14.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | touch file_one 3 | touch file_two 4 | if [ -f "file_one" ] && echo "Hello" && [ -f file_two ] && echo "there" 5 | then 6 | echo "in if" 7 | else 8 | echo "in else" 9 | fi 10 | exit 0 11 | -------------------------------------------------------------------------------- /Chapter08/if_15.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Please enter file name for checking file permissions" 3 | read file 4 | if [[ -r $file && -w $file && -x $file ]] 5 | then 6 | echo "The file has read, write, and execute permission" 7 | fi 8 | -------------------------------------------------------------------------------- /Chapter08/if_16.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | if [ -f file_one ] || echo "Hello" 3 | then 4 | echo "In if" 5 | else 6 | echo "In else" 7 | fi 8 | -------------------------------------------------------------------------------- /Chapter08/if_18.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Kindly enter name of directory : " 3 | read file 4 | if [[ -d $file ]] 5 | then 6 | echo "$file is a directory" 7 | elif [[ -f $file ]] 8 | then 9 | echo "$file is a file." 10 | else 11 | echo "$file is neither a file nor a directory. " 12 | fi 13 | -------------------------------------------------------------------------------- /Chapter08/if_19.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | city=London 3 | if grep "$city" city_database_file >& /dev/null 4 | then 5 | : 6 | else 7 | echo "City is not found in city_database_file " 8 | exit 1 9 | fi 10 | -------------------------------------------------------------------------------- /Chapter08/log.txt: -------------------------------------------------------------------------------- 1 | M 2 | -------------------------------------------------------------------------------- /Chapter08/sample.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-Linux-Shell-Scripting-Second-Edition/52baa290330bce1baa5d468e0766c3d6318d0011/Chapter08/sample.txt -------------------------------------------------------------------------------- /Chapter08/select_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | select var1 in a bc def ghi jkl 3 | do 4 | echo "Present value of var1 is" $var1 5 | done 6 | -------------------------------------------------------------------------------- /Chapter08/select_02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | PS3="please select any one : " 3 | select var in a b quit 4 | do 5 | case $var in 6 | a) echo option is a ;; 7 | b) echo option is b ;; 8 | quit) exit ;; 9 | *) echo option is default ;; 10 | esac 11 | done 12 | -------------------------------------------------------------------------------- /Chapter08/select_03.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | PS3="Please enter one of the option" 3 | select var in 1 2 3 4 4 | do 5 | case $var in 6 | 1) echo "One is selected";; 7 | 2) echo "Two is selected";; 8 | 3) echo "Two is selected";; 9 | 4) echo "Two is selected";; 10 | *) echo "not a proper option";; 11 | esac 12 | done 13 | -------------------------------------------------------------------------------- /Chapter08/select_04.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | PS3="Please select one of the above:" 3 | select COMPONENT in comp1 comp2 comp3 all none 4 | do 5 | case $COMPONENT in 6 | comp1|comp2|comp3) echo "comp1 or comp2 or comp3 selected" ;; 7 | all) echo "selected all" 8 | ;; 9 | none) break ;; 10 | *) echo "ERROR: Invalid selection, $REPLY." ;; 11 | esac 12 | done 13 | -------------------------------------------------------------------------------- /Chapter08/select_05.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | PS3="Enter the number for your fruit choice: " 3 | select fruit in apple orange banana peach pear "Quit Menu" 4 | do 5 | case $fruit in 6 | apple) 7 | echo "An apple has 80 calories." 8 | ;; 9 | orange) 10 | echo "An orange has 65 calories." 11 | ;; 12 | banana) 13 | echo "A banana has 100 calories." 14 | ;; 15 | peach) 16 | echo "A peach has 38 calories." 17 | ;; 18 | pear) 19 | echo "A pear has 100 calories." 20 | ;; 21 | "Quit Menu") 22 | break 23 | ;; 24 | *) 25 | echo "You did not enter a correct choice." 26 | ;; 27 | esac 28 | done 29 | -------------------------------------------------------------------------------- /Chapter08/test01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | str1="Ganesh"; 3 | str2="Mumbai"; 4 | str3= 5 | [ $str1 = $str2 ] # Will Check Two Strings Are Equal Or Not 6 | echo $? 7 | [ $str1 != $str2 ] # Will Check Two Strings Are Not Equal 8 | echo $? 9 | [ -n $str1 ] # Will confirm string length is greater than zero 10 | echo $? 11 | [ -z $str3 ] # Will Confirm length of String is Zero 12 | echo $? 13 | -------------------------------------------------------------------------------- /Chapter08/test02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Enter First name" 3 | read name1 4 | echo "Enter Second name" 5 | read name2 6 | [ $name1 = $name2 ] # Check equality of two names 7 | echo $? 8 | [ -n $name2 ] # Check String Length is greater than Zero 9 | echo $? 10 | -------------------------------------------------------------------------------- /Chapter08/test03.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | num1=10 3 | num2=30 4 | echo $(($num1 < $num2)) # compare for less than 5 | [ $num1 -lt $num2 ] # compare for less than 6 | echo $? 7 | [ 139 ] 8 | [ $num1 -ne $num2 ] # compare for not equal 9 | echo $? 10 | [ $num1 -eq $num2 ] # compare for equal to 11 | echo $? 12 | -------------------------------------------------------------------------------- /Chapter08/test04.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Please enter 1st First Number" 3 | read num1 4 | echo "Please enter 2nd Number" 5 | read num2 6 | echo "Please enter 3rd Number" 7 | read num3 8 | [[ $num1 > $num2 ]] # compare for greater than 9 | echo $? 10 | [[ $num1 != $num2 ]] # compare for not equal to 11 | echo $? 12 | [[ $num2 == $num3 ]] # compare for equal to 13 | echo $? 14 | [[ $num1 && $num2 ]] # Logical And Operation 15 | echo $? 16 | [[ $num2 || $num3 ]] # Logical OR Operation 17 | echo $? 18 | -------------------------------------------------------------------------------- /Chapter08/test05.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | Var1=20 3 | Var2=30 4 | Str1="Accenture" 5 | FileName="TestStringOperator" 6 | test $Var1 -lt $Var2 # Test for Less Than 7 | echo $? 8 | test $Var1 -gt $Var2 # Test For Greater Than 9 | echo $? 10 | test -n $Str1 # Test for String Having Length Greater Than 0 11 | echo $? 12 | test -f $FileName # Test for File Attributes 13 | echo $? 14 | -------------------------------------------------------------------------------- /Chapter08/test06.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Please enter 1st Number" 3 | read num1 4 | echo "Please enter 2nd Number" 5 | read num2 6 | echo 7 | test $num1 -eq $num2 # Test for Equal 8 | echo $? 9 | test $num1 -ne $num2 # Test for Not Equal 10 | echo $? 11 | test $num1 -ge $num2 # Test for Greater Than Equal 12 | echo $? 13 | echo "Please enter 1st String" 14 | read Str1 15 | echo "Please enter 2nd String" 16 | read Str2 17 | test $Str1 = $Str2 # Test for Two Strings Are Equal 18 | echo $? 19 | test -z $Str1 # Test for The Length Of The String Is > 0 20 | echo $? 21 | test $Str2 # Test for The String Is Not NULL 22 | echo $? 23 | -------------------------------------------------------------------------------- /Chapter08/test07.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Check if file is Directory 3 | [ -d work ] 4 | echo $? 5 | # Check that is it a File 6 | [ -f test.txt ] 7 | echo $? 8 | # Check if File has size greater than 0 9 | [ -s test.txt ] 10 | echo $? 11 | -------------------------------------------------------------------------------- /Chapter08/test08.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Check if File has Read Permission 3 | [ -r File2 ] 4 | echo $? 5 | # Check if File Has Write Permission 6 | [ -w File2 ] 7 | echo $? 8 | # Check if File Has Execute Permission 9 | [ -x File2 ] 10 | echo $? 11 | -------------------------------------------------------------------------------- /Chapter08/until_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | x=0 3 | until [ $x -eq 10 ] 4 | do 5 | echo $x 6 | x=`expr $x + 1` 7 | done 8 | -------------------------------------------------------------------------------- /Chapter08/until_02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | INPUT="" 3 | until [ "$INPUT" = quit ] 4 | do 5 | echo "" 6 | echo 'Enter a word (quit to exit) : ' 7 | read INPUT 8 | echo "You typed : $INPUT" 9 | done 10 | -------------------------------------------------------------------------------- /Chapter08/until_03.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | until who | grep "$1" > /dev/null 3 | do 4 | sleep 60 5 | done 6 | echo -e \\a 7 | echo "***** $1 has just logged in *****" 8 | exit 0 9 | -------------------------------------------------------------------------------- /Chapter08/while_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | file=/etc/resolv.conf 3 | while IFS= read -r line # IFS : inter field separator 4 | do 5 | # echo line is stored in $line 6 | echo $line 7 | done < "$file" 8 | -------------------------------------------------------------------------------- /Chapter08/while_02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | declare -i x 3 | x=0 4 | while [ $x -le 10 ] 5 | do 6 | echo $x 7 | x=$((x+1)) 8 | done 9 | -------------------------------------------------------------------------------- /Chapter08/while_03.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | INPUT="" 3 | while [ "$INPUT" != quit ] 4 | do 5 | echo "" 6 | echo 'Enter a word (quit to exit) : ' 7 | read INPUT 8 | echo "You typed : $INPUT" 9 | done 10 | -------------------------------------------------------------------------------- /Chapter08/while_04.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | num=1 3 | while (( num < 6 )) 4 | do 5 | echo "The value of num is: $num" 6 | (( num = num + 1 )) # let num=num+1 7 | done 8 | echo "Done." 9 | -------------------------------------------------------------------------------- /Chapter08/while_05.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | count=1 3 | num=1 4 | while [ $count -le $1 ] 5 | do 6 | echo $num 7 | num=`expr $num + 2` 8 | count=`expr $count + 1` 9 | done 10 | -------------------------------------------------------------------------------- /Chapter09/function_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | hello() 3 | { echo "Executing function hello" 4 | } 5 | echo "Script has started now" 6 | hello 7 | echo "Script will end" 8 | -------------------------------------------------------------------------------- /Chapter09/function_02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | function greet() 3 | { echo "Hello $LOGNAME, today is $(date)"; } 4 | greet 5 | -------------------------------------------------------------------------------- /Chapter09/function_03.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | function_lister () 3 | { 4 | echo Your present working directory is `pwd` 5 | echo Your files are: 6 | ls 7 | } 8 | function_lister 9 | -------------------------------------------------------------------------------- /Chapter09/function_04.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # pause: causes a script to take a break 3 | pause() 4 | { 5 | echo "To continue, hit RETURN." 6 | read q 7 | } 8 | pause 9 | -------------------------------------------------------------------------------- /Chapter09/function_05.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | yesterday() 3 | { 4 | date --date='1 day ago' 5 | } 6 | yesterday 7 | -------------------------------------------------------------------------------- /Chapter09/function_06.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | function Convert_Upper() 3 | { 4 | echo $1 | tr 'abcdefghijklmnopqrstuvwxyz' \ 5 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 6 | } 7 | Convert_Upper "ganesh naik - embedded android and linux training" 8 | -------------------------------------------------------------------------------- /Chapter09/function_07.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | quit() 3 | { 4 | exit 5 | } 6 | ex() 7 | { 8 | echo $1 $2 $3 9 | } 10 | ex Hello hi bye # Function ex with three arguments 11 | ex World # Function ex with one argument 12 | echo $1 # First argument passed to script 13 | echo $2 # Second argument passed to script 14 | echo $3 # Third argument passed to script 15 | quit 16 | echo foo 17 | -------------------------------------------------------------------------------- /Chapter09/function_08.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | countries() 3 | { 4 | # let us store first argument $1 in variable temp 5 | temp=$1 6 | echo "countries(): \$0 = $0" # print command 7 | echo "countries(): \$1 = $1" # print first argument 8 | echo "countries(): total number of args passed = $#" 9 | echo "countries(): all arguments (\$*) passed = -\"$*\"" 10 | } 11 | # Call function with one argument 12 | echo "Calling countries() for first time" 13 | countries USA 14 | # Call function with three arguments 15 | echo "Calling countries() second time " 16 | countries USA India Japan 17 | -------------------------------------------------------------------------------- /Chapter09/function_09.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # mcd: mkdir + cd; creates a new directory and 3 | # changes into that new directory 4 | mcd () 5 | { 6 | mkdir $1 7 | cd $1 8 | } 9 | mcd test1 10 | -------------------------------------------------------------------------------- /Chapter09/function_10.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | yesno ( ) 3 | { 4 | while true . 5 | do 6 | echo "$*" 7 | echo "Please answer by entering yes or no : " 8 | read reply 9 | case $reply in 10 | yes) 11 | echo "You answered Yes" 12 | return 0 13 | ;; 14 | no) 15 | echo "You answered No" 16 | return 1 17 | ;; 18 | * ) 19 | echo "Invalid input" 20 | ;; 21 | esac 22 | done 23 | } 24 | yesno 25 | -------------------------------------------------------------------------------- /Chapter09/function_11.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # We will define variable temp for sharing data with function 3 | temp="/temp/filename" 4 | remove_file() 5 | { 6 | echo "removing file $temp..." 7 | } 8 | remove_file 9 | -------------------------------------------------------------------------------- /Chapter09/function_12.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | name="John" 3 | hello() 4 | { name="Maya" 5 | echo $name 6 | } 7 | echo $name # name contains John 8 | hello # name contains Maya 9 | echo $name # name contains Maya 10 | -------------------------------------------------------------------------------- /Chapter09/function_13.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | name="John" 3 | hello() 4 | { local name="Mary" 5 | echo $name 6 | } 7 | echo $name # name contains John 8 | hello # name contains Mary 9 | echo $name # name contains John 10 | -------------------------------------------------------------------------------- /Chapter09/function_14.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | is_user_root() { [ $(id -u) -eq 0 ]; } 3 | is_user_root && echo "You are root user, you can go ahead."\ 4 | || echo "You need to be administrator to run this script" 5 | -------------------------------------------------------------------------------- /Chapter09/function_15.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | declare -r TRUE=0 3 | declare -r FALSE=1 4 | is_user_root() 5 | { 6 | [ $(id -u) -eq 0 ] && return $TRUE || return $FALSE 7 | } 8 | is_user_root && echo "You can continue" || echo "You need to be root to run this script." 9 | -------------------------------------------------------------------------------- /Chapter09/function_16.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | yes_or_no() 3 | { 4 | echo "Is your name $*?" 5 | while true 6 | do 7 | echo -n "Please reply yes or no :" 8 | read reply 9 | case $reply in 10 | Y | y | yes ) return 0;; 11 | N | n | no ) return 1;; 12 | *) echo "Invalid answer" 13 | esac 14 | done 15 | } 16 | if yes_or_no $1 17 | then 18 | echo "Hello $1" 19 | else 20 | echo "name not provided" 21 | fi 22 | -------------------------------------------------------------------------------- /Chapter09/function_17.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dobackup() 3 | { 4 | echo "Started backup" 5 | tar -zcvf /dev/st0 /home >/dev/null 2>& 1 6 | echo "Completed backup" 7 | } 8 | dobackup & 9 | echo -n "Task...done." 10 | echo 11 | -------------------------------------------------------------------------------- /Chapter11/dialog_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dialog --title "Delete file" \ 3 | --backtitle "Learning Dialog Yes-No box" \ 4 | --yesno "Do you want to delete file \"~/work/sample.txt\"?" 7 60 5 | # Selecting "Yes" button will return 0. 6 | # Selecting "No" button will return 1. 7 | # Selecting [Esc] will return 255. 8 | result=$? 9 | case $result in 10 | 0) rm ~/work/sample.txt 11 | echo "File deleted.";; 12 | 1) echo "File could not be deleted.";; 13 | 255) echo "Action Cancelled – Presssed [ESC] key.";; 14 | esac 15 | -------------------------------------------------------------------------------- /Chapter11/dialog_02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | result="output.txt" 3 | >$ $result # Create empty file 4 | dialog --title "Inputbox Demo" \ 5 | --backtitle "Learn Shell Scripting" \ 6 | --inputbox "Please enter your name " 8 60 2>$result 7 | response=$? 8 | name=$(<$result) 9 | case $response in 10 | 0) echo "Hello $name" 11 | ;; 12 | 1) echo "Cancelled." 13 | ;; 14 | 255) echo "Escape key pressed." 15 | esac 16 | rm $result 17 | -------------------------------------------------------------------------------- /Chapter11/dialog_03.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # creating the file to store password 3 | result="output.txt 2>/dev/null" 4 | # delete the password stored file, if program is exited pre-maturely. 5 | trap "rm -f output.txt" 2 15 6 | dialog --title "Password" \ 7 | --insecure \ 8 | --clear \ 9 | --passwordbox "Please enter password" 10 30 2> $result 10 | reply=$? 11 | case $reply in 12 | 0) echo "You have entered Password : $(cat $result)";; 13 | 1) echo "You have pressed Cancel";; 14 | 255) cat $data && [ -s $data ] || echo "Escape key is pressed.";; 15 | esac 16 | -------------------------------------------------------------------------------- /Chapter11/dialog_04.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Declare file to store selected menu option 3 | RESPONSE=menu.txt 4 | # Declare file to store content to display date and cal output 5 | TEMP_DATA=output.txt 6 | vi_editor=vi 7 | # trap and delete temp files 8 | trap "rm $TEMP_DATA; rm $RESPONSE; exit" SIGHUP SIGINT SIGTERM 9 | function display_output(){ 10 | dialog --backtitle "Learning Shell Scripting" --title "Output" 11 | --clear --msgbox "$(<$TEMP_DATA)" 10 41 12 | } 13 | function display_date(){ 14 | echo "Today is `date` @ $(hostname -f)." >$TEMP_DATA 15 | display_output 6 60 "Date and Time" 16 | } 17 | function display_calendar(){ 18 | cal >$TEMP_DATA 19 | display_output 13 25 "Calendar" 20 | } 21 | # We are calling infinite loop here 22 | while true 23 | do 24 | # Show main menu 25 | dialog --help-button --clear --backtitle "Learn Shell Scripting" \ 26 | --title "[ Demo Menubox ]" \ 27 | --menu "Please use up/down arrow keys, number keys\n\ 28 | 1,2,3.., or the first character of choice\n\ 29 | as hot key to select an option" 15 50 4 \ 30 | Calendar "Show the Calendar" \ 31 | Date/time "Show date and time" \ 32 | Editor "Start vi editor" \ 33 | Exit "Terminate the Script" 2>"${RESPONSE}" 34 | menuitem=$(<"${RESPONSE}") 35 | # Start activity as per selected choice 36 | case $menuitem in 37 | Calendar) display_calendar;; 38 | Date/time) display_date;; 39 | Editor) $vi_editor;; 40 | Exit) echo "Thank you !"; break;; 41 | esac 42 | done 43 | # Delete temporary files 44 | [ -f $TEMP_DATA ] && rm $TEMP_DATA 45 | [ -f $RESPONSE ] && rm $RESPONSE 46 | 47 | -------------------------------------------------------------------------------- /Chapter11/dialog_05.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | declare -i COUNTER=1 3 | { 4 | while test $COUNTER -le 100 5 | do 6 | echo $COUNTER 7 | COUNTER=COUNTER+1 8 | sleep 1 9 | done 10 | } | dialog --gauge "This is a progress bar" 10 50 0 11 | -------------------------------------------------------------------------------- /Chapter11/trap_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | trap "echo caught signal SIGINT" SIGINT 3 | trap "echo caught signal SIGQUIT" 3 4 | trap "echo caught signal SIGTERM" 15 5 | while : 6 | do 7 | sleep 50 8 | done 9 | -------------------------------------------------------------------------------- /Chapter11/trap_02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | trap "echo caught signal SIGINT" SIGINT 3 | trap "echo caught signal SIGQUIT" 3 4 | trap "echo caught signal SIGTERM" 15 5 | trap "echo caught signal SIGTSTP" TSTP 6 | echo "Enter any string (type 'bye' to exit)." 7 | while true 8 | do 9 | echo "Rolling...\c" 10 | read string 11 | if [ "$string" = "bye" ] 12 | then 13 | break 14 | fi 15 | done 16 | echo "Exiting normally" 17 | -------------------------------------------------------------------------------- /Chapter13/awk_script: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | do { 3 | ++x 4 | print x 5 | } while ( x <= 4 ) 6 | } 7 | -------------------------------------------------------------------------------- /Chapter13/country.txt: -------------------------------------------------------------------------------- 1 | Country Capital ISD Code 2 | USA Washington 1 3 | China Beijing 86 4 | Japan Tokyo 81 5 | India Delhi 91 6 | -------------------------------------------------------------------------------- /Chapter13/new.txt: -------------------------------------------------------------------------------- 1 | Chicken 3 5 15 2 | -------------------------------------------------------------------------------- /Chapter13/people.txt: -------------------------------------------------------------------------------- 1 | Bill Thomas 8000 08/9/1968 2 | Fred Martin 6500 22/7/1982 3 | Julie Moore 4500 25/2/1978 4 | Marie Jones 6000 05/8/1972 5 | Tom Walker 7000 14/1/1977 6 | -------------------------------------------------------------------------------- /Chapter13/sample.txt: -------------------------------------------------------------------------------- 1 | Happy Birth Day 2 | We should live every day. 3 | -------------------------------------------------------------------------------- /Chapter13/sed_example.txt: -------------------------------------------------------------------------------- 1 | List of Fruits 2 | Mango 3 | Apple 4 | apple 5 | Apple 6 | Watermelon 7 | Banana 8 | Guava 9 | Pineapple 10 | Strawberry 11 | -------------------------------------------------------------------------------- /Chapter13/shopping.txt: -------------------------------------------------------------------------------- 1 | Product Quantity Unit_Price Total_Cost 2 | Apple 2 3 6 3 | Orange 2 .8 1.6 4 | Papaya 2 1.5 3 5 | Chicken 3 5 15 6 | Cashew 1 10 10 7 | -------------------------------------------------------------------------------- /Chapter13/shopping1.txt: -------------------------------------------------------------------------------- 1 | Product Quantity Unit_Price 2 | Apple 200 3 3 | Orange 200 .8 4 | Papaya 100 1.5 5 | Chicken 65 5 6 | Cashew 50 10 7 | April, third week 8 | -------------------------------------------------------------------------------- /Chapter14/embed_01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | function now_date_time 3 | { 4 | python - <