Upload
purnag9
View
19
Download
5
Tags:
Embed Size (px)
DESCRIPTION
Brief explanation about unix
Citation preview
UNIX
Author – Gayathri Rudrareddy (228433)
The Unix Operating System
•An operating system (OS) is a set of programs that act as a link between the computer and the user.
•It controls both the
–hardware (things you can touch—such as keyboards, displays, and disk drives)
–software (application programs that you run, such as a word processor).
•Some computers have a single-user OS, which means only one person can use the computer at a time. They can also do only one job at a time.
•UNIX is a multiuser, multitasking operating system. It can let many people use the computer at the same time and let each user run several jobs at once.
2
Kernel
• Manages memory and allocates it to each process
• Schedules work done by the CPU
• Organizes transfer of data from one part of machine to another
• Accepts instructions from shell and carries them out
Shell
• Users communicate with the kernel through a program known as the shell.
• interprets the user commands/requests
• calls programs from memory
• executes them one at a time or in a series (pipe)
Applications
• specific capabilities added on to the operating system
Kernel( Heart of Unix Operating System)
Shell Interpreter
Tools and Applications
Unix Structure
3
File System
Contains data, text or program instructions Stores ordinary &
special files.
Usually represent devices used by the system.eg., /dev/cd0
4
All data in UNIX is organized into files.
All files are organized into directories.
These directories are organized into a tree-like structure called the filesystem.
File Structure
5
Path names
Absolute Path name
* A file is identified by the path name from the root
e.g., /usr/trg/c/test.c
where
• test.c is an ordinary file
• usr, trg, c are directories
• trg is a sub-directory under usr
Relative path name
* UNIX keeps track of the user's current directory
* If a "/" does not precede a file name then the name interpretation
begins with the current directory
e.g.,
If current directory is /usr/trg
then the file could be just referenced as c/test.c
6
Starting and Ending a Session
login : User can type his name and password to identify himself
Enter username at login: prompt. (Unix is case sensitive.)
Enter password at password: prompt.
passwd : To change your password
logout : exits or logs-out from the current user and executes the file .logout
before coming out
e.g., $ logout
or
$ exit
or
$ <ctrl-d>
7
Command line structure
General Structure:
Command Name [Options] [Arguments]
E.g.,
Command Name
Tells the shell what to do
(in this case list files) Options
Control how the command will work
(in this case ask for a long listing)
Arguments
on which the command works
(in this case the directory
usr/trg )
ls -l /usr/trg
8
Basic UNIX commands
• mkdir – creates a new directory ( ―-p‖ - creates missing intermediate directories)
- eg., mkdir /usr/test
• rmdir – removes directory ( ―-p‖ – removes all directories)
- directory must be empty in order to delete it.
- eg., rmdir /usr/test
• cd – change working directory (―cd ..‖ – one level back, ―cd ~‖ - home,
―cd /‖ - root, ―cd .‖ – current directory)
• pwd – print working directory
• touch - creates a zero-length file.
- If a file with the name already exists the last modification time is updated with the
current time.
9
List command
ls – list all files in a directory
options: -a lists files including hidden files
-x multi-column output
-F to identify directories (/) and executables (*)
-d force listing of directories
-t sorts files by modification time
-r reverse sort order
-l long listing (lists mode, link info, owner, size, last modification)
eg.,
drwxr-xr-x 2 qdaemon GG_CRMCU 256 Jan 11 12:04 temp
-rwxrwxrwx 1 qdaemon qdaemon 886362 Jan 12 16:49 sqlnet.log
$ ls -l
-rwxr-xr-x 2 ankit staff 1024 jan 12 10:35 test
Permission bits
file/dir link count
user name
user group
character count
last modified time
filename 10
File Permissions rwx r-x r-x
owner(U) owner’s others(O)
group(G)
r ---- read (4)
w ---- write (2)
x ---- execute (1)
Access Mode Ordinary Directory
Read Examination of
contents
Listing of files in the
directory
Write Changing of the
contents
Creating new files in
the directory
Execute Execution of the file
as a command
Searching the
directory
• chmod - change the file or directory access permissions (mode)
eg., chmod 777 /usr/test/script.sh
• chgrp - change the group of the file (can be done only by the member of the group)
• chown - change the owner of a file
11
Basic commands (contd.)
echo prints the string
cat concatenate (list) output, displays a file and creates a file if file not present
more displays file contents one screen at a time
cp copies a file
rm removes a file
mv moves a file to a different directory or renames a file
cut to cut a part of the string
grep searches a pattern in a file
find searches a files in a directory
sort sorts the input received
wc displays no. of lines (-l), words (-w) and characters (-m)
head display first 10 (or #) lines of file
tail display last 10 (or #) lines of file
date displays current date and time
df information about space on file systems
ps shows current status of active processes
who lists all users currently on system
who am i reports information on command user
clear clears screen
cal displays calendar
bc calculator mode (^D to leave calc mode)
expr to perform arithmetic operations
12
stdin, stdout, and stderr
• Each shell (and in fact all programs) automatically open three ―files‖ when they start up
– Standard input (stdin): Usually from the keyboard – FILE ID 0
– Standard output (stdout): Usually to the terminal – FILE ID 1
– Standard error (stderr): Usually to the terminal – FILE ID 2
Redirecting stdin
• Instead of reading from the terminal, you can tell a program to read from another file using the <
operator
Examples:
– Mail [email protected] < message
Redirecting stderr
• Instead of writing errors to the terminal, you can tell a program to write them to another file using the:
Examples (suppose a file that does not exist)
home$cat IPL_team.list
cat: cannot open IPL_team.list
home$cat IPL_team.list 2> error_file.txt
Home$cat error_file.txt
cat: cannot open IPL_team.list
Redirecting stdout
• Instead of writing to the terminal, you can tell a program to print its output to another file using the >
operator
Examples: man ls > ls_help.txt
cat file1 >> file2 13
Pipes and filters
• Pipe (|): a way to send the output of one command to the input of another
• Filter: a program that takes input and transforms it in some way
e.g., awk, sed, grep
1) cat file | wc
2) $ cat basket.txt
Layer1 = cloth
Layer2 = strawberries
$ cat basket.txt | awk -F= '{print $1}'
Layer1
Layer2
3) $ cat apple.txt
worm
seed
$ cat apple.txt | sed -e "s/e/WWW/"
worm
sWWWed
4) $ grep seed apple.txt
seed
14
Program & Process
• Program is an executable file that resides on the disk.
• Process is an executing instance of a program.
• A Unix process is identified by a unique non-negative integer called the process ID.
• Check process status using the “ps” command.
– After a successful login, the shell program is run.
– /home/userid$ ps
PID TTY TIME CMD
23110 pts/3 0:00 ps
18666 pts/3 0:00 ksh
15
Foreground and Background Process
• A program run using the ampersand operator “&” creates a background process.
E.g.: /home/userid$ back &
• otherwise it creates a foreground process.
E.g.: /home/userid$ back
• Only 1 foreground process for each session. Multiple background processes.
• Where are background processes used?
• All system daemons, long user processes, etc.
e.g. printer-daemon process or mailer-daemon process.
• These processes are always running in background.
16
17
How to stop a process?
• Foreground processes can generally be stopped by pressing CONTROL C (^C).
• Background processes can be stopped using the kill command.
• Usage: kill SIGNAL <process id list>
• kill -9 <process id list> (-9 means no blocked) Or kill <process id list>.
• If a foreground process is not stopping by ^C, you can open another session and use the kill command.
17
18
UNIX Shell
The shell is your interface with the Unix system.
Is an ―interpreter‖ of commands
19
Shell Scripting
Creating a simple shell script
A shell script is a plain text file that contains series of commands that the shell can execute.
• Any commands you enter in response to a shell prompt
A utility
A compiled program
Another shell script
• Control flow commands
Run a shell script
Enter the script filename on the command line
The shell interprets and execute the commands sequentially
Why shell script?
Simply and quickly initiate a complex series of tasks or a repetitive procedure.
Execution of script – 3 Simple steps
20
1. Use any editor to write shell script.
2. After writing shell script set execute permission for your script.
Syntax:
chmod <permission> <your-script-name>
Example:
$chmod +x your-script-name
$chmod 755 your-script-name
3. Execute your script
Syntax:
sh <your-script-name>
./<your-script-name>
Run the script with different shell other than your interactive shell
Ex: home>sh test
Put special characters on the first line of a shell script
The firsts two character of a script are #! (shebang)
Then followed by the absolute pathname of the program that should execute the script
Ex:
home> cat test
#!/bin/ksh
# This line will not run since it is commented out...
echo 'This is Test!„
Pound sign (#) start a comment line until the end of that line as second line
in previous example, except
#! In the first line.
21
Environmental Variables
22
Special variables used by the system -- you can use these, but may not be able to change
them.
The special variables use uppercase names, or punctuation characters.
Some variables are set by the login process and inherited by the shell (e.g. $USER), while
others are used only by the shell.
e.g., .profile file in UNIX korn shell is executed whenever a user logs in. It is used to set
environment variables and shell options.
Example :
log_path=/usr/test/logs
export log_path
User Defined Variables
23
1. To define user defined variable (UDV) use following syntax
Syntax: variable_name=value
Example:
To define variable called 'vehicle' having value Bus
vehicle1=Bus
echo “The value of vehicle1 is : $vehicle1.”
The value of vehicle1 is : Bus.
To define variable called n having value 10
n=10
echo “The value of n is $n.”
The value of n is 10.
Note: Syntax for referencing UDV is $variable_name
User Defined Variables (contd.,)
24
1. Rules for naming variable name
• Variable name must begin with Alphanumeric character or
underscore character (_), followed by one or more Alphanumeric
character ( eg. HOME, SYSTEM_VERSION )
• Don't put spaces on either side of the equal sign when assigning
value to variable. ( number=10 )
• Variables are case-sensitive. (no, No, NO, nO are all different
variable names)
• You can define NULL variable as follows
• vech= (or) vech=“”
• Do not use ?, * etc. for naming variables.
Wild Cards
25
1. * - Matches any string or group of characters.
• ls ut*.c – will show all files having extension .c but file name must
begin with “ut”.
2. ? - Matches any single character.
• ls fo? - will show all files whose names are 3 character long and
file name begin with “fo”
3. […] - Matches any one of the enclosed characters
• ls [abc]* - will show all files beginning with letters a,b,c
4. [..-..] – Matches range of character
ls [a-d]* - will show all files beginning with letters a,b,c,d
ls [^a-c]* - will show all files beginning with letters other than a,b,c
Shell script
26
1. Exit Status of command or shell script?
echo $? ( zero return value indicates success, else failure )
2. Read statement
• Use to get input (data from user) from keyboard and store (data) to
variable.
Syntax:
read variable1
3. To run two command with one command line.
Syntax:
command1;command2
4. Continuing Lines
Type a backslash, \, at the end of line
to continue command on the next line.
echo –n “Please enter your name: “
read NAME
echo “Welcome! $NAME !”
date;who
name=Raghu
echo “Value of name is: \
$name “
Shell Arithmetic
27
1. Use to perform arithmetic operations.
2. Syntax:
expr op1 math-operator op2
Examples:
$expr 1 + 3
$expr 2 - 1
$expr 10 / 2
$expr 20 % 3
$expr 10 \* 3
$echo `expr 6 + 3`
Redirection operators
28
1. > - To output result (output of command or shell script) to file
• Note: If file already exists, it will be overwritten else new file is
created
• $ls tmp > ls.out 2>errors
2. >> - To output result to END of file
• Note: If file exists , it will be opened and new information/data
will be written to END of file, without losing previous
information/data
• If file does not exist, then new file is created
3. < - To take input from file instead of key-board
• $sort < /home/emp.txt > results
Special command symbols
29
; command separator
& run the command in the background
&& run the following command only if previous command completes successfully
|| run the following command only if previous command not complete successfully
\ escape the following character (take it literally)
‘ ‘ don‘t allow any special meaning to characters within single quotes
" " allow variable and command substitution inside double quotes (does not disable $ and \)
` ` take the output of command and substitute it into the command line (Works inside
double-quotes)
Arguments & Command Substitution
30
Arguments can be passed to a script from the command line
-- Positional parameters are used to receive their values from within the script
EXAMPLE
At the command line:
$ scriptname arg1 arg2 arg3 …
In a script:
echo $1 $2 $3 Positional parameters
echo $* All the positional parameters
echo $# The number of positional parameters
Command Substitution allows to assign the output of a UNIX command to a
variable or use the output of a command in a string
-- back quotes are used.
EXAMPLE
variable_name=`ls`
echo $variable_name
Shell scripting - Test
31
• test expression OR [ expression ] is used to see if an expression is true or false.
Comparing Numbers
Test Usage
$x –eq $y Returns true if x equal y
$x –ne $y Returns true if x does not equal y
$x –gt $y Returns true if x is greater than y
$x –ge $y Returns true if x is greater than or equal to y
$x –lt $y Returns true if x is less than y
$x –le $y Returns true if x is less than or equal to y
Testing Numbers
32
# Number test
echo “***Equal Comparison*** “
X=5
Y=10
if [ $X –eq $Y ]then
echo “X = Y.”
else
echo “X != Y.”
fi
# Number test
echo “***Less than Comparison*** “
X=5
Y=10
if test $X –lt $Y
then
echo “X < Y.”
else
echo “X > Y.”
fi
Comparing Strings
33
Test Usage
“$s1” = “$s2” Returns true if s1 equal s2
“$s1” != “$s2” Returns true if s1 does not equal s2
$s1 Returns true if s1 is not null
$s1 -z Returns true if length of s1 is zero
$s1 -n Returns true if length of s1 is not zero
#String test
echo “***Null Comparison*** “
String=“Unix Class”
if (test “$String”) then
echo “We have non-null string”
else
echo “We have null string”
fi
#String test
echo “***Length Check*** “
String=“Unix Class”
if (test –z “$String”) then
echo “Length of string is zero”
else
echo “Length of string is NOT zero”
fi
Testing Files
34
Test Usage
-d filename Returns true if filename exists and is a directory
-e filename Returns true if filename exists
-f filename Returns true if filename exists and is a regular file
-r filename Returns true if filename exists with read permission
-s filename Returns true if filename exists and is not empty (Len>0)
-w filename Returns true if filename exists with write permission
-x filename Returns true if filename exists with execute permission
Control flows
35
1. „For‟ Loop
2. „While‟ Loop
3. „Until‟ Loop
4. For each and every loop
• First, the variable used in loop condition must be initialized, then
execution of the loop begins.
• A test (condition) is made at the beginning of each iteration.
• The body of loop ends with a statement that modifies the value of
the test (condition) variable.
For Loop
36
Syntax:
for { variable name } in { list }
do
command1
command2
…
last command
done
Example
#print welcome 5 times
for i in 1 2 3 4 5
do
echo "Welcome $i times“
done
# Example to look at files in a directory and print out file names
for filename in *
do
echo “$filename”
done
Looping over files
--- FOR Loops can also be nested
While Loop
37
Syntax:
while [ condition ]
do
command1
command2 …
done
Note:
Loop is executed as long as
given condition is true
Example:
#print multiplication table for given number
if [ $# -eq 0 ] then
echo "Syntax : $0 <positive number>"
exit 1
fi
n=$1
i=1
while [ $i -le 10 ]
do
echo "$n * $i = `expr $i \* $n`"
i=`expr $i + 1`
done
Until Loop
38
Syntax:
Until [ condition ]
do
command1
command2 …
done
Note:
Similar to while, but the
loop is executed as long as
given condition is false
Example:
#print multiplication table for given number
if [ $# -eq 0 ] then
echo "Syntax : $0 <positive number>"
exit 1
fi
n=$1
i=1
until [ $i -eq 11 ]
do
echo "$n * $i = `expr $i \* $n`"
i=`expr $i + 1`
done
Break and Continue
39
• Break and Continue commands take no arguments.
• When the shell encounters a break command, it immediately exits the body
of the enclosing loop ( while, until, or for) command.
• When the shell encounters a continue command, it immediately
discontinues the current iteration of the loop. If the loop condition permits,
other iterations may occur; otherwise the loop is exited.
Conditional Statements – IF loop
40
Syntax:
if [ expression ]
then
block of statements
[elif command]
[else command]
fi
Example
#Testing equality
if [ $a –eq $b ] then
echo “Both are qual”
else
echo “Both are not equal”
fi
The if construct is followed by a command
If an expression is to be tested, it is enclosed in square brackets
The then keyword is placed after the closing parenthesis
An if must end with a fi
Case Statement
41
Syntax:
case $variable-name in
pattern1) command1... ..
command2;;
pattern2) command1 ... ..
command2;;
patternN) command1 ... ..
command2;;
*) command1... ..
command2;;
esac
FRUIT="kiwi"
case "$FRUIT" in
"apple")
echo "Apple pie is quite tasty." ;;
"banana")
echo "I like banana nut bread." ;;
"kiwi")
echo "New Zealand is famous for kiwi." ;;
esac
Functions
42
Functions allow you to define a section of shell code and give it a name.
Syntax:
function_name() {
block of code
}
-------------------------------------------------
Example:
lister() {
echo Your present working directory is `pwd`
echo Your files are:
ls
}
Crontabs
43
• Crontab file contains the schedule of cron entries to be run and at specified
times.
• The jobs which are scheduled are called as cron jobs.
• Location - /var/spool/cron/ --- only root user has access to the directory.
crontab -e Edit your crontab file, or create one if it doesn‗t already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file.
(option available only on few systems.)
Crontab Restrictions
-- User can execute crontab if name appears in the file /usr/lib/cron/cron.allow.
-- If allow file does not exist, then if username does not appear in the file
/usr/lib/cron/cron.deny.
-- If only cron.deny exists and is empty, all users can use crontab.
-- If neither file exists, only the root user can use crontab.
Crontab syntax
44
45