Unix
Unix
Introduction to Unix
Apoorva Mathur
Topics To Cover
• What is Unix
• The Unix File System
• Unix Security
• Process Control and Pipes
• The Unix Toolbox
• Shell Programming
• Makefiles
• More Information
What is Unix?
/
usr
grid hm
pkgs
The Unix File System
• To copy a file :
cp /hm/kanwar/tmp/haha . ( copies haha in cwd )
cp /hm/kanwar/tmp/haha ./hoho ( copy haha as hoho in
cwd)
The Unix Security
owner name
process 1
haha
~
~
~
Basic vi
• head • tail
• prints first few lines of files • prints the last part of a
• head [-n] [file1] [file2] ... file
n = no. of lines • tail [+|- n] [file]
• By default, n = 10 + starts wrt beginning of file.
- starts relative to end of
file.
• default is -10.
find
• set • setenv
– displays the values of all – displays values of all
shell variables. environment variables.
• set var • setenv var
– assigns null to the variable – assigns null to var.
var. • setenv var value
• set var = value – assigns value to var.
– assigns value to var. where value may be
• a single word/quoted string.
where value may be
• a colon-separated list of words.
• a single word/a quoted string.
• a space-separated list of words enclosed
in parentheses.
Disk Usage
• df [-k] [file] (eg. df -k .)
– -k flag gives the following info:
• name of the file system.
• total space allocated to the file system in kilobytes.
• space allocated to existing files.
• space available for creation of new files.
• percentage of normally available space that is currently
allocated to the files on the file system.
• du [-s] [-k|h] [file …] (eg. du -sh .)
• gives the total space allocated to the file hierarchy rooted in the
specified file; and also the space allocated to each of its
subdirectory.
• -k|h gives the size in units of 1kB/MB rather than the default
512 byte units.
• -s reports only the total space for the specified file/directory.
Compression utilities
• tar
– ‘tar -cvf <archive_name> *’ Creates an archive of all files in the
current dir.
– ‘tar -tvf <archive_name>’ lists all files present in the archive.
– ‘tar -xvf <archive_name>’ extracts the files from the archive.
• compress <file_name>
– creates a compressed file <file_name.Z>.
• gzip -d <file_name>.Z | uncompress <file_name>.Z
– extracts file_name from the compressed archive.
wc
C Shell Scripting
Agenda
• Basics
• Arguments
• Expressions and Operators
• Conditional Statements
• Loops( foreach , while , switch)
• Input data from Terminal
Basics
#!/usr/bin/csh
echo “ I am a shell script”
# This completes my first shell script
#!/usr/bin/csh
if($#argv == 0 ) then
echo “ need atleast one argument”
exit 1
endif
cat $argv[1]
exit 1
Arguments( an example )
• Logical operators :
@ r = ( $num > 8 || $num < 4 ) ( $r is 1 if $num is 10)
@ r = ( $num > 8 && $num > 11) ( $r is 0 if $num is 10)
@ r = ! $num ( $r is 0 if $num is 10)
Expressions and Operators
• Comparison Operators
==
!=
>
>=
<=
!=
• String operators are == and !=
Expressions and Operators
#!/usr/bin/csh -f
if ( -e “.cshrc”) then
echo “ yeah my .cshrc exists “
end if
Conditional Statements
if(expression) then
commands
end if
if ( expression ) then
commands
else
commands
end if
Conditional Statements
#!/usr/bin/csh -f
#!/usr/bin/csh -f
#!/usr/bin/csh -f
while($#argv)
if ( -d $argv[1]) then
echo “ $argv[1] is a directory”
end if
shift
end
Loops
target … : prerequisites …
recipe
…
…
• Pattern rules
%.o : %.cpp
g++ -c –O3 -Wall $<
• Implicit rule
%.o: %cpp
$(CXX) -c $(CFLAGS) -o $@ $<
• Phony targets
– For example clean
– Target name is not a generated file but just a names for recipe
to be executed on an explicit request
compare:
diff test.log test.au
clean
$(RM) $(CLEAN_THESE)
More information