Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

IP CEIT INICTEL-UNI

India Peru Centre of Excellence in Information


Technology

PROGRAMA DE CERTIFICACIN
PROFESIONAL

Linux
Advanced

Instructor - India Peru Centre of Excellence in Information Technology


BASH (Bourne Again Shell) Scripting
- In BASH scripting language, the instructions (Source Code) is read and
executed by an interpreter(BASH) line by line instead of compiled into an
executable.
- A BASH script contains Linux shell commands, variables and control
structures (sequence, decision, loop)
- First line of the BASH script file starts with Shebang
- #! /bin/bash (Tells the operating system that the following will
be a script and not a regular text file )
- To run a BASH script file
- make it executable : chmod +x script_file
- Start script file : bash script_file or add the path of the
script_file to the PATH environment
user input -
Syntax:
read varname [varname1] or read p varname [varname1]
Where -
1. varname [varname1] is the variable to which the values or
parameter entered by user gets assigned to
2. last variable gets rest of input line
Eg:
#! /bin/sh
read -p "enter your name : x # .. Here x is the variable
echo Welcome $x"
Parameter information
- $0 : Name of the current shell script
- $1 : Positional parameters 1
- $# : The number of positional parameters
- $* : A list of all the parameters, in a single variable, separated
by the first character in the environment variable IFS. If IFS
is modified, then the way $* separates the command line
into parameters will change
- $@ : A subtle variation on $*; it doesnt use the IFS environment
variable, so parameters are not run together even if IFS is
empty.
- $? : Return status of most recently executed command
- $$ : Process id of current bash script running /ran
Eg:
IFS=
set hello hi you
echo $@
hello hi you
echo $*
hellohiyou
unset IFS
echo $*
hello hi you
Bash Control Structures :
- if-then-else
- case
- loops
for
while
until
select
Syntax :
if [ condition ];
then
statements
fi
statements are executed only if condition is true, i.e. has
return status 0
if [ condition ];
then
statements-1
else
statements-2
fi

executes statements-1 if condition is true


executes statements-2 if condition is false
if [ condition-1 ];
then
statements-1
elif [ condition-2 ];
then
statements-2
else
statements-3
fi
executes statements-1 if condition-1 is true
executes statements-2 if condition-2 is true and condition-1 is false
executes statements-3 if condition-1 and condition-2 are false
Syntax:
test expression
[ expression ]
evaluates expression and returns true or false
Eg:
read -p "Do you want to continue?" ans
if test $ans = "y"; then
echo "You entered " $ans
fi
Relational Operators
Logical Expressions :
File Testing Operations:

-d file True if file is a directory


-f file True if file is an ord. file
-r file True if file is readable
-w file True if file is writable
-x file True if file is executable
-s file True if length of file is nonzero
#!/bin/bash
echo "Enter a filename: "
read filename
if [ ! r "$filename" ]
then
echo "File is not read-able"
fi
Case Statement:
use the case statement for a decision that is based on multiple choices
Syntax:
case word in
pattern1) command-list1
;;
pattern2) command-list2
;;
patternN) command-listN
;;
esac
Case Pattern Match:
*
?
[ ]
[:class:]
multiple patterns can be listed via |
Eg:
#!/bin/bash
echo "Enter y or yes to see all files including hidden
files"
echo "Enter n or no to see all non-hidden files"
echo "Enter q to quit"

read -p "Enter your choice: " ans

case $ans in
y|yes) echo "Displaying all (really) files"
ls -a ;;
n|no) echo "Display all non-hidden files..."
ls ;;
q) exit 0 ;;

*) echo "Invalid choice "; exit 1 ;;


esac
Loops :
To execute commands in command-list as long as expression evaluates to true
Syntax:
while [ expression ]
do
command-list
Done
Eg:
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]
do
echo The counter is $COUNTER
let COUNTER=$COUNTER+1
done
To execute commands in command-list as long as expression evaluates to
false
Syntax:
until [ expression ]
do
command-list
done
Eg:
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]
do
echo $COUNTER
let COUNTER-=1
done
To execute commands as many times as the number of words
in the argument-list
Syntax:
for variable in argument-list
do
commands
done
Eg:
#!/bin/bash

for i in 1 2 3 4 5
do
echo $i
done
Constructs simple menu from word list
Allows user to enter a number instead of a word
User enters sequence number corresponding to the word
Syntax:
select WORD in LIST
do
RESPECTIVE-COMMANDS
done
Loops until end of input, i.e. ^d (or ^c)
Interrupt for, while or until loop
The break statement
transfer control to the statement AFTER the done statement
terminate execution of the loop
The continue statement
transfer control to the statement TO the done statement
skip the test statements for the current iteration
continues execution of the loop
Functions :
- A shell function is similar to a shell script
stores a series of commands for execution later
shell stores functions in memory
shell executes a shell function in the same shell that called it
- must be defined before they can be referenced usually placed at
the beginning of the script
- Variables defined within functions are global,
i.e. their values are known throughout the entire shell program
- keyword local inside a function definition makes referenced
variables local to that function
Syntax:
function-name () {
statements
}
Eg:
#!/bin/bash

hello () {
echo Hello World function called."
}
#function call
hello
Debugging is troubleshooting errors that may occur during the execution of a program/script
The following two commands can help you debug a bash shell script:
echo
use explicit output statements to trace execution
Set
The set command is a shell built-in command
has options to allow flow of execution
v option prints each line as it is read
x option displays the command and its arguments
n checks for syntax errors
options can turned on or of
To turn on the option: set -xv
To turn of the options: set +xv
Options can also be set via she-bang line
#! /bin/bash -xv
MySQL
- MySQL is a relational database management system
(RDBMS)
- Installation : yum install mysql-server mysql
Server mysqld
Clients - mysql, mysqladmin
- Set root password
mysqladmin u root password the_password
- Login to mysql database using root user
mysql uroot -p
- Display Databases show databases;
mysql database provides information about mysql users
- Use database named mysql - use mysql;
- Create a database named customer
create database customer;
- Create table amount in a database customer
create table customer.amount (id INT(8) NOT NULL, name
VARCHAR(16) NOT NULL, price INT(8) NOT NULL, type
VARCHAR(16) NOT NULL, email VARCHAR(20) NOT NULL);
or (if inside the database customer) use customer;
create table amount (id INT(8) NOT NULL, name VARCHAR(16)
NOT NULL, price INT(8) NOT NULL, type VARCHAR(16) NOT NULL , email
VARCHAR(20) NOT NULL);
- Display table show tables;
- Insert values into the tables fields(id, name, price, type, email)
INSERT INTO amount (id,name,price,type,email)
VALUES(1,jose",55,liquid,jose@microsoft.com");
- Display the table amount field values
SELECT * FROM amount; (Display all the table field values)
SELECT * FROM amount WHERE id=1; (Display all the table
values where the field (id) value matches 1)
SELECT name FROM amount WHERE id=1; (Display the name field
value from the table values where the field (id) value matches 1)
- Describe information about the table amount in the customer database
describe customer.amount;
- Create an user(jose) in mysql, which has access from
system with IP address 192.168.1.2
CREATE USER jose@l192.168.1.2 IDENTFIFIED BY
the_password;
Verify
SELECT user,host FROM mysql.user;
- Provide full access permission on the database customer to
the user jose from system with IP 192.168.1.2
GRANT ALL PRIVILEGES ON customer.* TO
jose@l192.168.1.2;
- Updating table(amount) fields, replace name jose with marco
UPDATE amount SET name=marco WHERE name=jose;
- Delete the entry from the table(amount) , where name field matches marco
DELETE FROM amount WHERE name=marco;
- Sort values, sort values in ascending order based on field price from the
table amount
SELECT * FROM amount ORDER BY price asc;
- Group values, group values based on the field type
SELECT * FROM amount GROUP BY type;
- Joins (Join two tables based on a common identifier)
Left join (Join table amount_1(right) to table amount(left)
based of common identifier id
SELECT * FROM amount LEFT JOIN amount_1 ON
amount.id=amount_1.id;
- export the database amount to a file mysql_amount.dump
mysqldump uroot p amount > mysql_amount.dump
- export the database amount to a file mysql_amount.dump
mysqldump uroot p amount > mysql_amount.dump
- Import the database amount from the file mysql_amount.dump
mysqldump uroot p amount < mysql_amount.dump
Note : the database amount must exist

You might also like