0% found this document useful (0 votes)
20 views10 pages

SOS!!!shell-pgming Basics 4EPOKA

The document discusses shell programming on UNIX systems. Shell programs can perform tasks using programming constructs like variables, control flow, and calling UNIX commands. The document provides instructions on writing shell programs including naming them, making them executable, using comments and formatting for readability.

Uploaded by

Alketa Alia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

SOS!!!shell-pgming Basics 4EPOKA

The document discusses shell programming on UNIX systems. Shell programs can perform tasks using programming constructs like variables, control flow, and calling UNIX commands. The document provides instructions on writing shell programs including naming them, making them executable, using comments and formatting for readability.

Uploaded by

Alketa Alia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CSCI 330

THE UNIX SYSTEM


Shell Programming
UNIX COMMAND INTERPRETERS

CSCI 330 - The UNIX System


2
INTRODUCTION TO SHELL PROGRAMMING
 Shell programming is one of the most powerful
features on any UNIX system

CSCI 330 - The UNIX System


 If you cannot find an existing utility to
accomplish a task, you can build one using a shell
script

3
SHELL PROGRAM STRUCTURE
 A shell program contains high-level programming
language features:

CSCI 330 - The UNIX System


⚫ Variables for storing data
⚫ Decision-making control (e.g. if and case statements)
⚫ Looping abilities (e.g. for and while loops)
⚫ Function calls for modularity
 A shell program can also contain:
⚫ UNIX commands
⚫ Pattern editing utilities (e.g. grep, sed, awk)

4
YOUR SHELL PROGRAMMING
LIBRARY
 Naming of shell programs and their output
⚫ Give a meaningful name

CSCI 330 - The UNIX System


⚫ Program name example: findfile.csh
⚫ Do not use: script1, script2
⚫ Do not use UNIX command names
 Repository for shell programs
⚫ If you develop numerous shell programs, place them
in a directory (e.g. bin or shellprogs)
⚫ Update your path to include the directory name
where your shell programs are located

5
STEPS TO CREATE SHELL PROGRAMS
 Specify shell to execute program
⚫ Script must begin with #! (pronounced
“shebang”) to identify shell to be executed

CSCI 330 - The UNIX System


Examples:
#! /bin/sh (defaults to bash)
#! /bin/bash
#! /bin/csh
#! /usr/bin/tcsh
 Make the shell program executable
⚫ Use the “chmod” command to make the
program/script file executable
6
FORMATTING SHELL PROGRAMS
 Formatting of shell programs
⚫ Indent areas (3 or 4 spaces) of programs to indicate
that commands are part of a group

CSCI 330 - The UNIX System


⚫ To break up long lines, place a \ at the end of one
line and continue the command on the next line
 Comments
⚫ Start comment lines with a pound sign (#)
⚫ Include comments to describe sections of your
program
⚫ Help you understand your program when you look at
it later
7
STEPS OF PROGRAMMING
 Guidelines:
⚫ use good names for

CSCI 330 - The UNIX System


 script
 variables

⚫ use comments
 lines start with #
⚫ use indentation
to reflect logic
and nesting

8
EXAMPLE: “HELLO” SCRIPT
#! /bin/csh
echo "Hello $USER"

CSCI 330 - The UNIX System


echo "This machine is `uname -n`"
echo "The calendar for this month is:"
cal
echo "You are running these processes:"
ps

9
EXAMPLE SCRIPT OUTPUT
% chmod u+x hello
% ./hello
Hello ege!

CSCI 330 - The UNIX System


This machine is turing
The calendar for this month is
February 2008
S M Tu W Th F S
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
You are running these processes:
PID TTY TIME CMD
24861 pts/18 0:00 hello.csh 10
24430 pts/18 0:00 csh

You might also like