0% found this document useful (0 votes)
56 views11 pages

Ex 1 Basic Linux Commands

The document provides examples of using various Linux commands and their syntax. It covers basic commands like date, cal, echo; file and directory commands like cat, pwd, mkdir; filters and pipes like head, tail, grep; simple functions; basic tests on numbers, strings and files; loops to calculate factorials; patterns and substitutions; and expansions using braces and tilde. Each command is demonstrated with the command syntax and sample outputs. The document is testing and explaining Linux/Unix commands and their usage through examples.

Uploaded by

301047 ktr.it.17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views11 pages

Ex 1 Basic Linux Commands

The document provides examples of using various Linux commands and their syntax. It covers basic commands like date, cal, echo; file and directory commands like cat, pwd, mkdir; filters and pipes like head, tail, grep; simple functions; basic tests on numbers, strings and files; loops to calculate factorials; patterns and substitutions; and expansions using braces and tilde. Each command is demonstrated with the command syntax and sample outputs. The document is testing and explaining Linux/Unix commands and their usage through examples.

Uploaded by

301047 ktr.it.17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

EX NO: 1 COMMAND SYNTAX

a) VERIFY THE UNIX BASIC COMMANDS

COMMAND1: DATE
[kavitha@localhost ~]$ date
Wed Jul 18 10:14:54 EDT 2007

COMMAND2: CAL
[kavitha@localhost ~]$ cal 11 2010
November 2010
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

COMMAND3: ECHO
[kavitha@localhost ~]$ echo "Good Morning"
Good Morning

COMMAND4: WHO AM I
[kavitha@localhost ~]$ who am i
kavitha pts/1 2007-07-18 09:38 (131.30.50.57)

COMMAND5: WHO

COMMAND6: CLEAR
[kavitha@localhost ~]$ clear

b) FILE AND DIRECTORY COMMANDS


COMMAND1: CAT
[kavitha@localhost ~]$ cat > test
Welcome to Prathyusha
Good Morning
Have a Nice Day
[kavitha@localhost ~]$

COMMAND2: PWD
[kavitha@localhost shell]$ pwd
/home/kavitha/shell

COMMAND3: MKDIR
[kavitha@localhost ~]$ ls
cpgm shell test
[kavitha@localhost ~]$ mkdir dir1
[kavitha@localhost ~]$ ls
cpgm dir1 shell test

COMMAD4: RMDIR
[kavitha@localhost ~]$ rmdir dir1
[kavitha@localhost ~]$ ls
cpgm shell test

COMMAND5: CD
[kavitha@localhost ~]$ cd shell
[kavitha@localhost shell]$

COMMAND6: CP
[kavitha@localhost ~]$ cp test new
[kavitha@localhost ~]$ ls
cpgm new shell test

COMMAND7: RM
[kavitha@localhost ~]$ rm new
[kavitha@localhost ~]$ ls
cpgm shell test

COMMAND8: LS
[kavitha@localhost ~]$ ls
cpgm shell test

COMMAND9: WC
[kavitha@localhost ~]$ cat test
Welcome to Prathyusha
Good Morning
Have a Nice Day
[kavitha@localhost ~]$ wc test
3 9 52 test
[kavitha@localhost ~]$
C) FILTERS AND PIPES

[kavitha@localhost ~]$ cat file


color
yellow
green
orange
gray
violet
pink
red
rose
blue
white
black
brown
cyan
meganta
silver
gold
maroon

COMMAND1: HEAD
[kavitha@localhost ~]$ head file
color
yellow
green
orange
gray
violet
pink
red
rose
blue

COMMAND2: TAIL
[kavitha@localhost ~]$ tail file
rose
blue
white
black
brown
cyan
meganta
silver
gold
maroon

COMMAND3: PAGE
[kavitha@localhost ~]$ ls -a\p
./ .bash_history .bash_profile cpgm/ file .kde/ test .zshrc
../ .bash_logout .bashrc .emacs .gtkrc shell/ .viminfo
[kavitha@localhost ~]$

COMMAND4: MORE
[kavitha@localhost ~]$ cat file
color
yellow
green
orange
gray
violet
pink
--More--(37%)

COMMAND5: GREP
[kavitha@localhost ~]$ grep a file
orange
gray
black
cyan
meganta
maroon

COMMAND6: SORT
[kavitha@localhost ~]$ sort file
black
blue
brown
color
cyan
gold
gray
green
maroon
meganta
orange
pink
red
rose
silver
violet
white
yellow
COMMAND7: PIPE
[kavitha@localhost ~]$ cat file | tr "[a-z]" "[A-Z]"
COLOR
YELLOW
GREEN
ORANGE
GRAY
VIOLET
PINK
RED
ROSE
BLUE
WHITE
BLACK
BROWN
CYAN
MEGANTA
SILVER
GOLD
MAROON
[kavitha@localhost ~]$

COMMAND8: TR
[kavitha@localhost ~]$ tr "[a-z]" "[A-Z]"
Good Morning
GOOD MORNING
[kavitha@localhost ~]$

EX NO: 2 SIMPLE FUNCTIONS

FUNCTION1: list()
[kavitha@localhost ~]$ list() { ls; }
[kavitha@localhost ~]$ list
cpgm file new shell test

FUNCTION2: user()
[kavitha@localhost ~]$ user() { who am i; }
[kavitha@localhost ~]$ user
kavitha pts/1 2007-07-18 09:38 (131.30.50.57)
FUNCTION3: combine()
[kavitha@localhost ~]$ combine() { ls;pwd;cal; };
[kavitha@localhost ~]$ combine
cpgm file new shell test
/home/kavitha
July 2007
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

EX NO: 3 BASIC TESTS

a) NUMERICAL TESTS

PROGRAM:
echo " enter Three numbers:"
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo " $a is great "
elif [ $b -gt $a -a $b -gt $c ]
then
echo " $b is great"
else
echo " $c is greatest"
fi

OUTPUT:
[kavitha@localhost shell]$ sh great.sh
enter Three numbers:
234
4 is greatest

[kavitha@localhost shell]$ sh great.sh


enter Three numbers:
253
5 is great

[kavitha@localhost shell]$ sh great.sh


enter Three numbers:
753
7 is great
[kavitha@localhost shell]$

b) STRING TESTS

PROGRAM:
echo " enter the first string"
read str1
echo " enter the second string"
read str2
if [ $str1 == $str2 ]
then
echo " strings are equal"
else
echo " strings are not equal"
fi

OUTPUT:
[kavitha@localhost shell]$ sh string.sh
enter the first string
hi
enter the second string
hi
strings are equal

[kavitha@localhost shell]$ sh string.sh


enter the first string
hi
enter the second string
hello
strings are not equal
[kavitha@localhost shell]$

c) FILE TESTS

PROGRAM:
echo "Enter the filename: "
read filename
if [ -f $filename ]
then
echo " $filename exists"
else
echo " $filename does not exists"
fi

OUTPUT:
[kavitha@localhost shell]$ sh filetest.sh
Enter the filename:
great.sh
great.sh exists

[kavitha@localhost shell]$ sh filetest.sh


Enter the filename:
process.c
process.c does not exists

EX NO: 4 LOOPS

PROGRAM:
echo "Enter the value of n: "
read n
i=1
fact=1
while [ $i -le $n ]
do
fact=`expr $fact "*" $i`
i=`expr $i + 1`
done
echo "factorial of $n is: $fact "

OUTPUT:
[kavitha@localhost shell]$ sh fact.sh
Enter the value of n:
6
factorial of 6 is: 720

[kavitha@localhost shell]$ sh fact.sh


Enter the value of n:
8
factorial of 8 is: 40320

EX NO: 5 PATTERNS AND SUBSTITUTIONS

a) PATTERNS

[kavitha@localhost ~]$ ls
cpgm file.sh new shell test1 test3 test5
file find.sh new.sh test test2 test4

PATTERN1: *
[kavitha@localhost ~]$ ls *.sh
file.sh find.sh new.sh

PATTERN2: ?
[kavitha@localhost ~]$ ls test?
test1 test2 test3 test4 test5

PATTERN3: [charcter1-character2]
[kavitha@localhost ~]$ ls test[1-3]
test1 test2 test3
b) SUBSTITUTIONS

SUBSTITUTION1: `command`
[kavitha@localhost ~]$ ls `pwd`
cpgm file.sh new shell test1 test3 test5
file find.sh new.sh test test2 test4

SUBSTITUTION2: &command
[kavitha@localhost ~]$ ls &pwd
[5] 3782
/home/kavitha
[kavitha@localhost ~]$ cpgm file.sh new shell test1 test3 test5
file find.sh new.sh test test2 test4

[5] Done ls --color=tty


[kavitha@localhost ~]$

EX NO: 6 EXPANSIONS

a) BRACE EXPANSION:
[kavitha@localhost ~]$ ls
cpgm file.sh new shell test1 test3 test5
file find.sh new.sh test test2 test4

[kavitha@localhost ~]$ mkdir color

[kavitha@localhost ~]$ mkdir color/{red,green,blue}

[kavitha@localhost ~]$ ls
color file find.sh new.sh test test2 test4
cpgm file.sh new shell test1 test3 test5

[kavitha@localhost ~]$ cd color


[kavitha@localhost color]$ ls
blue green red
[kavitha@localhost color]$

b) TILDE EXPANSION
[kavitha@localhost color]$ ls
blue green red

[kavitha@localhost color]$ ls ~
color file find.sh new.sh test test2 test4
cpgm file.sh new shell test1 test3 test5

[kavitha@localhost color]$ ls ~+
blue green red

You might also like