. Display commands File manipulation commands Directory manipulation commands Process commands Grouping commands
b) Shell Programs 1. Write shell script for simple shell programming. 2. Write shell script for conditional statements. 3. Write shell script testing and loops statements. 4. Greatest of three numbers 5. Conversion of temperature 6. Checking the number prime or not 7. Raising a number to its power 8. Armstrong number 9. Fibonacci series 10. Factorial of a given number 11. Employee details 12. Multiplication table 13. Menu driven calculator c) Unix C programs
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
Function with no arguments & no return values Function with arguments & return values
Dynamic memory allocation Dynamic allocation of a string & print it backwards Binary search using function Factorial in finding ncr using function Cosine values using function Product of matrix using arrays Creation & reading the content of a file Appending content to the file
www.eeecube.blogspot.com
UNIX COMMANDS
www.eeecube.blogspot.com
INTRODUCTION TO UNIX
UNIX: It is a multi-user operating system. Developed at AT & T Bell Industries, USA in1969. Ken Thomson along with Dennis Ritchie developed it from MULTICS (Multiplexed Information and Computing Service) OS. By 1980, UNIX had been completely rewritten using C language. LINUX: It is similar to UNIX, which is created by Linus Torualds. All UNIX commands works in Linux also and it is available free of cost. The main feature of Linux is coexisting with other OS. STRUCTURE OF A LINUX SYSTEM: It consists of three parts.
www.eeecube.blogspot.com
USERS
UNIX SYSTEM
TOOLS AND APPLICATIONS
SHELLS
COMPUTER HARDWARE
UNIX KERNEL: Kernel is the core of the UNIX OS. It controls all tasks, schedule all processes and carries out all the functions of OS. Decides when one program stops and another starts. SHELL: Shell is the command interpreter in the UNIX OS. It accepts command from the user and analyses and interprets them.
www.eeecube.blogspot.com
1. DISPLAY COMMANDS
a)
b)
cal used to display the calendar syn: $ cal 2 2009 echo used to print the message on the screen. Syn: $ echo text ls used to list the files. Your files are kept in a directory. Syn: $ ls
All files (include files with . prefix) Long detail (provide file statistics) Order by creation time Sort by access time (or show when last accessed together with l) Order by size Reverse order Mark directories with / ,executable with * , symbolic links with @ , local sockets with = , named pipes (FIFOs) with | Show file size Human Readable, show file size in Kilo Bytes & Mega Bytes (h can be used together with l or -s) List all the files whose name begin with alphabets From a to m List all the files whose name begins with a or A
c)
d)
ls s ls l ls t ls u ls s ls r ls f ls s ls h ls [a-m]* ls [a]*
Eg:
www.eeecube.blogspot.com
e)
lp used to take printouts syn: $ lp filename man used to provide manual help on every UNIX commands. Syn: $ man unixcommand $ man cat who & who am i it displays data about all users who have logged in to the system currently. The next command displays about current user only. Syn: $ who $ who am i uptime tells you how long the computer has been running since its last reboot or power-off. Syn: $ uptime uname it displays the system information such as hardware platform, system name and processor, OS type. Syn: $ uname a hostname displays and set system host name syn: $ hostname bc stands for best calcualtor
$ bc 10/2*3 15 quit $ bc scale =1 2.25+1 3.35 quit $ bc ibase = 2 obase = 16 11010011 89275 1010 Quit $ bc sqrt(196) 14
f)
g)
h)
i)
j)
k)
www.eeecube.blogspot.com
a)
cat this create, view and concatenate files. Creation: Syn: $ cat > filename Viewing: Syn: $ cat filename Add text to an existing file: Syn: $ cat >> filename Concatenate: Syn: $ cat file1 file2 > file3 $ cat file1 file2 >> file3 (no overwriting of file3)
b)
grep used to search a particular word or pattern related to that word from the file. Syn: $ grep searchword filename Eg: $ grep anu student rm deletes a file from the file system syn: $ rm filename touch used to create a blank file. Syn: $ touch file names cp copies the files or directories syn: $ cp source file destination file eg: $ cp student stud mv to rename the file or directory syn: $ mv old file new file Eg: $ mv i student student list (-i prompt when overwrite) cut it cuts or pick up a given number of character or fields of the file. Syn: $ cut <option> <filename> Eg: $ cut c filename $ cut c 1 -10 emp $ cut f 3,6 emp
c)
d)
e)
f)
g)
www.eeecube.blogspot.com
$ cut f 3-6 emp -c cutting columns -f cutting fields h) head displays 10 lines from the head (top) of a given file syn: $ head filename eg: $ head student To display the top two lines: $ head -2 student i) tail displays last 10 lines of the file syn: $ tail filename eg: $ tail student To display the bottom two lines; $ tail -2 student chmod used to change the permissions of a file or directory. Syn: $ chmod category operation permission file Where, category is the user type Operation is used to assign or remove permission Permission is the type of permission File are used to assign or remove permission
Category u users g group o others a - all Operation + assign - remove = assign absolutely Permission r read w write x- execute
j)
Examples: $ chmod u-wx student removes write and execute permission for users $ chmod u+rw, g+rw student assigns read and write permission for users and groups $ chmod g=rwx student assigns absolute permission for groups of all read, write and execute permissions
.
www.eeecube.blogspot.com
k)
wc it counts the number of lines, words, character in a specified file(s) with the options as l, -w, -c syn: $wc l filename $wc w filename $wc c filename
www.eeecube.blogspot.com
3. DIRECTORY COMMANDS
a)
b)
rmdir it is an utility for deleting empty directories. Syn: $ rmdir directory name
c)
cd changes the current directory of the shell. Syn: $ cd ~ (stores the path to your home directory) $ cd.. (changes to parent directory) $ cd
d)
pwd (Print Working Directory) shows the current directory. Syn: $ pwd
www.eeecube.blogspot.com
4. PROCESS COMMANDS
a)
b)
c)
d)
telnet connect to remote machine using the telnet protocol syn: $ telnet
www.eeecube.blogspot.com
5. GROUPING COMMANDS
a)
The semicolon (;) - used ot execute more than one command at a time eg: $ who ; date ; ls
b)
The && operator signifies the logical AND operation. It means that only if first command is successfully executed, then the nest command will be executed. Eg: $ ls marks && date
c)
The || operator signifies the logical OR operation. It means the first command will happen to be unsuccessful, it will continue to execute next command. Eg: $ ls marks || date
www.eeecube.blogspot.com
UNIX EDITOR
AIM: To study the UNIX editor vi and EMACS CONCEPT: Editor is a program that allows user to see a portions a file on the screen and modify characters and lines by simply typing at the current position. UNIX supports variety of Editors. They are: ed ex vi EMACS vi - vi is stands for visual. vi is the most important and powerful editor. vi is a full screen editor that allows user to view and edit entire document at the same time. vi editor was written in the University of California, at Berkley by Bill Joy, who is one of the co-founder of Sun Microsystems. Features of vi: It is easy to learn and has more powerful features. It works in great speed and is case sensitive. vi has powerful undo functions and has 3 modes: command mode insert mode Escape or ex mode In command mode, no text is displayed on the screen. In Insert mode, it permits user to edit insert or replace text. In escape mode, it displays commands at command line. Moving the cursor with the help of h,l,k,j,I,etc
www.eeecube.blogspot.com
EMACS Editor Motion Commands: M -> Move to end of file M -< Move to beginning of file C -v Move forward a screen M -v Move backward a screen C -n Move to next line C -p Move to previous line C -a Move to the beginning of the line C -e Move to the end of the line C -f Move forward a character C -b Move backward a character M -f Move forward a word M -b Move backward a word EMACS help system command options A B C F I K L M S T V W C -c C -d C -n C -w List all commands matching the specified word. List all key mappings. Describe any key sequence pressed. Describe the specified function Start up the info browser Fully describe the result of a particular key sequence Show the last 100 characters you typed Describe the current mode you are in List a command syntax table. Start a EMACS tutorial Define and describe the specified variable Indicate what keystroke invokes a particular function EMACS copyright and distribution information EMACS ordering information Recent EMACS changes EMACS warranty
www.eeecube.blogspot.com
Deletion Commands: DEL C -d M -DEL M -d C -x DEL M -k C -k C-x u delete the previous character delete the current character delete the previous word delete the next word delete the previous sentence delete the rest of the current sentence delete the rest of the current line undo the last edit change
y n q !
Change the occurence of the pattern Dont change the occurence, but look for the other Dont change. Leave query replace completely Change this occurence and all others in the file
www.eeecube.blogspot.com
SHELL PROGRAMMING
www.eeecube.blogspot.com
RESULT:
AIM: To write a program to find the greatest of three numbers. ALGORITHM: STEP 1: Start the program. STEP 2: Enter any three numbers. STEP 3: Read the values as a, b and c. STEP 4: If a greater than b and greater than c, print the value of a as the greatest number. STEP 5: Else if b is greater than c, print the value of b as greatest number. STEP 6: Else print the value of c as the greatest number. STEP 7: Stop the program.
www.eeecube.blogspot.com
www.eeecube.blogspot.com
AIM: To Conversion between different temperature scales using shell program ALGORITHM: STEP 1: Start the program STEP 2: Input the choice as 1 or 2 STEP 3: Is choice is 1 then goto step4 otherwise goto step 7 STEP 4: Input temperature in Celsius STEP 5: Calculate Fahrenheit F =((9/5)*c) +32 STEP 6: Print Fahrenheit F and goto step 10 STEP 7: Input temperature in Fahrenheit STEP 8: Calculate Celsius C=((5/9)*(f-32)) STEP 9: Print Celsius C STEP 10: Stop the program
www.eeecube.blogspot.com
read tc tf=$( echo scale=2; (( 9 / 5 ) * $tc ) + 32 | bc ) echo $tc C = $tf F elif [ $choice eq 2 ] echo Enter the Farenheit temperature F : read tf tc=$( echo scale=2; ( 5 / 9 ) * ( $tf 32 ) | bc ) echo $tf F = $tc C else echo Please select 1 or 2 exit 1 fi
output:
Converting between different temperature scales 1. Convert Celsius into Farenheit 2. Convert Farenheit into Celsius Select your choice (1 or 2) : Enter the Celsius temperature C : 37 37 C=98.60F
Converting between different temperature scales 1. Convert Celsius into Farenheit 2. Convert Farenheit into Celsius Select your choice (1 or 2) : Enter the Farenheit temperature F : 100 100 F=37.40 C
www.eeecube.blogspot.com
AIM: To write a program to find number is prime or not. ALGORITHM: STEP 1: Start the program STEP 2: Input number n STEP 3: Assign i=2, j=o STEP 4: Is i<n then r=n%i. otherwise go to step 8 STEP 5: Is r=0 then increment I and j value by i. otherwise go to step 6 STEP 6: Increment I value by one STEP 7: Is j=0 then print number is prime and go to step 10 STEP 8: Is j!=0 then print number is not a prime number STEP 9: Stop the program
www.eeecube.blogspot.com
fi i=$(( $i + 1 )) done if test $j eq 0 then echo Number is Prime else echo Number is Not Prime fi
www.eeecube.blogspot.com
AIM: To find the value of one number raised to the power of another using shell program ALGORITHM: STEP 1: Start the program STEP 2: Input number and power values STEP 3: Assign i=1 and product=1 STEP 4: If i<power then calculate product=product* number and increment I value by 1. STEP 5: Repeat step 4 STEP 6: Print the result STEP 7: Stop the program
www.eeecube.blogspot.com
www.eeecube.blogspot.com
AIM: To write a program to find the Armstrong numbers between 1 to 500. ALGORITHM: STEP 1: Start the program. STEP 2: When I equal to 0 and less than or equal to 500, calculate increment value of i. STEP 3: Assign value of I to temp and n. STEP 4: Assign value of ams equal to zero. STEP 5: When n not equal to zero calculate d equal to n modulus 10, ams equal to product of ams and cube of d. Then find n equal to n divided by 10. STEP 6: If temp equal to ams then print the value of ams. STEP 7: Thus for each value of I, values of ams is printed. STEP 8: Stop the program.
www.eeecube.blogspot.com
while test $n -ne 0 do d=$(( $n % 10 )) ams=$(( $ams + $d * $d * $d )) n=$(( $n / 10 )) done if test $temp -eq $ams then echo $ams fi done
www.eeecube.blogspot.com
AIM: To generate Fibonacci series. ALGORITHM: STEP 1: Start STEP 2: Get the value s from the user STEP 3: Assign a=0,b=1 and print a,b values STEP 4: Assign i=2 STEP 5: If i<n then goto step 6 otherwise goto step 9 STEP 6: Calculate c=a+b,i=i+1 and assign a=b, b=c STEP 7: Print the value of c STEP 8: Repeat the value of c. STEP 9: Stop the program
www.eeecube.blogspot.com
www.eeecube.blogspot.com
AIM: To calculate factorial of a given number using shell program ALGORITHM: STEP 1: Start the program STEP 2: Input number n STEP 3: Initialize the value of I and factorial f to 1 STEP 4: Is the value of I less than the number find the value of factorial by multiplying i and f. STEP 5: Increment the value of i by 1 STEP 6: Print the result STEP 7: Stop the program
www.eeecube.blogspot.com
www.eeecube.blogspot.com
AIM: To write a program to display the employee details. ALGORITHM: STEP 1: Start the program STEP 2: Read the number of employees. STEP 3: Using while loop get the employee details as empno, ename, bsal. STEP 4: For the given bsalary using ifelse statement get the hra, da, ta. STEP 5: Calculate the salary STEP 6: Display the employee details STEP 7: Stop the program.
www.eeecube.blogspot.com
hra=1000 da=700 ta=500 elif test $bsal -lt 10000 -a $bsal -ge 5000 then hra=750 da=500 ta=350 elif test $bsal -lt 5000 -a $bsal -ge 3000 then hra=500 da=300 ta=200 else hra=300 da=200 ta=100 fi netsal=$(( $hra + $da + $ta + $bsal )) echo -----------------------------echo PAY SLIP OF $ename echo -----------------------------echo Empname = $ename echo Empno = $eno echo basicpay = $bsal echo HRA = $hra echo TA = $ta echo DA = $da echo Netpay = $netsal done
www.eeecube.blogspot.com
OUTPUT Enter the number of employees 1 Enter the Employee ID Number 1234 Enter the Employee name Mr.V.ArunKumar Enter the employees basic salary 12000 -----------------------------PAY SLIP OF Mr.V.ArunKumar -----------------------------Empname = Mr.V.ArunKumar Empno = 1234 basicpay = 12000 HRA = 1000 TA = 500 DA = 700 Netpay = 14200
www.eeecube.blogspot.com
AIM: To generate multiplication table using shell program ALGORITHM: STEP 1: Start the program STEP 2: Input number n STEP 3: If i<10 then m=n*i STEP 4: Print the value of I,n and m in multiplication table format STEP 5: Increment the value of i. STEP 6: Stop the program.
www.eeecube.blogspot.com
OUTPUT
Enter the number 2 1*2=2 2*2=4 3*2=6 4*2=8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18 10*2=20
www.eeecube.blogspot.com
AIM: To write a program to display the menu driven calculator. ALGORITHM: STEP 1: Start the program. STEP 2: Read the option and operators a,b STEP 3: Evaluate the expression based on the user option a) If operator is + then result is a+b b) If operator is - then result is a-b c) If operator is x or X then result is a*b d) If operator is / then result is a/b STEP 4: Print the result. STEP 5: Stop the program
echo Menu echo 1. Addition echo 2. Subtraction echo 3. Multiplication echo 4. Division echo 5. Exit echo Enter the choice read n case $n in 1) .
www.eeecube.blogspot.com
echo Enter the two numbers read a read b c=$(( $a + $b )) echo Addition of $a and $b is $c ;; 2) echo Enter the two numbers read a read b c=$(( $a - $b )) echo Subtraction of $a and $b is $c ;; 3) echo Enter the two numbers read a read b c=$(( $a * $b )) echo Multiplication of $a and $b is $c ;; 4) echo Enter the two numbers read a read b c=$(( $a / $b )) echo Division of $a and $b is $c ;; *) echo Wrong Selection ;; esac .
www.eeecube.blogspot.com
www.eeecube.blogspot.com
C PROGRAMMING
www.eeecube.blogspot.com
Program: #include<stdio.h> int sum(void); main() { sum(); } int sum(void) { int num1, num2, num3; printf(Enter two numbers: \n); scanf(%d%d, &num1, &num2); num3=num1+num2; printf(Summation of %d and %d is %d \n, num1, num2, num3); }
OUTPUT FOR FUNCTION NO ARGUMENTS AND NO RETURN VALUES Enter two numbers: 10 20 Summation of 10 and 20 is 30
www.eeecube.blogspot.com
Program: #include<stdio.h> #include<conio.h> int largest(int, int); void main() { int a,b,big; printf(Enter two numbers : \n); scanf(%d%d, &a, &b); big=largest(a,b); printf(Largest Element = %d, big); } int largest(int a1, int b1) { if(a1>b1) return a1; else return b1; }
OUTPUT FOR FUNCTION ARGUMENTS AND RETURN VALUES Enter two numbers 15 25 Largest Element = 25
www.eeecube.blogspot.com
www.eeecube.blogspot.com
19. ALLOCATE SPACE FOR A STRING DYNAMICALLY AND PRINT THE STRING BACKWARDS
Program: #include<stdio.h> #include<string.h> int main() { char *s; register int i; s=malloc(80); if(!s) { printf(Memory request failed................. \n); exit(1); } gets(s); for(i=strlen(s)-1; i>=0; i--) putchar(s[i]); free(s); return 0; }
Hai iaH
www.eeecube.blogspot.com
www.eeecube.blogspot.com
www.eeecube.blogspot.com
www.eeecube.blogspot.com
Value of ncr = 10
www.eeecube.blogspot.com
cos(x) ");
%6.2f", x,cosine(x));
float cosine(float x) { float s, term; int i,k; x=x*3.14/180; s=0; term=1; i=0; for(k=1;k<=15;k++) { s=s+term; term=term*x*x*(-1) / ((i+1) * (i+2)); i=i+2; } return(s); }
www.eeecube.blogspot.com
----------------------------------------------------------------------
x in degrees
0 30 60 90 120 150 180
cos(x)
1.0 0.87 0.50 0.00 -0.50 -0.87 -1.00
----------------------------------------------------------------------
-------------------------------------------------------------------
www.eeecube.blogspot.com
www.eeecube.blogspot.com
www.eeecube.blogspot.com
For A matrix How many rows and columns ? 3 Enter A matrix values 3 -2 3 2 0 2 1 4 -1 3
For B matrix How many rows and columns? 3 Enter B matrix values 6 2 4 4 1 -5 -1 2 4 3
Resultant Matrix is
26
5 18 -3
4 -28 18 19
www.eeecube.blogspot.com
www.eeecube.blogspot.com
while (!feof(efile)) { printf("\n %d \t %-20s %0.2f",erec.eno,erec.ename,erec.bpay); fread(&erec, sizeof(erec),1,efile); } printf("\n --------------------------------------------------"); fclose(efile); return 0; }
www.eeecube.blogspot.com
Enter the number of Employees: 2 Enter the 1 employee details : Enter the Employee number: 101 Employee name: xxxx Basic Pay: 65433.4
Enter the 2 employee details : Enter the Employee number: 102 Employee name: yyyy Basic Pay: 7000 After adding content to the file ----------------------------------------------------------Emp.no Employee name Basic Pay ---------------------------------------------------------101 102 xxxx yyyy 65433.4 7000.0
----------------------------------------------------------
www.eeecube.blogspot.com
#include<stdio.h> #include<ctype.h> struct emp { int eno; char ename[20]; float bpay; }; FILE *efile; int main() { struct emp erec; efile = fopen("EMPLOY.DAT", "a"); printf("\nEnter the Employee number"); scanf("%d",&erec.eno); printf("Employee Name"); scanf("%s",erec.ename); printf("Basicpay"); scanf("%f",&erec.bpay); fwrite(&erec,sizeof(erec),1,efile); fclose(efile); printf(" After Appending the content of file"); efile=fopen("EMPLOY.DAT","r"); printf("\n--------------------------------------------------"); printf("\n Emp.no Employee name Basic Pay"); printf("\n --------------------------------------------------"); fread(&erec,sizeof(erec),1,efile); while (!feof(efile)) { printf("\n %d \t %-20s %0.2f",erec.eno,erec.ename,erec.bpay); fread(&erec, sizeof(erec),1,efile); } printf("\n --------------------------------------------------\n"); fclose(efile); return 0; }
.
www.eeecube.blogspot.com
Enter the Employee number: 103 Employee name: zzzz Basic Pay: 12000
After Appending the content of file ----------------------------------------------------------Emp.no Employee name Basic Pay ---------------------------------------------------------101 102 103 xxxx yyyy zzzz 65433.4 7000.0 12000.0
----------------------------------------------------------
www.eeecube.blogspot.com