unix_lab_manual
unix_lab_manual
PERISSERY
CS 1541: FOSS Lab
TABLE OF CONTENTS
P a g e 2 | 35
CS 1541: FOSS Lab
* date Command
Display the current time in the given FORMAT, or set the system date.
Options
* mkdir Command
Options
* rmdir Command
Remove the DIRECTORY(ies), if they are empty.
P a g e 3 | 35
CS 1541: FOSS Lab
* bc Command
usage: bc [options] [file ...]
* passwd Command
Change user password.
Options:
-e, --expire force expire the password for the named account
* who Command
Print information about users who are currently logged in.
P a g e 4 | 35
CS 1541: FOSS Lab
Options
* tty Command
Print the file name of the terminal connected to standard input.
Options
* ls Command
List information about the FILEs (the current directory by default).
Options
* cat Command
Concatenate FILE(s), or standard input, to standard output
Options
-e equivalent to -vE
-t equivalent to -vT
* cp Command
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Options
* rm Command
Remove (unlink) the FILE(s).
Options
* mv Command
Usage: mv [OPTION]... [-T] SOURCE DEST
Options
* wc Command
Usage: wc [OPTION]... [FILE]...
Print newline, word, and byte counts for each FILE, and a total line if
Options
* cmp Command
Usage: cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
Options
* comm Command
Usage: comm [OPTION]... FILE1 FILE2
Options
* chmod Command
Usage: chmod [OPTION]... MODE[,MODE]... FILE...
P a g e 8 | 35
CS 1541: FOSS Lab
Options
-c, --changes like verbose but report only when a change is made
* head Command
Usage: head [OPTION]... [FILE]...
* tail Command
Usage: tail [OPTION]... [FILE]...
Options
* sort Commands
Usage: sort [OPTION]... [FILE]...
Options
P a g e 9 | 35
CS 1541: FOSS Lab
INTRODUCTION TO VI-EDITOR.
The default editor that comes with the UNIX operating system is called vi (visual editor)
The UNIX vi editor is a full screen editor and has two modes of operation:
1. Command mode commands which cause action to be taken on the file, and
In the command mode, every character typed is a command that does something to the text file
being edited; a character typed in the command mode may even cause the vi editor to enter the
insert mode. In the insert mode, every character typed is added to the text in the file; pressing
the <Esc> (Escape) key turns off the Insert mode.
NOTE: Both UNIX and vi are case-sensitive. Be sure not to use a capital letter in place of a
lowercase letter; the results will not be what you expect.
To Start vi
To use vi on a file, type in vi filename. If the file named filename exists, then the first
page (or screen) of the file will be displayed; if the file does not exist, then an empty
file and screen are created into which you may enter text.
vi -r filename recover filename that was being edited when system crashed
To Exit vi
Usually the new or modified file is saved when you leave vi. However, it is also possible
to quit vi without saving the file.
Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of
command is completed by hitting the <Return> (or <Enter>) key.
* :x<Return> quit vi, writing out modified file to file named in original invocation
:wq<Return> quit vi, writing out modified file to file named in original invocation
* :q!<Return> quit vi even though latest changes have not been saved for this vi call
P a g e 10 | 35
CS 1541: FOSS Lab
In the table below, the symbol ^ before a letter means that the <Ctrl> key should be held
down while the letter key is pressed.
j or <Return>
* move cursor down one line
[or down-arrow]
h or <Backspace>
* move cursor left one character
[or left-arrow]
l or <Space>
* move cursor right one character
[or right-arrow]
* 0 (zero) move cursor to start of current line (the one with the cursor)
Screen Manipulation
The following commands allow the vi editor screen (or window) to move up or down
several lines and to be refreshed.
P a g e 11 | 35
CS 1541: FOSS Lab
Perhaps the most important command is the one that allows you to back up
and undo your last action. Unfortunately, this command acts like a toggle, undoing and
redoing your most recent action. You cannot go back more than one step.
The main purpose of an editor is to create, add, or modify text for a file.
* o open and put text in a new line below current line, until <Esc> hit
* O open and put text in a new line above current line, until <Esc> hit
P a g e 12 | 35
CS 1541: FOSS Lab
Changing Text
The following commands allow you to modify text.
R replace characters, starting with current cursor position, until <Esc> hit
change N words beginning with character under cursor, until <Esc> hit;
cNw
e.g., c5w changes 5 words
C change (replace) the characters in the current line, until <Esc> hit
Cc change (replace) the entire current line, stopping when <Esc> is hit
change (replace) the next N lines, starting with the current line,
Ncc or cNc
stopping when <Esc> is hit
Deleting Text
The following commands allow you to delete text.
D delete the remainder of the line, starting with current cursor position
P a g e 13 | 35
CS 1541: FOSS Lab
Nyy or yNy copy (yank, cut) the next N lines, including the current line, into the buffer
P put (paste) the line(s) in the buffer into the text after the current line
Other Commands
Searching Text
A common occurrence in text editing is to replace one word or phase by another. To
locate instances of particular sets of characters (or strings), use the following
commands.
provides the current line number, along with the total number of lines,
^g
in the file at the bottom of the screen
P a g e 14 | 35
CS 1541: FOSS Lab
Saving and Reading Files
These commands permit you to input and output files other than the named file with which you
are currently working.
:w! prevfile<Return> write current contents over a pre-existing file named prevfile
P a g e 15 | 35
CS 1541: FOSS Lab
PROGRAM 1:
AIM:- Write a shell program to find the average of n numbers.
PROGRAM:
sum= 0;
for((i=0;i<l;i++))
do
sum=$(($sum + ${a[$i]}));
done
OUTPUT
P a g e 16 | 35
CS 1541: FOSS Lab
PROGRAM 2:
AIM:- Write a shell program to find the number is even or odd.
PROGRAM:
read a;
n=` expr $a % 2 `;
if [ $n -eq 0 ]
then
else
fi
OUTPUT
Enter a number
3 is an odd number
Enter a number
6 is an even number
P a g e 17 | 35
CS 1541: FOSS Lab
PROGRAM 3:
AIM:- Write a shell program to print the Multiplication table.
PROGRAM:
read a;
do
n=` expr $i \* $a `;
done
OUTPUT
Enter a number
4*1=4
4*2=8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
P a g e 18 | 35
CS 1541: FOSS Lab
PROGRAM 4:
AIM:- Write a shell program to find the largest of three numbers.
PROGRAM:
read a
read b
read c
if [ $a -gt $b -a $a -gt $c ]
then
then
else
fi
OUTPUT
5 is Greatest !
P a g e 19 | 35
CS 1541: FOSS Lab
PROGRAM 5:
AIM:- Write a shell program to find the sum of digits of a number.
PROGRAM:
read n
temp=$n
sd=0
sum=0
while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
n=$(( $n / 10 ))
done
OUTPUT
Enter a Number:
542
Sum is 11
P a g e 20 | 35
CS 1541: FOSS Lab
PROGRAM 6:
AIM:- Write a shell program to check whether the year is leap year or not.
PROGRAM:
read y
year=$y
y=$(( $y % 4 ))
if [ $y -eq 0 ]
then
else
fi
OUTPUT
Enter Year:
2016
Enter Year:
2017
P a g e 21 | 35
CS 1541: FOSS Lab
PROGRAM 7:
AIM:- Write a shell program to check whether the number is palindrome or not.
PROGRAM:
OUTPUT
232
232
Number is palindrome
321
123
P a g e 22 | 35
CS 1541: FOSS Lab
PROGRAM 8:
AIM:- Write a shell program to check whether the number is prime or not.
PROGRAM:
read a;
flag=0
do
b=`expr $a % $i`
if [ $b -eq 0 ]
then
flag=1
fi
done
if [ $flag -eq 1 ]
then
else
fi
P a g e 23 | 35
CS 1541: FOSS Lab
OUTPUT
2 is a prime number
P a g e 24 | 35
CS 1541: FOSS Lab
PROGRAM 9:
AIM:- Write a shell program to find the factorial of a number.
PROGRAM:
read n
fact=1
for((i=$n;i>=1;i--))
do
done
OUTPUT
Enter a number
The factorial of 3 is 6
P a g e 25 | 35
CS 1541: FOSS Lab
PROGRAM 10:
AIM:- Write a shell program to print the Fibonacci series up to N.
PROGRAM:
read Num
f1=0
f2=1
for (( i=0;i<Num;i++ ))
do
fn=$((f1+f2))
f1=$f2
f2=$fn
done
OUTPUT
011235
P a g e 26 | 35
CS 1541: FOSS Lab
PROGRAM 11:
AIM:- Write a shell program to check the number is Armstrong or not.
PROGRAM:
read n
m=` expr $n `
s=0
while [ $n -gt 0 ]
do
x=` expr $n % 10 `
d=` expr $x \* $x \* $x `
s=` expr $s + $d `
n=` expr $n / 10 `
done
if [ $m -eq $s ]
then
else
fi
P a g e 27 | 35
CS 1541: FOSS Lab
OUTPUT
153
Armstrong number
234
P a g e 28 | 35
CS 1541: FOSS Lab
PROGRAM 12:
AIM:- Write a shell program to copy the contents of two files.
PROGRAM:
read k
read n
if [ -f $k ]
then
cp $k $n
else
fi
OUTPUT
upper.sh
r1.sh
File copied
P a g e 29 | 35
CS 1541: FOSS Lab
PROGRAM 13:
AIM:- Write a shell program to print system configurations like.
1) Currently logged user and his logname
2) Your current shell
3) Your home directory
4) Your current path setting
5) Your current working directory
6) Show Currently logged number of users
PROGRAM:
nouser=`who | wc -l`
OUTPUT
Home Directory:/home/acer
Path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
P a g e 30 | 35
CS 1541: FOSS Lab
PROGRAM 14:
AIM:- Write a shell program to print the pattern.
PROGRAM:
makePyramid()
{
n=$1;
for((i=1;i<=n;i++)) Enter the user name
guest
acer
acer is logged in
do
for((k=i;k<=n;k++))
do
echo -ne " ";
done
for((j=1;j<=2*i-1;j++))
do
echo -ne "*"
done
echo;
done
}
makePyramid 5
OUTPUT
*
***
*****
*******
*********
P a g e 31 | 35
CS 1541: FOSS Lab
PROGRAM 15:
AIM:- Write Script to see current date, time, username, and current directory.
PROGRAM:
OUTPUT
Hello, acer
P a g e 32 | 35
CS 1541: FOSS Lab
PROGRAM 16:
AIM:- Write a shell program to check whether a given user is logged in or not.
PROGRAM:
read name
then
else
fi
OUTPUT
guest
acer
acer is logged in
P a g e 33 | 35
CS 1541: FOSS Lab
PROGRAM 17:
AIM:- Write a shell program to perform arithmetic operations using function.
PROGRAM:
case $op in
1) addition ;;
2) subtraction ;;
3) multiplication ;;
4) division ;;
5) exit
esac
P a g e 34 | 35
CS 1541: FOSS Lab
OUTPUT
12
Sum is16
P a g e 35 | 35