Ex 1 Basic Linux Commands
Ex 1 Basic Linux 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
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
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 ~]$
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
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
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
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
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
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
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 ~]$ ls
color file find.sh new.sh test test2 test4
cpgm file.sh new shell test1 test3 test5
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