You are on page 1of 32

IP CEIT INICTEL-UNI

India Peru Centre of Excellence in Information
Technology

PROGRAMA DE CERTIFICACIÓN
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"

$0 : Name of the current shell script . . then the way $* separates the command line into parameters will change .$$ : Process id of current bash script running /ran . separated by the first character in the environment variable IFS.$* : A list of all the parameters.$? : Return status of most recently executed command . so parameters are not run together even if IFS is empty.Parameter information – .$@ : A subtle variation on $*.$# : The number of positional parameters . If IFS is modified.$1 : Positional parameters 1 . it doesn’t use the IFS environment variable. in a single variable.

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 .

e.Syntax : if [ condition ]. has return status “0” . then statements fi statements are executed only if condition is true. i.

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 .

then echo "You entered " $ans fi .Syntax: test expression [ expression ] evaluates ‘expression’ and returns true or false Eg: read -p "Do you want to continue?" ans if test $ans = "y".

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 .

esac . pattern2) command-list2 .Case Statement: use the case statement for a decision that is based on multiple choices Syntax: case word in pattern1) command-list1 ... patternN) command-listN ..

Case Pattern Match: * ? [ … ] [:class:] multiple patterns can be listed via | .

exit 1 ... *) echo "Invalid choice ".. n|no) echo "Display all non-hidden files. esac .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 ." ls .... q) exit 0 .

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. ^d (or ^c) .e. i.

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 .Interrupt for.

keyword “local” inside a function definition makes referenced variables “local” to that function .e. their values are known throughout the entire shell program .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 .Functions : .must be defined before they can be referenced usually placed at the beginning of the script .Variables defined within functions are global. i.

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 .

Set root password – mysqladmin –u root password ‘the_password’ .Login to mysql database using root user – mysql –uroot -p . MySQL . mysqladmin .MySQL is a relational database management system (RDBMS) .mysql.Installation : yum install mysql-server mysql Server – mysqld Clients .

or (if inside the database customer) use customer. create table amount (id INT(8) NOT NULL.Use database named mysql . price INT(8) NOT NULL..Create a database named customer – create database customer. name VARCHAR(16) NOT NULL.Create table amount in a database customer – create table customer. price INT(8) NOT NULL.Display Databases – show databases. . mysql database provides information about mysql users . . name VARCHAR(16) NOT NULL.use mysql. type VARCHAR(16) NOT NULL .amount (id INT(8) NOT NULL. email VARCHAR(20) NOT NULL). type VARCHAR(16) NOT NULL. email VARCHAR(20) NOT NULL). .

email) INSERT INTO amount (id. type.55.type.price.name. .Insert values into the tables fields(id. (Display all the table values where the field (id) value matches 1) SELECT name FROM amount WHERE id=1.amount.“jose".Display the table amount field values SELECT * FROM amount.Describe information about the table amount in the customer database describe customer.email) VALUES(1. (Display all the table field values) SELECT * FROM amount WHERE id=1. price.com"). name.”liquid”. .“jose@microsoft.. (Display the name field value from the table values where the field (id) value matches 1) . .Display table – show tables.

2 GRANT ALL PRIVILEGES ON customer.1.Create an user(jose) in mysql.168. . .168.1.2 CREATE USER ‘jose’@’l192.2’ IDENTFIFIED BY ‘the_password’.Provide full access permission on the database customer to the user jose from system with IP 192.* TO ‘jose’@’l192.168.host FROM mysql.user.2’..168.1. which has access from system with IP address 192. Verify SELECT user.1.

Updating table(amount) fields. . group values based on the field type SELECT * FROM amount GROUP BY type.Sort values. replace name jose with marco UPDATE amount SET name=“marco” WHERE name=“jose”. . ..Group values. . where name field matches marco DELETE FROM amount WHERE name=“marco”.Delete the entry from the table(amount) . sort values in ascending order based on field price from the table amount SELECT * FROM amount ORDER BY price asc.

Import the database amount from the file mysql_amount.dump . .dump mysqldump –uroot –p amount > mysql_amount.export the database amount to a file mysql_amount.dump mysqldump –uroot –p amount < mysql_amount.id.dump Note : the database amount must exist .export the database amount to a file mysql_amount.dump ..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.dump mysqldump –uroot –p amount > mysql_amount.