Basics On Linux An Commands and Shell Scriptig
Basics On Linux An Commands and Shell Scriptig
Shell Scripting
Basics
Functions
2
Strictly speaking, Linux is a kernel. A kernel provides
access to the computer hardware and control access
to resources
Created by Linus Torvalds.
Open source .
3
Linux distribution
A Linux distribution (often abbreviated as distro) is
an operating system made from a software
collection, which is based upon the Linux kernel and,
often, a package management system.
Linux users usually obtain their operating system by
downloading one of the Linux distributions
4
Widely used distributions
Debian
Knoppix
Linux Mint Debian Edition(LMDE)
Ubuntu(Kubuntu, Linux Mint, Trisquel, Elementary OS).
Fedora
Red Hat Enterprise Linux (RHEL)
(CentOS, Oracle Linux, Scientific Linux,
MandrivaLinux ( Mageia, PC LinuxOS, ROSA Linux,)
Open SUSE(SUSE Linux Enterprise)
Arch Linux, Gentoo, Slackware.
5
Why Linux is better than Windows
Managing File system
Virus
Update
Servers
Total cost of ownership
Sound architecture
6
Basic commands
Cat
Tac
Head
Tail
Wc
Others.
7
Cat and tac
The cat command is one of the most universal tools.
All it does is copy standard input to standard output.
$ cat filename
create flat text files.
cat > filename
Press ctlr d send end of file
Or $ cat > hot.txt <<stop - declare stop key word
8
head
Print the first 10 lines from a file directory
$ head filename
The head command can also display the first n lines of a
file.
$ head n filename
9
Tail
Similar to head, the tail command will display the last
ten lines of a file.
$ tail filename
You can give tail the number of lines you want to see.
$ tail -3 count.txt
10
Sort file
The sort command sorts its input line by line
By default, does alphanumeric sort on entire line
Command line options include:
f ignore uppercases
n- numeric sort
r-revers sort
k N- sort on a field N
uniq- remove duplicates from a sorted list.
11
wc
The command wc counts lines, words and characters
in its input files
Command line options include
l show only the lines
w- show only the words
c- show only the character
L- show the length of the longest files
12
Name cd
Syntax: cd [directory]
Description: The current working directory to the
directory specified by "directory".
Name: ls
Syntax: ls [options] [pathname-list]
Description: display the file name within the
directory and file name specified in the
"pathname-list"
13
Name: pwd
Syntax: pwd
Description: Displays the absolute path of the
current directory.
Name: mkdir
Syntax: mkdir [options] dirName
Description: create name is dirName subdirectory
14
Name: rmdir
Syntax: rmdir [-p] dirName Description: delete
empty directories
Name: cp
Syntax: cp [options] file1 file2 Description: Copy
the file file1 to file2.
15
Name: mv
Syntax: mv [options] source ... directory
Description: Rename the file, or the number of files
to another directory. Example: aaa renamed as
bbb:
Name: rm
Syntax: rm [options] name ...
Description: delete files and directories. Commonly
used options:-f to force delete files
16
Name: cat
Syntax: cat [options] [file-list]
Description: standard output connection, display a list of
files in the file-list file
ln:
Syntax: ln [options] existing-file new-file ln [options]
existing-file-list directory
Description: create a link named "existing-file" new-file
17
Name: chmod
Syntax: chmod [option] mode file-list
Description: read, write, or execute permissions
change or set the parameters in the file-list
Example: Add file job executable permissions
chmod + x job.sh
Syntax: chmod [option] [files]
18
Standard Input Output redirection
19
Introduction
21
Redirecting standard Output
> stdout
stdout can be redirected with a greater than sign. While
scanning the line, the shell will see the > sign and will
clear the file.
[paul@RHELv4u3 ~]$ echo It is cold today! > winter.txt
[paul@RHELv4u3 ~]$ cat winter.txt
It is cold today!
22
Cont..
command > file
This pattern redirects the standard output of a command to a file.
ls ~ > root_dir_contents.txt
The command above passes the contents of your system's root
directory as standard output,
and writes the output to a file named rootdircontents.txt.
It will delete any prior contents in the file, as it is a single-bracket
command.
23
Cont..
command > /dev/null
/dev/null is a special file that is used to trash any data that is
redirected to it.
It is used to discard standard output that is not needed, and
that might otherwise interfere with the functionality of a
command or a script.
Any output that is sent to /dev/null is discarded.
24
Cont..
noclobber
Erasing a file while using > can be prevented by setting the
noclobber option.
[paul@RHELv4u3 ~]$ cat winter.txIt is cold today!
[paul@RHELv4u3 ~]$ set -o noclobber
[paul@RHELv4u3 ~]$ echo It is cold today! > winter.txt
-bash: winter.txt: cannot overwrite existing file
[paul@RHELv4u3 ~]$ set +o noclobber
25
Cont..
command >> file
26
Cont..
It then appends the text received by the second echo
contents.
Ls ~ >> data.txt
27
Redirecting standard error
2> stderr
Redirecting stderr is done with 2>. This can be very
useful to prevent error messages from cluttering your
screen.
The screenshot below shows redirection of stdout to a
file, and stderr to /dev/null.
[paul@RHELv4u3 ~]$ mkdir > allfiles.txt 2> /dev/null
28
Cont..
2>&1
To redirect both stdout and stderr to the same file, use
2>&1.
[paul@RHELv4u3 ~]$ find / > allfiles_and_errors.txt
2>&1
[paul@RHELv4u3 ~]$
29
Cont..
command 2>> file
The pattern above redirects the standard error stream
of a command to a file without overwriting the file's
existing contents.
Mkdir '' 2>> stderr_log.txt
30
Redirecting Standard input
Many commands can accept input from a facility called
standard input
By default, standard input gets its contents from the
keyboard,
but like standard output, it can be redirected.
To redirect standard input from a file instead of the
keyboard, the "<" character is used
31
Cont
This < character used with other command like cat, pipes or |
sort etc. and the redirecting of input is works with the
combination of < and >.
Cat - concatenate files
Sort - Sorts standard input then outputs the sorted result on
standard output.
paul@deb503:~$ cat > tennis.txt << ace
> Justine Henin
> Venus Williams
> Serena Williams
> Martina Hingis
> Kim Clijsters
> ace
32
Cont
paul@deb503:~$ cat < tennis.txt > test.sh
paul@deb503:~$ cat test.sh
Justine Henin
Venus Williams
Serena Williams
Martina Hingis
Kim Clijsters
33
Pipes
A pipe is a form of redirection that is used in Linux
and other to send the output of one program to
another program for further processing.
Can run multiple commands on the same command
line.
A pipe is designated in commands by the vertical
bar character.
General syntax:
command_1 | command_2 [| command_3 . . ]
34
Examples of usage of pipe
the output of the ls command is commonly piped
to the the less (or more) command to make the
output easier to read :
ls -al | less or
ls -al | more
For other directories like /etc
ls al /etc | less
35
cont
Capture standard out put:
Example:
echo -e "orange \npeach \ncherry" | sort > fruit
the echo command sends what it follows to the
stdout,
the e option tells the computer to interpret each \n
as a new line symbol,
the pipe redirects the out put to the sort command
then redirected to the file fruit.
36
cont
Capture standard out put:
Example:
echo -e "orange \npeach \ncherry" | sort > fruit
the echo command sends what it follows to the
stdout,
the e option tells the computer to interpret each \n
as a new line symbol,
the pipe redirects the out put to the sort command
then redirected to the file fruit.
37
Other examples
ls l | grep ^- : list all files in the current directory.
ls | head | tail > myfile : list the head and tail files
and print it on myfile.
Wc l < myfile | tee newfile
dmesg | less : startup messages can conveniently
be viewed one screen full at a time.
38
Linux Shell
39
What is Linux Shell?
In Linux operating system, there is a special program
called the shell. The shell accepts human readable
commands and translates them into something the
kernel can read and process
Generally speaking the shell:
is a user program or it is an environment provided for
user interaction
40
Linux Shell. . .
41
Linux Shell. . .
KSH (Korn SHell) - Created by David Korn at AT & T
Bell Labs. The Korn Shell also was the base for the
POSIX Shell standard specifications
etc. . .
Each shell does the same job, but each understands
different command syntax and provides different
built-in functions. Under MS-DOS, the shell name is
COMMAND.COM which is also used for the same
purpose, but it is by far not as powerful as our
Linux Shells are!
42
Linux shell. . .
Shell Prompt
There are various ways to get
shell access:
Terminal - Linux desktop
provide a GUI based login
system. Once logged in you
can gain access to a shell by
running X Terminal (XTerm),
Gnome Terminal (GTerm), or
KDE Terminal (KTerm)
application
43
How do I find Out My Current Shell
Name?
To find all of the available shells in your system, type
the following command:
cat /etc/shells
In case the shells file has more than one shell listed
under it, then it means that more than one shell is
supported by your Platform
To find out your current shell type following
command:
echo $SHELL
44
Getting Help In Linux
Most commands under Linux will come with
documentation. Use any one of the following
option to get more information about Linux
commands:
man commandName
info commandName
commandName --help
45
What is a Shell Script or shell
scripting?
A Shell script can be defined as "a series of
command(s) stored in a plain text file".
A shell script is similar to a batch file in MS-DOS, but it
is much more powerful compared to a batch file.
Each shell script consists of:
- Shell keywords, Shell commands, Linux binary
commands, Text processing utilities, Functions, and
Control flow statements
46
Why shell scripting?
Whenever you find yourself doing the same task over
and over again you should use shell scripting, i.e.
repetitive task automation.
Shell commands: The bash shell comes with two types
of commands
1. Internal commands (builtins) - part of the shell itself,
i.e. built into the shell
2. External commands - separate binaries stored in
/sbin, /usr/sbin, /usr/bin, /bin, or /usr/local/bin
directories
47
type Command
48
Starting Shell- Hello world
Test
Ship to production
49
Simplest of all
50
Hello world
#!/bin/bash #tells indicate an interpreter for execution under UNIX / Linux operating systems.
clear
echo "Hello, World!"
echo "Working with Linux scripting gets started."
51
shebang
All scripts under Linux execute using the interpreter specified on a first
line.
Almost all bash scripts often begin with #!/bin/bash (assuming that Bash has
been installed in /bin)
This ensures that Bash will be used to interpret the script, even if it is
53
Shell Comments
54
Changing permission on script
56
Variables in shell
57
Rules for naming variables
59
Read from keyboard
60
Perform Arithmetic Operations
61
Shell Decision Making
62
If ---------fi statements
if [ expression ] then
fi
63
Cont
Give you attention on the spaces between braces and expression. This space is
mandatory otherwise you would get syntax error.
#!/bin/bash
a=10
b=10
if [ $a == $b ]
then
fi
64
The if...else...fi statement
Syntax
if [ expression ]
Then
else
fi
65
Cont
Give you attention on the spaces between braces and expression. This space is
mandatory otherwise you would get syntax error.
#!/bin/bash
a=10
b=10
if [ $a == $b ]
then
fi
66
Cont
Give you attention on the spaces between braces and expression. This space is
mandatory otherwise you would get syntax error.
#!/bin/bash
a=10
b=10
if [ $a == $b ]
then
fi
67
If elif fi
if [ expression 1 ]
then
elif [ expression 2 ]
then
elif [ expression 3 ]
then
68
Cont
Give you attention on the spaces between braces and expression. This space is
mandatory otherwise you would get syntax error.
#!/bin/bash
a=10
b=10
if [ $a == $b ]
then
fi
69
The case esac statements
Syntax
case word in
pattern1)
70
Cont
Give you attention on the spaces between braces and expression. This space is
mandatory otherwise you would get syntax error.
#!/bin/bash
a=10
b=10
if [ $a == $b ]
then
fi
71
Example
#!/bin/bash
language=python
Case $language in
php) echo learn php at w3schools
;;
java) echo install jdk first.
;;
python) echo download from http://www.python.org/.
;;
esac
72
Shell Loop Types
73
Syntax
while command
do
done
74
Example
#!/bin/bash
a=0 Shellcommandis
Syntax
do
done
76
Example 1
#!/bin/bash
for var in 0 1 2 3 4 5 6 7 8
do
echo $var
done
77
Example 2
#!/bin/bash
for item in *
do
if [ -d $item ]
then
echo $item
fi
done
78
Cont
79
Syntax
until command
do
Statement(s) to be executed until command is true.
done
80
Example
#!/bin/bash
a=0 Shellcommandis
82
What are functions?
83
Function shell scripting
84
Function shell
85
Cont
86
Cont
87
Example
#!/bin/bash
#define our function here
Hello () {
echo hello world
}
#invoke our function here
Hello
88
Pass parameters to a functions
89
Returning values from functions
If we want to terminate execution of the function, then there
is a way to come out of a defined function
Based on this we can return any value from our function
using the return command.
Like return code
code can be anything you choose
90
Example
#!/bin/bash
92
Example
#!/bin/bash
#!/bin/bash
function () {
echo Dear customer please choose three /3/ models of car that you
want to buy.
read car1
read car2
read car3
}
function ()
echo dear customer, you have choose first model : $car1
echo dear customer, you have choose second model : $car2
echo dear customer, you have choose third model : $car3
95
Examples that require users to input value
#!/bin/bash
function () {
echo Dear customer please choose three /3/ models of car that you
want to buy.
read car1
read car2
read car3
}
function ()
echo dear customer, you have choose first model : $car1
echo dear customer, you have choose second model : $car2
echo dear customer, you have choose third model : $car3
96
Examples that require users to input value
#!/bin/bash
function () {
echo Dear customer please choose three /3/ models of car that you
want to buy.
read car1
read car2
read car3
}
function ()
echo dear customer, you have choose first model : $car1
echo dear customer, you have choose second model : $car2
echo dear customer, you have choose third model : $car3
97
User Management and File permission
98
User management is a critical part of maintaining a
secure system. Ineffective user and privilege
management often lead many systems into being
compromised. Therefore, it is important that you
understand how you can protect your server/DB
through simple and effective user account
management techniques.
99
Enable the root account
sudo passwd
To disable the root account password, use the
following passwd syntax:
The process for managing local users and groups is straightforward and
differs very little from most other GNU/Linux operating systems. Ubuntu
and other Debian based distributions encourage the use of the "adduser"
package for account management.
To add a user account, use the following syntax, and follow the prompts to
give the account a password and identifiable characteristics, such as a full
name, phone number, etc.
103
User Profile Security
When a new user is created, the adduser utility creates a brand new home
directory named /home/username. The default profile is modeled after the
contents found in the directory of /etc/skel, which includes all profile basics.
By default, user home directories in Ubuntu are created with world
read/execute permissions. This means that all users can browse and access
the contents of other users home directories. This may not be suitable for
your environment.
ls -ld /home/username
104
Password Expiration
To easily view the current status of a user account, use the following syntax:
106
Read, Write & Execute Permissions
107
Changing Directory and File Permissions
To view file permissions and ownership on files and directories, use the ls
-al command. The adoption is to show hidden files or all files, and
the l option is for the long listing. The output will be similar to the
following:
d is a directory
rwx the user has read, write, and execute permissions
rw- the group has read and write permissions
r all others have read only permissions
109
Chmod Octal Format
The execute permission is equal to the number one (1), the write
permission is equal to the number two (2), and the read permission is
equal to the number four (4). Therefore, when you use the octal format,
you will need to calculate a number between 0 and 7 for each portion of
the permission. A table has been provided below for clarification.
110
Changing File Ownership and group
By default, all files are owned by the user who creates them and by that
users default group. To change the ownership of a file, use
the chown command in the chown user:group /path/to/file format. In the
following example, the ownership of the list.html file will be changed to
the cjones user in the marketing group:
111