Introduction to Shell Programming and Scripts - Anna University Multiple Choice Questions

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.