Unix Basics: by Rajesh Menghani
Unix Basics: by Rajesh Menghani
Unix Basics: by Rajesh Menghani
By Rajesh Menghani
Topics
• Unix Introduction
• The Shell
• Unix Commands
• Files and Directories
• Permissions
• Program and Process
• Make Utility
• Shell Programming
What’s UNIX?
•Name derived from Multics, a multi-user OS
• Make looks at the time stamp for each file in the chain and
compiles from the point that is required to bring every file in the
chain up to date. If any file is missing it is updated if possible.
What Make does ?
• Make builds object files from the source files
and then links the object files to create the
executable file.
• % make prog1
Explanation of Descriptor File
• make finds the target prog1 and sees that it depends on the object files
file1.o file2.o file3.o
• make next looks to see if any of the three object files are listed as targets.
– They are so make looks at each target to see what it depends on.
– make sees that file1.o depends on the files file1.cc and mydefs.h.
• Now make looks to see if either of these files are listed as targets
– since they aren't, it executes the commands given in file1.o's rule and
compiles file1.cc to get the object file.
• make looks at the targets file2.o and file3.o and compiles these object files in a
similar fashion.
• make now has all the object files required to make prog1 and does so by
executing the commands in its rule.
Dependency Rules
• A rule consist of three parts, one or more targets,
zero or more dependencies, and zero or more
commands in the following form:
• Commands:
– Each command in a rule is interpreted by a shell to be executed.
– By default make uses the /bin/sh shell.
– The default can be over ridden by using the macro SHELL =
/bin/sh or equivalent to use the shell of your preference.
– This macro should be included in every descriptor file to make
sure the same shell is used each time the descriptor file is
executed.
Shell Programming
• Shell scripting skills have many applications, including:
• Ability to automate tasks, such as
– Backups
– Administration tasks
– Periodic operations on a database via cron
– Any repetitive operations on files
• Increase your general knowledge of UNIX
– Use of environment
– Use of UNIX utilities
– Use of features such as pipes and I/O redirection
Examples of Shell Programming
• Store the following in a file named simple.sh and execute it
#!/bin/sh
# Show some useful info at the start of the day
date
echo Good morning $USER
cal
last | head -6
• Notice that the commands themselves are not displayed, only the
results
Storing File Names in Variables
• A variable is a name that stores a string
• It's often convenient to store a filename in a variable
• Store the following in a file named variables.sh and execute it
#!/bin/sh
# An example with variables
filename="/etc/passwd"
echo "Check the permissions on $filename"
ls -l $filename
echo "Find out how many accounts there are on
this system"
wc -l $filename