You are on page 1of 3

Any shell command 1) can be executed either by typing it on the

command line or 2) by putting it in a shell script file and then


execute that script:
1) Start:
$ command
2)
script containts:
- - command
- - Posible starts:
$ script or $ ./script or $ sh script
By above "command" or "script" we denote the following:
command name / script file name,
arguments: options, files, expressions, and
I/O standard redirections: < << > >> <& >&
If a command must be represented with two (or more) lines,
all lines, except the last, must fe finish with \<ENTER>
(the sequences of characters '\\' '\n').
Two consecutive commands must be separate by:
; && || \n (new line) | (pipe connection) & (background execution).
More commands can be grupping by ( commands ) or { commands }
A shell script is a regular text file with execution permissions
(use the command chmod 755 filename).
It lines contains shell commands and / or directives for flow of
command execution.
The comments in shell scripts start with the character # and can
continue until the end of the line.
It is recomended, but not mandatory, that the shell script start
with a special comments like:
#!/bin/sh or #!/bin/sh or #!/bin/ksh . . .
This special comment define the Shell interpreter for the current script.
For Shell programming do'nt forgets the following entities:
Flow control (directives):
if, case, for, while, until, true, false, break, continue
Interaction with standard I/O and parameters:
shift, read, readonly, sleep, exit, echo
Utilities:
test (equivalent with "[ ..]"), export, expr, basename,
(reversed apostrophes)
Commands for files and directories:
ls, pwd, cat, find, locate, file, more, less, rm, mkdir, rmdir,
cp, mv, cd, chmod, chown, ln, touch, du, cut, sort, uniq, cmp,

diff, head, tail, wc, split


User information:
finger, w, who, ps, last, id, users
Network information:
netstat, ping, hostname, host, ftp, ftpwho
Other commands:
clear, date, mail, uptime, df, fg, bg
Details about all the commands above can be found from the UNIX
manual pages using the command man followed by the name of the
command you want to know more about.
AN EXAMPLE of script shell for the following problem:
Write a shell script that receives as command line parameters a
directory name D and a number N. Display the first N lines from
each file in directory D and it's subdirectories for which the
user has read and execute permissions.
Solution (file shell1.sh):
#!/bin/bash
# Check if there are 2 parameters on the command line
# The special variable $# gives the number of parameters given to
# the script
if [ ! $# -eq 2 ]
then echo usage: shell1.sh dirname number
exit 1
fi
# Check if the first parameter is a directory name
if [ ! -d $1 ]
then echo $1 is not a directory\!
exit 1
fi
D=$1 # Can use $1, but use D like in above text of the problem
N=$2 # Can use $1, but use N like in above text of the problem
#
#
#
#
#
#
#

The command `find $D -perm -u=rx -type f` will find all the
files in directory $D with read (r) and excute (x) permissions
The command could also be written as `find $D -perm -500 -type
f` A second alternative is the test command with the options -f
-r -x Reversed apostrophes around a command make it so that
their content (initially the command itself) is replaced by the
output of the command

for fis in `find $D -perm -u=rx -type f`; do


echo $fis
# disply the file name
head -$N $fis # display the first $2 lines in the file
done
START:
First, the execution right must be establish:
$ chmod 755 shell1.sh

Now, start from the parent directory and the first 5 lines:
$ ./shell1.sh .. 5

You might also like