What Linux is?

Download Report

Transcript What Linux is?

What Linux is?
Free
Unix Like
Open Source
Network operating system
Information Networking Security and Assurance Lab
National Chung Cheng University
1
Who developed the Linux?
 In 1991, Linus Torvalds studied Unix at the
University, where he used special educational
experimental purpose operating system called Minix
(small version of Unix and used in Academic
environment).
 But Minix had it's own limitations. Linus felt he
could do better than the Minix. So he developed his
own version of Minix, which is now know as Linux.
 Linux is Open Source From the start of the day. For
more information on Linus Torvalds, please visit
http://www.cs.helsinki.fi/u/torvalds/.
Information Networking Security and Assurance Lab
National Chung Cheng University
2
How to get Linux?
http://www.freeos.com/guides/lsst/
Information Networking Security and Assurance Lab
National Chung Cheng University
3
What's Kernel
Applications
Shell
Kernel
 The hart of Linux OS.
 Manage resource of Linux OS.
Kernel decides who will use this resource, for how long and
when.
 Perform the following tasks
I/O management
Process management
Device management
File management
Memory management
http://files.nixp.ru/books/programming/Linux%20Shell%20Scripting%20Tutorial.pdf
4
Information Networking Security and Assurance Lab
National Chung Cheng University
Shell
A command language interpreter that executes
commands read from the standard input device
(keyboard) or from a file.
Information Networking Security and Assurance Lab
National Chung Cheng University
5
Most Popular Shells
$ echo $SHELL
Information Networking Security and Assurance Lab
National Chung Cheng University
6
Input – Output Redirection (I)
> Redirector Symbol
Syntax: Linux-command > filename
To output Linux-commands result to file.
If file already exist, it will be overwritten else new
file is created.
EX:
$ ls > myfiles
Information Networking Security and Assurance Lab
National Chung Cheng University
7
Input – Output Redirection (II)
>> Redirector Symbol
Syntax: Linux-command >> filename
To output Linux-commands result to END of file.
If file exist , it will be opened and new information
will be written to END of file (appendix), without
losing previous information, And if file is not exist,
then new file is created.
EX:
$ date >> myfiles
Information Networking Security and Assurance Lab
National Chung Cheng University
8
Input – Output Redirection (III)
 < Redirector Symbol
Syntax: Linux-command < filename
To take input to Linux-command from file instead of
key-board.
EX:
$ cat < myfiles
Information Networking Security and Assurance Lab
National Chung Cheng University
9
Pipe
A pipe is a way to connect the output of one
program to the input of another program
without any temporary file.
Pipes are used to run more than two commands
( Multiple commands) from same command
line.
Syntax: command1 | command2
Information Networking Security and Assurance Lab
National Chung Cheng University
10
Pipe Examples
Information Networking Security and Assurance Lab
National Chung Cheng University
11
Variables in Linux
Variable (a.k.a memory variable): Programmer
can give a unique name to this memory
location/address.
In Linux, there are two types of variable
System variables - Created and maintained by Linux
itself.
 This type of variable defined in CAPITAL LETTERS.
User defined variables (UDV) - Created and
maintained by user.
This type of variable defined in lower LETTERS.
Information Networking Security and Assurance Lab
National Chung Cheng University
12
Some System variables
 You can change system variables by giving command
like $ set,
 You can print any of the variables via “echo”
Ex: $ echo $USERNAME; $ echo $HOME
Information Networking Security and Assurance Lab
National Chung Cheng University
13
How to define and print User defined
variables (UDV)
 Syntax: variablename=value
 Ex:
 $ no=10 # this is ok
 $ 10=no # Error, NOT Ok, Value must be on right side of = sign.
 Q.1.How to Define variable x with value 10 and print it on
screen
 $ x=10
 $ echo $x
 Q.2.How to Define variable xn with value Rani and print it on
screen
 $ xn=Rani
 $ echo $xn
Information Networking Security and Assurance Lab
National Chung Cheng University
14
Rules for Naming variable name (Both
UDV and System Variable)
 Variable name must begin with Alphanumeric character or underscore character (_),
followed by
 one or more Alphanumeric character.
 EX:
 HOME
 SYSTEM_VERSION
 no






Don't put spaces on either side of the equal sign when assigning value to variable.
$ no=10
()
$ no
=10 (X)
$ no= 10
(X)
$ no = 10 (X)
Variables are case-sensitive, just like filename in Linux.
 $ no=10
 $ NO=11
Information Networking Security and Assurance Lab
National Chung Cheng University
15
Some Good Questions (I)
Q.1: How to print sum of two numbers, let's
say 6 and 3
$ echo 6 + 3
This will print 6 + 3, not the sum 9
Try $ expr 6 + 3
How about $ expr 6+3 (please note that no space
between 6 and 3)?
Information Networking Security and Assurance Lab
National Chung Cheng University
16
Some Good Questions (II)
 Q.2:How to define two variable x=20, y=5 and then
to print division of x and y (i.e. x/y)
$x=20
$ y=5
$ expr x / y
Or
$ x=20
$ y=5
$ z=`expr x / y`
$ echo $z
Information Networking Security and Assurance Lab
National Chung Cheng University
17
What is Shell Scripts?
Shells are interactive.
 It means shell accept command from you (via
keyboard) and execute them.
If you want to execute the sequence of
commands , the you can store this sequence of
command to text file and tell the shell to
execute this text file instead of entering the
commands. This is know as shell script.
Information Networking Security and Assurance Lab
National Chung Cheng University
18
Why to Write Shell Script ?
Shell script can take input from user, file and
output them on screen.
Useful to create our own commands.
Save lots of time.
To automate some task of day today life.
System Administration part can be also
automated.
Information Networking Security and Assurance Lab
National Chung Cheng University
19
How to write shell script
Write a script that will print "Knowledge is
Power" on screen.
$ cat > first
#
# My first shell script
#
clear
echo "Knowledge is Power"
Information Networking Security and Assurance Lab
National Chung Cheng University
20
How to Run Shell Scripts
 Shell Script does not get execution permission by
default. Two Ways to run it
1. Use chmod command to give execution permission
Syntax: chmod +x shell-script-name OR Syntax: chmod 777
shell-script-name
2. Run our script as
Syntax: ./your-shell-program-name
Ex: $ ./first
OR /bin/sh &nbsh;&nbsh; your-shell-program-name
Ex:
$ bash first
$ /bin/sh first
Information Networking Security and Assurance Lab
National Chung Cheng University
21
More on Running Shell Scripts
Need to run the scripts in same directory where
you created your script, if you are in different
directory your script will not run.
To Overcome this problem, there are two ways
specify complete path of your script when ever you
want to run it from other directories like giving
following command.
Ex: $ /bin/sh /home/vivek/first
This take time and you have to remember complete
path.
Information Networking Security and Assurance Lab
National Chung Cheng University
22
Another Way To Run Shell Scripts
 Can be directly executed from prompt from
any directory
 All our executables files are installed in directory called
/bin and /bin directory is set in your PATH setting.
 Principles: It will look for current directory, if found
shell will execute command from current directory, if
not found, then Shell will Look PATH setting, and try to
find our requested commands executable file in all of
the directories mentioned in PATH settings,
 Procedures:
 Create bin directory in your home directory and then
copy your tested version of shell script to this bin
directory. After this you can run you script as
executable file without using $ ./shell
Information Networking Security and Assurance Lab
National Chung Cheng University
$ cd
$ mkdir bin
$ cp first ~/bin
$ first
23
How to de-bug the shell script?
 Use -v and -x option with sh or bash
command to debug the shell script.
 Syntax:
sh option { shell-script-name }
OR
bash option { shell-script-name }
 -v: Print shell input lines as they are
read.
 -x: After expanding each simplecommand, bash displays the expanded
value of PS4 system variable, followed
by the command and its expanded
arguments.
Information Networking Security and Assurance Lab
National Chung Cheng University
$ cat > dsh1.sh
#
# Script to show debug of shell
#
tot=`expr $1 + $2`
echo $tot
$ chmod 755 dsh1.sh
$ ./dsh1.sh 4 5
9
$ sh -x dsh1.sh 4 5
#
# Script to show debug of shell
#
tot=`expr $1 + $2`
expr $1 + $2
++ expr 4 + 5
+ tot=9
echo $tot
+ echo 9
9
24
One Small Test
 Q.6.Point out error if any in following script
 $ vi variscript
#
#
# Script to test MY knowledge about variables!
#
myname=Vivek
myos = TroubleOS
myno=5
echo "My name is $myname"
echo "My os is $myos"
echo "My number is myno, can you see this number"
Information Networking Security and Assurance Lab
National Chung Cheng University
25
The read Statement
Use to get input (data from user) from
keyboard and store (data) to variable.
Syntax: read variable1, variable2,...variableN
Following script first ask user, name and then
waits to enter name from the user via keyboard.
#
#Script to read your name from key-board
#
echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"
Information Networking Security and Assurance Lab
National Chung Cheng University
$ chmod 755 sayH
$ ./sayH
Your first name please: vivek
Hello vivek, Lets be friend!
26
Wild cards
Information Networking Security and Assurance Lab
National Chung Cheng University
27
Shorthand at the Command Prompt (I)
 / - root directory
 ./ - current directory
 ./command_name - run a command in the current directory
when the current directory is not on the path
 ../ - parent directory
 ~ - home directory
 $ - typical prompt when logged in as ordinary user
 # - typical prompt when logged in as root or superuser
 ! - repeat specified command
 !! - repeat previous command
Information Networking Security and Assurance Lab
National Chung Cheng University
28
Shorthand at the Command Prompt (II)
 & - run a program in background mode
 [Tab][Tab] - prints a list of all available commands.
This is just an example of autocomplete with no
restriction on the first letter.
 x[Tab][Tab] - prints a list of all available completions
for a command, where the beginning is ``x''
 [Ctrl]c - kill the current process
 [Ctrl]d - logout from the current terminal
 [Ctrl]z - send current process to the background
 reset - restore a terminal to its default settings
Information Networking Security and Assurance Lab
National Chung Cheng University
29
Important Bash Shell Variables
HOME - home directory, abbreviated as ~
PATH - directory paths to search for
executable files.
PS1 - prompt string. Things that can be put in
the prompt string include \h (hostname), \u
(username), \w (absolute pathname of working
directory), \W (name of working directory w/o
path), \d (date), \t (time).
Information Networking Security and Assurance Lab
National Chung Cheng University
30
Linux Command Related with Process
Linux is multi-user, multitasking OS. It means
you can run more than two process
simultaneously if you wish.
Information Networking Security and Assurance Lab
National Chung Cheng University
31