Sei sulla pagina 1di 31

Unix Intro

UNIX Intro and Scripting

Ramalingam Ganesan
1

Unix Intro
UNIX is an operating system which was first developed in the 1960s, and
has been under constant development ever since. By operating system,
we mean the suite of programs which make the computer work. It is a
stable, multi-user, multi-tasking system for servers, desktops and
laptops.
The most popular varieties of UNIX are Sun Solaris, GNU/Linux, and
MacOS X.

Unix Intro
Commands Unix makes little distinction between commands (user-level
programs) for system operation and maintenance (e.g. cron), commands
of general utility (e.g. grep), and more general-purpose applications such
as the text formatting and typesetting package.
DIRECTORY COMMANDS:
cd Change directory.
Usage: cd <filename>
Eg: cd my-directory
cd go to home directory
cd .. go up one directory

Unix Intro
pwd Print working directory on the terminal.
ls List the content of a directory.
Usage: ls [options] or ls [options] <directory-path>
Options: -l list all files in long format.
(permissions, users, filesize,date, and time are displayed).
-a list all files including those beginning with a .
-F list files distinguishing
directories/ executables* symbolic links@
-R recursively list subdirectories encountered.

Unix Intro
mkdir Create a new directory.
Usage: mkdir <directory-path>
rmdir Remove a directory if its empty.
Usage: rmdir <directory-path>
SYMBOLIC LINKS:
ln Create symbolic links between files or between directories.
Usage: ln [options] <file-to-be-linked> <new-file>
ln [options] <directory-to-be-linked> <my-directory>

Unix Intro
Options: -s allows linking across file systems and allows the display
of the links name upon ls -l.
Eg: ln -s course-file myfile
Eg: ln -s course-directory myspace
FILE COMMANDS
cp Copy files.
Usage: cp [options] <source-filename> <destination-filename>
cp [options] <source-filepath > <destination filepath>
Options: -r recursively copy directory structures.

Unix Intro
Options: -s allows linking across file systems and allows the display
of the links name upon ls -l.
Eg: ln -s course-file myfile
Eg: ln -s course-directory myspace
FILE COMMANDS
cp Copy files.
Usage: cp [options] <source-filename> <destination-filename>
cp [options] <source-filepath > <destination filepath>
Options: -r recursively copy directory structures.

Unix Intro
mv Move or Rename files or directories.
Usage: mv [options] <old-filepath> <new-filepath>
mv [options] <old-filename> <new-filename>
Options: -i query user for confirmation.
rm Remove files.
Usage: rm [options] <filname>
Options: -r recursively remove directory structures.
-i query user for confirmation.
cat View complete file content.

Unix Intro
cat <filename>
more View file contents in sections determined by the size of the terminal.
Usage: more <filename>
USEFUL CSH
| Pipe the output of a command to be processed by another command.
Usage: command1 |command2
Eg: ls -l | more
more file-name | grep pattern
more filename | wc

Unix Intro
> Redirect output........ to file (overwrite ).
Usage: command > filename
Eg: wc filename > new-file
>> Append (the result of the command) to the end of the file.
Usage: command >> file-name
Eg: pwd >> existing-file
< Take the input for the command from a file.
Usage: command1 < filename.

10

Unix Intro
& Run process in the backgound so that the shell remains active.
Usage: program-name &
program-name filename &
; Separate commands on the same line.
Usage: command1 ; command2
Eg: pwd ; ls
* Match a string of zero or more characters.
Eg: cp * copy all files

11

Unix Intro
chmod Set the permission on a file or a directory.
Usage: chmod [options] <who> <opcode> < permission> <filename>
Options: -R Recursively updates permisions within a directory
structure.
Who: u user
g group
o other
a all

12

Unix Intro
passwd Change the password.
df Displays the amount of free and used disk space.
du Displays the amount of disk usage.

13

Unix Intro
Programming Tools
ar - create library archives, and add or extract files
awk - pattern scanning and processing language
cb - a simple C program beautifier
cc - C compiler
cpp - the C language preprocessor
dbx - source-level debugger
indent - indent and format a C program source file
gprof - display call-graph profile data
ld - link editor
lex - lexical analysis program generator

14

Unix Intro

lint - a C program verifier


lorder - find an ordering relation for an object library
make - maintain, update, and regenerate related programs and files
mkstr - create an error message file by massaging C source files
nm - print symbol name list
prof - display profile data
ranlib - convert archives to random libraries
sccs - front end for the Source Code Control System SCCS. Includes a
number of related commands with their own man pages.
size - display the size of an object file
strip - remove symbols and relocation bits from an object file

15

Unix Intro

xstr - extract strings from C programs to implement shared strings


yacc - yet another compiler-compiler: parsing program generator .

16

Unix Intro
Printing
cancel - cancel requests to a printer
lp - send requests to a printer
lpq - display the queue of printer jobs
lpr - send a job to the printer
lprm - remove jobs from the printer queue
pr - prepare files for printing, perhaps in multiple columns

17

Unix Intro
Printing
cancel - cancel requests to a printer
lp - send requests to a printer
lpq - display the queue of printer jobs
lpr - send a job to the printer
lprm - remove jobs from the printer queue
pr - prepare files for printing, perhaps in multiple columns

18

Unix Intro
Information:
write - send messages to other users on the system in real time
who - identifies the users currently logged in
whoami - display your username.

19

Unix
Scripting
Scripting:
A shell script is a file that contains ASCII text. To create a shell script, you
use a text editor. A text editor is a program, like a word processor, that
reads and writes ASCII text files
Types:
Bourne shell (sh)
C shell (csh)
TC shell (tcsh)
Korn shell (ksh)
Bourne Again SHell (bash)

20

Unix
Scripting
Shell Scripting Introduction
Unix uses shells to accept commands given by the user, there are quite a
The various shells all have built in functions which allow for the creation
of shell scripts, that is, the stringing together of shell commands and
constructs to automate what can be automated in order to make life
easier for the user.
#!/path/to/shell

21

Unix
Scripting
We use < to specify that we want the command immediately before the
redirection symbol to get its input from the source specified immediately
after the symbol, for instance, we could redirect the input to grep(which
searches for strings within files) so that it comes from a file like this:
grep searchterm < file

22

Unix
Scripting
Variables
When a script starts all environment variables are turned into shell
variables. New variables can be instantiated like this:
name=value
You must do it exactly like that, with no spaces either side of the equals
sign, the name must only be made up of alphabetic characters, numeric
characters and underscores, it cannot begin with a numeric character.
You should avoid using keywords like for or anything like that, the
interpreter will let you use them but doing so can lead to obfuscated code
;)

23

Unix
Scripting
Variables are referenced like this: $name, here is an example:
#!/bin/sh
msg1=Hello
msg2=There!
echo $msg1 $msg2

24

Unix
Scripting
Command Line Arguments
Command line arguments are treated as special variables within the
script, the reason I am calling them variables is because they can be
changed with the shift command. The command line arguments are
enumerated in the following manner $0, $1, $2, $3, $4, $5, $6, $7, $8 and
$9. $0 is special in that it corresponds to the name of the script itself
!#/bin/sh
echo $((1 + 3 + 4))

25

Unix
Scripting
Control Constructs
The flow of control within SH scripts is done via four main constructs;
if...then...elif..else, do...while, for and case.
If..Then..Elif..Else
This construct takes the following generic form, The parts enclosed
within ([) and (]) are optional:
if true
then doActiva
[elif true+1
then doactivi2] ...
[else list]
fi

26

Unix
Scripting
When a Unix command exits it exits with what is known as an exit status,
this indicates to anyone who wants to know the degree of success the
command had in performing whatever task it was supposed to do,
usually when a command executes without error it terminates with an exit
status of zero. An exit status of some other value would indicate that
some error had occurred, the details of which would be specific to the
command. The commands' manual pages detail the exit status messages
that they produce.

27

Unix
Scripting
#!/bin/sh
if [ "$1" = "1" ]
then
echo "The first choice is nice"
elif [ "$1" = "2" ]
then
echo "The second choice is just as nice"
elif [ "$1" = "3" ]
then
echo "The third choice is excellent"
else

28

Unix
Scripting
echo "I see you were wise enough not to choose"
echo "You win"
fi

29

Unix
Scripting

Every script should being with #!/


Every new line is a new command
Comment lines start with a #
Commands are surrounded by ()

First script:

#!/bin/bash
# My first script
echo "Hello World!"

30

Unix
Scripting
Setting permissions
The next thing we have to do is give the shell permission to execute
your script. This is done with the chmod command as follows:
[me@linux me]$ chmod 755 my_script
The "755" will give you read, write, and execute permission.
Everybody else will get only read and execute permission. If you want
your script to be private (i.e., only you can read and execute), use
"700" instead.

31

Potrebbero piacerti anche