You are on page 1of 18

Linux Programming File

Submitted by:

Index
S. no
1. 2. 3. Shell Commands Shell Commands for Processes File commands Shell Scripting 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. Shell Script for Displaying a message To accept name, age of the user and display it on the screen To find whether a number is even or odd To print the Fibonacci series To calculate the simple interest To calculate the factorial of a number To find whether a year is leap year or not To calculate the average of numbers given at command line To print a seven digit number in alternate sequence To count occurrence of a particular digit in inputted number Reverse a String 7 8 9 10 11 12 13 14 15 16 17

Programs

Page No
1 5 6

Remark

T. Sign

Shell commands
i) Command for making directory
root@mca113:/# mkdir mca root@mca113:/# cd mca root@mca113:/mca# cat> PROJ.sem there are 60 students in PROJ sem root@mca113:/mca# cd PROJ there are 60 students in PROJ sem

ii) Command for displaying calendar


root@mca113:/# cal March 2011 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

iii)Command for displaying date root@mca113:/# date Tue Mar 29 13:11:10 IST 2011

iv) Command for doing echo on screen root@mca113:/# echo " how are you " how are you

v) Command for knowing id root@mca113:/# id uid=0(root) gid=0(root) groups=0(root)

vi) Command for knowing logname root@mca113:/# logname root

vii) Command for setting new password root@mca113:/# passwd Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully

viii) Command for removing directory root@mca113:/# rmdir PROJ

ix)

Command for listing of contents of file

root@mca113:/# ls a1 avearge.sh~ boot fact.sh home lib mnt niec proc sev test1 var sev~ test.c

a2 ave.sh vmlinuz alter.sh xyz.c ave.sh~

cdrom fact.sh~ initrd.img lost+found naina dev etc fib fib~ leap leap~ mca media

occur.sh root

nancy occur.sh~ sbin nancy.c opt

srv tmp

avearge.sh bin

selinux sys usr

x) Command for knowing total process root@mca113:/# who root root root tty7 pts/0 pts/2 2011-03-29 11:32 (:0) 2011-03-29 11:35 (:0.0) 2011-03-29 12:59 (:0.0)

xi) Command for knowing name of login person root@mca113:/# who am i root pts/2 2011-03-29 12:59 (:0.0)

xi) Command for knowing user name of login person root@mca113:/# uname Linux

Shell Commands for Processes


i)Command to sleep a process
root@mca112:/# sleep 100

ii)Command to send a process in background


root@mca112:/# bgbash: bg: current: no such job

iii)Command to get a process in forground


root@mca112:/# fgbash: fg: current: no such job

iv)Command to kill a process


root@mca112:/# killkill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

File commands
i) Command for counting total number of character line and word in a file
root@mca113:/# wc niec
1

4 15 niec

ii) Command for copying one file into another file


root@mca113:/# cp niec mca

iii)Command for knowing details of file


root@mca113:/# file niec niec: ASCII text, with no line terminators

iv)Command for renaming file


root@mca113:/# mv niec neeta

Shell Scripting
Program Display welcome screen
#WAP to display a simple welcome message echo "WELCOME TO SHELL SCRIPT"

Program To accept name, age of the user and display it on the screen
#WAP to accept name and age of the user and display it on the screen echo "Enter the name of the person: " read name echo "Now enter the age his age: " read age echo "The name of the person is $name and his age is $age"

Program To find whether a number is even or odd


#WAP to find whether a number is even or odd echo "To find whether a number is even or odd" echo "Enter a number: " read num tst=`expr $num % 2` if [ $tst -eq 0 ] then echo "The number you entered is even " else echo "The number is odd" fi

Program To print the Fibonacci series


#shell script to generate fibonacci series echo "how many fibonnaci numbers do u want " read limit a=0 b=1 d=1 echo "____________________________________________________" echo -n $a echo -n " " while test $d -le $limit do c=`expr ${a} + ${b}` echo -n $c echo -n " " b=$a a=$c d=`expr $d + 1` done

Program To calculate the simple interest


#WAP to calculate the simple interest echo "Enter the principle amount, rate of interest, time period(in months)" read principle read rate read time echo "You entered: \n principle: $principle\n rate of interest: $rate\n time period: $time" prod=`expr $principle \\* $rate \\* $time` interest=`expr $prod / 1200` echo "The simple interest is: $interest"

Program To calculate the factorial of a number


#WAP to calculate the factorial of a number echo "Enter the number " read num echo "The factorial is " i=1 fact=1 while [ $i -le $num ] do fact=`expr $fact \\* $i` i=`expr $i + 1` done echo "$fact"

Program To find whether a year is leap year or not


#WAP to find whether a year is leap year or not echo "Enter the year " read year tst=`expr $year % 4` if [ $tst -eq 0 ] then echo "The year is leap" else echo "The year is not a leap year" fi

Program To calculate the average of numbers given at command line


#WAP to calculate the average of numbers given at command line echo "The average is: " sum=0 i=0 for x in $* do sum=`expr $sum + $x` i=`expr $i + 1` done avg=`expr $sum / $i` echo "$avg"

Program To print a seven digit number in alternate sequence


#WAP to print a seven digit number echo " enter a seven digit number " read number len=`echo $number|wc -c` flag=1 while [ $flag -le $len ] do echo $number|cut -c$flag flag=`expr $flag + 2` done

in alternate

Program To count occurrence of a particular digit in inputted number


#shell script to count occurrence of a particular digit in inputted number" echo " enter any number :- " read num echo "enter the number to find occurence " read digit len=`echo -n $num|wc -c` echo "the length of number is $len" count=0 while [ $len -gt 0 ] do flag=`echo -n $num|cut -c $len` if [ $flag -eq $digit ] then count=`expr $count + 1 ` fi len=`expr $len - 1` done echo "$digit occured $count times in number($num)"

Program - Reverse a String


echo -n "Enter a string " read string echo $string len=`echo -n $string|wc -c` echo "No of characters in the string $len " while [ $len -gt 0 ] do rev=`echo -n $string|cut -c $len` echo -n $rev len=`expr $len - 1` done echo " "