MTC logo MTC logo Midlands Technical College
Prospective StudentsEnrolled StudentsFaculty & StaffCommunity Partners
About
Academics
Continuing Education
Distance Learning
Student Forms
Library
Class Schedule
News & Updates
Calendar
Directory
Site Map
Ask MTC
MTC Jobs Bookstore
Home
navigation
 
Search

 

About MTC Academics Continuing Education Distance Learning Student Forms Library Class Schedule News & Updates Calendar Directory Site Map Ask MTC MTC Jobs Bookstore Home


CPT 247


Quick Reference

Exercise

Shellscript1

 

More shell scripts

Additional shell scripts for you to practice.

Make sure you use the command chmod to change file permission to be executable.

   chmod 755 filename.sh

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

# Name this file readcommand.sh

# Usage readcommand.sh 1 2 3 4

#!/bin/sh
# Illustrate the use of positional parameters, user-defined
# variables and the read command

echo "What is your name?"
read name
echo "Well, $name, you typed $# arguments:"
echo "$*"

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

# Name this file addnumber.sh

# Usage : addnumber.sh 35 50

#!/bin/sh
# add two numbers

sum=`expr $1 + $2`
echo $sum

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

# Name this file testday.sh

#!/bin/sh
set `date`
if test $1 =  Fri
then
echo "Thank goodness it is Friday"
elif test $1 =  Sat  ||  test  $1  =  Sun
then
echo " You should not be working on the weekend."
echo " Log off the system and go home."
else
echo " It is not yet weekend. Go to work!"
fi

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

# Name this file  whileloop.sh

# Usage: whileloop.sh this is a test

#!/bin/sh
# print a message ten times
count=10
while test $count -gt 0
do
echo $*
count=`expr $count - 1`
done

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