Monday, 3 June 2013

Introduction to Shell Programming and Scripts

Introduction to Shell Programming

What is Shell Programming?

• putting UNIXTM commands in a file
• almost always special-purpose code
• often one-time code
• seldom used where speed is important
• often used to manipulate files

Basic shell scripts

Some of the familiar commands often useful in shell scripts are

cat - concatenate files
cp - copy a file
date -print the date and time
grep - scan for a string
head - show first lines of a file
tail - show last lines of a file
mv - move or rename a file
rm -f - remove files (silently)
wc - count lines, words, characters
        wc output format varies between systems

path names of files and directories
schedule relative
/home/37/jones/schedule absolute

wild cards in filenames
* matches zero or more characters
? matches exactly 1 character

redirection
> redirects std-out to a file
>> appends std-out to a file
< redirects std-in from a file

Pipelines of Commands

send std-out of one command to std-in of another
look e - shows spelling words that begin with e
look e | more - displays the words one page at a time
often use echo to feed a pipeline
echo "count me" | wc - prints 1 2 9
echo * | wc -w - counts files in current directory

About Shell Scripts

Type shell program text in a file using an editor:
#! /bin/sh
# this is a comment
body of program
to continue a line append \
this is the rest of the continued line
exit 0

chmod +x - scriptfile make the file executable
scriptfile - execute the program
sh -v scriptfile - print input lines as read
sh -x scriptfile - print commands as executed

shell programs often use temporary files in /tmp
and send unwanted outputs to /dev/null

Example Scripts

#!/bin/sh
# hi
echo "Hello, world!"
exit 0

unix[1] hi
Hello, world!
-----------------------------------
#!/bin/sh
# himike
name=Mike
echo "Hello, $name!"
exit 0

unix[2] himike
Hello, Mike!

-----------------------------------

Some more about Shell Programming

Exercises

some hints are given in the files
exer1, exer2, exer2.aix, and exer3.
1. Write a script that counts files. 
(a) First make it count the files in the current directory.
(b) Now modify your script to accept a parameter that is the name of a directory, and count
the files in that directory. Try this version on the current directory (.) and on the /afs/rpi.edu/campus/doc directory.
(c) Further modify your script so that if it is invoked without a parameter it prints out an explanation of how to use it.
2. If the ls command is given the name of a single extant file it merely prints that filename back out. 
(a) Write a script myls that behaves like ls except that when a single filename parameter is supplied it produces the output that ls -l would give for the file.
(b) Revise your script so that when a single filename parameter is given the output produced is the filename followed by the date and time of its most recent change and then the size of the file in bytes.
3. A script isyes is required that sets its exit code to 0 if its parameter is some variation of y or yes, and to 1 otherwise.
(a) Assume the only acceptable parameter values meaning “yes” are y, yes, Y, and YES, and solve the problem using only
shell programming features we have discussed.
(b) Simplify and generalize your script by using tr a-z A-Z, which reads from std-in, translates to upper case, and writes to std-out.
4. Write a script that adds up the sizes reported by ls for the files in the current directory. The script should print out only the total number of bytes used.


Sunday, 2 June 2013

C program to find the average of three numbers

Aim:

To write a c program to find the average of three numbers

Algorithm:

1.Start the program
2.Get three numbers from the users
3.Add three numbers and divide it by 3
4.Print the average value
5.Stop the program

C Program - Average of three numbers

#include<stdio.h>

#include<conio.h>
void main()
{
int n1,n2,n3;
float average;
clrscr();
printf("Enter the three numbers");
Scanf("%d %d %d",&n1,&n2,&n3);
average =(n1+n2+n3)/3.0;
printf("average of above three numbers is %.4f",average);
getch();
}

Output for  Average of three numbers C program

Enter three numbers
56
78
93
Average of above three numbers is 75.6667

Result
Thus the C program to find the average of three numbers is written and executed successfully.

C program to find sum of two numbers - Adding two numbers

Algorithm:

1.Start the program
2.Read two numbers from users
3.Add two numbers using addition operator
4.Print the sum value
5.Stop the program

C program - Sum of two numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,num3;
clrscr();
printf("Enter first number \n");
scanf("%d",&num1);
printf("Enter second number \n");
scanf("%d",&num2);
num3 = num1+num2;
printf("sum is %d \n",num3);
getch();
}

Output for  Sum of two numbers c program 

Enter first number
10
Enter second number
20
sum is
30

Result:

Thus the c program to add two numbers is written and executed successfully.

Friday, 31 May 2013

TNDTE Diploma Results April 2013

TNDTE Diploma April 2013 Result announcing date :

Many of the students are telling TNDTE Diploma Results April 2013 will be published om 31-05-2013.But there is very less chance to publish TNDTE Diploma April 2013 results on 31st may 2013.

The students who wrote TNDTE Diploma exams can get your results in this website.Mostly TNDTE Diploma Results will be announced in the first week of June 2013.If the TNDTE Diploma Results announced you can see your Diploma April 2013 results by clicking the links provides below.

Hope all the students will get good marks.The final year students who wrote diploma exams are eagerly waiting for their results than lower year students because many of the students was paid fees in many engineering college for admission.But no problem You will get pass mark.Pray well students.All the Best by Students Blog100


Popular Searches:
  • TamilNadu Diploma results 2013
  • TN Diplamo april 2013 result
  • l scheme diploma result
  • k scheme diplomo result 2013