Transcript
Page 1: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

OPERATING SYSTEMSOPERATING SYSTEMSLINUXLINUX

Božo Krstajić, PhD, University of Montenegro Podgorica

[email protected]

Page 2: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Finding FilesSometime you have needs to find some files. You could go looking through directories, but there are

quicker ways. There are four main file search commands available in

Slackware.

whichThe which command is usually used to locate a

program quickly. It just searches your PATH and returns the first instance it finds and the directory path to it.

which name

Page 3: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Finding FilesFor example:which bash

/bin/bash

From that you see that bash is in the /bin directory. This is a very limited command for searching, since it only searches your PATH.

Page 4: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Finding FileswhereisThe whereis command works similar to which, but

can also search for man pages and source files.

For example:

$ whereis bashbash: /bin/bash /usr/bin/bash

/usr/man/man1/bash.1.gz

This command not only told us where the actual program, but also where the online documentation is stored. Still, this command is limited.

Page 5: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Finding FilesWhat if you wanted to search for a specific

configuration file? You can't use which or whereis for that.

find

The find command will search for anything. We want to search the entire system for the default

sample file on the system.

find / -name sample(find “where” “what”)

If you run this command as a normal user, you will probably get permission denied error messages for directories that only root can see.

Page 6: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Finding Fileslocate

The locate command searches the entire filesystemjust like the find command can do, but it searches a database instead of the actual filesystem.

The database is set to automatically update at 4:40AM, so you have a somewhat fresh listing of files on your system. You can manually run updatedb(1) to update the locate database (before running updatedb by hand, you must first su to the nobody user). Here's an example of locate in action:

$ locate xinitrc # we don't have to go to the root

Page 7: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Example 1:• Locate file inetd.config• In your HOME directory find files which name

start with letter n• List of all files from / directory (and

subdirectories) which finished with .conf put into file conf_file in your HOME.

• Where is file passwd in our filesystem?

Page 8: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Example 2:Try to find how grep command works.

• In your filesystem find files which consist word “set” .

• In your HOME find files which consist phrase “name”

Page 9: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Searching …

grep

The grep command searches the named input FILEs for lines containing a match to the given PATTERN.

grep PATTERN FILEs

Page 10: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Example 3:• Find number the ‘o’ letters containing in your

HOME directory files.

Page 11: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

grep –cr ‘o’ ~/*

Page 12: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Compress and Archive FilesUnder Slackware Linux, there are several programs

that can be used to compress and archive files.

These programs are especially useful for making backups and sending copies of files between machines over a network connection.

There are programs for dealing with Unix formatted archives, as well as Windows archives.

Page 13: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Compress Filesgzip

The gzip is the GNU compression program. It takes a single file and compresses it. The basic

usage is as follows:gzip infile

The resulting file will be named infile.gz and will usually be smaller than the input file.

Note that infile.gz will replace infile. This means that infile will no longer exist, even though a gzippedcopy will.

Page 14: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Compress FilesRegular text files will compress nicely, while jpeg

images, mp3s, and other such files will not compress too well as they are already compressed.

The maximum compression can be achieved like so:

gzip -9 infile

This will take a longer time to compress the file, but the result will be as small as gzip can make it.

Page 15: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Compress FilesDecompressing gzipped files can be done using two

commands, which are really just the same program.

The gzip will decompress any file with a recognized file extension.

A recognized extension can be any of the following: .gz, -gz, .z, -z, .Z, or -Z.

The first method is to call gunzip on a file, like so:

gunzip infile.gz

This will leave a decompressed version of infile in the current directory

Page 16: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Compress FilesThe other way to decompress a gzipped file is to call

gzip on the file:

gzip -d infile.gz

This will cause exactly the same behavior as calling gunzip.

The reason for this is simple: gunzip is simply a symbolic link to /bin/gzip.

CAN YOU FIND THAT LINK ?

Page 17: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Example 1:• In your HOME directory create file skadar.• Compress the new file using gzip command.• Check the file size before and after this process.• What is the name of compressed file?• Show content of the compressed file.• Change name of the compressed file to name

without suffix gz?• Can you restore this file without changing the

name?• Restore the file (skadar).

Page 18: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Compress Filesbzip2

bzip2 is an alternative compression program installed on Slackware Linux. It uses a different compression algorithm from gzip, which results in some advantages and some disadvantages.

The main advantage for bzip2 is the compressed file size. bzip2 will almost always compress better than gzip. In some instances, this can result in dramatically smaller files.

The disadvantage to bzip2 is that it is more CPU intensive than gzip. This means that bzipping a file will generally take more time than gzip.

Page 19: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Compress FilesThe usage of bzip2 is very similar to gzip:

bzip2 infile

The resulting output file will usually be smaller than the input file, and will be called infile.bz2.

As with gzip, the input file will no longer exist, since bzip2 replaces the input file with a compressed copy.

Page 20: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Compress FilesThere are two commands to decompress files ending

in a .bz2 extension, just as with gzip.You can use bzip2 or bunzip2 to decompress

bzipped files:bzip2 -d infile.bz2

orbunzip2 infile.bz2

This will decompress the bzipped file and replace it with the decompressed copy.

Page 21: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Example 2:

• In your HOME directory create file skadar.• Explore some switches of command bzip2• Compress the new file using bzip2 command

such that origin file exists after compresson.• Check the file size before and after this process.• What is the name of compressed file?• Show content of the compressed file.• Remove compressed file.

Page 22: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Compress Fileszip

Finally, there are two utilities that can be used on zip files. These are very common in the Windows

world, so Linux has programs to deal with them. The compression program is called zip, and thedecompression program is called unzip .Compressing files is easy:

zip zip_filename *This will create the file zip_filename.zip, which will

contain all the files in the current directory.

Page 23: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Compress FilesYou can also recurse (-r) through the current

directory, zipping up any directories that are also laying around:

zip -r zip_filename ./*

Decompressing files is easy, as well.unzip zip_filename

This will extract all the files in the file zip_filename.zip, including any directories in the archive.

Page 24: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Example 3:

• In your HOME directory create subdirectory arh.• In arh copy all files from /etc directory which

name start with letter s• Compress all copied files and arh directory into

file archive.zip• Can you compress file archive.zip with bzip2

command?• Restore compressed files and directory.

Page 25: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Archive Filestar

tar is the GNU archiver. It takes several files or directories and creates one large file. This allows you to compress an entire directory tree, which is impossible by just using gzip or bzip2.

The tar has many command line options, which are explained in its man page.

The most common use for tar is to decompress and unarchive a package that you've downloaded from a web site or ftp site. Most files will come with a .tar.gz extension. This is commonly known as a “tarball”. It means that several files were archived using tar and then compressed using gzip.

Page 26: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Archive FilesTo create a compressed tar archive of all the files in

the current directory (including any subdirectories and their files), you would use tar like so:

tar -cvzf archive.tar.gz ./*

In this command line, the -c option tells tar to create an archive, while the -z option runs the resulting archive file through gzip to compress it, -v means to be verbose (this will list all the files that are being archived) and finally, the -f option tells tar that the next string on the command line is the file to operate on.

Page 27: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Archive FilesThe most common way to decompress a tarball is

like so:

tar -xvzf archive.tar.gz

The -x means to extract. This is important, as it tells tar exactly what to do with the input file.

Page 28: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Example 4:

• In your HOME directory create subdirectory arh.• In arh copy all files from /dev directory which

name star with letter h• Archive and compress all copied files and arh

directory into file archive.tar.gz• Check the archived file size.• Restore compressed files and directory.

Page 29: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

vi editorvi is the standard Unix text editing program, and

mastering it is essential for system administrators and users.

There are several versions (or clones) of vi available (vi, elvis, vile, and vim). One of these is available on just about any version of Unix, as well as on Linux.

On Slackware Linux, the default version of vi available is elvis.

All of these versions include the same basic feature set and commands.

Page 30: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Starting vivi can be started from the command line in a variety

of ways. The simplest form is just:

vi

At this point, you'll see a mostly blank screen. It is now in “command mode”, waiting for you to do something.

In order to quit out of vi, type the following::q

Page 31: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Starting viYou can also start vi for creating a new file

with a name. For example, the name of file will be new:

$ vi new

You can also start vi with a pre-existing file. For example, the file ~/sample would be opened like so:

$ vi ~/sample

Page 32: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Starting viFinally, vi can be started on a particular line

of a file. For example, you could start up vi on line 47 of /usr/src/linux/init/main.clike so:

$ vi +47 /usr/src/linux/init/main.c

Page 33: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Modesvi operates in various modes, which are

used to accomplish various tasks.

When you first start vi, you are placed into command mode. (manipulate text, move around in the file, save, quit, and more)

Editing the text is done in insert mode.

You can quickly move between modes with a variety of keystrokes.

Page 34: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Command modeYou are first placed into command mode.

From this mode, you cannot directly enter text or edit what is already there.

However, you can manipulate the text, search, quit, save, load new files, and more.

Probably the most often used command in command mode is changing to insert mode.

This is accomplished by hitting the i key.

The cursor changes shapes, and -- INSERT -- is displayed at the bottom of the screen.

Page 35: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Switch modesFrom there, all your keystrokes are entered

into the current buffer and are displayed to the screen.

To get back into command mode, hitthe escape key (ESC).

icommand mode insert mode

ESC

Page 36: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Command modeOn some systems, you can use the arrow keys to move

around.

On other systems, you may need to use the more traditional keys of “hjkl”.

Here is a simple listing of how these keys are used to move around:

• h move left one character• j move down one character• k move up one character• l move right one character

Page 37: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Command modeMany of the commands that you will use in command

mode begin with a colon (:).

For example, quitting is :q. The colon simply indicates that it is a command, while the “q” tells vi to quit.

Other commands are an optional number, followed by a letter. These commands do not have a colon before them, and are generally used to manipulate the text.

For example, deleting one line from a file is accomplished by hitting dd, or deleting one letter from file accomplished by hitting x.

Page 38: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Command modeCommand mode can also be used to cut and paste,

insert text, and read other files into the current buffer.

Copying text is accomplished with the y key (y stands for yank).

Copying the current line is done by typing yy, and this can be prefixed with a number to yank more lines (5yy).

Then, move to the location for the copy and hit p. The text is pasted on the line after the current one.

Cutting text is done by typing dd.

Page 39: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Command modeReading in text from another file is a simple

procedure. Just type :r, followed by a space and the file name that contains the text to be inserted.

:r sample

Page 40: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Saving FilesThere are several ways to save files in vi. If you want to save the current buffer to the file

new_f, you would type::w new_f

Saving it again is as simple as typing :w.

If you want to save the file and quit vi (a very common operation), you would type

:wq

You can also quit without saving with :q or :q!.

Page 41: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Opening FilesThe vi allows you to open files from command mode as

well as specifying a file on the command line to open.

To open the file /etc/lilo.conf:

:e /etc/lilo.conf

If you have made changes to the current buffer without saving, vi will complain.

You can still open the file without saving the current buffer by typing :e!, followed by a space and the filename.

Page 42: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Insert ModeInserting and replacing text is accomplished in insert

mode. As you see, you can get into insert mode by hitting i from

command mode. Then, all text that you type is entered into the current

buffer.

Replacing text is accomplished in several ways. From command mode, hitting r will allow you to replace the one character underneath the cursor.

Just type the new character and it will replace the one under the cursor. You will then be immediately placed back into command mode.

Page 43: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Replace ModeHitting R allows you to replace as many characters

as you'd like.

To get out of this replacement mode, just hit escapeto go back into command mode.

Rcommand mode replace mode

ESC

Page 44: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

vi keysMovement

Operation Key

• left, down, up, right h, j, k, l• To the end of the line $• To the beginning of the line ^• To the end of the file G• To the beginning of the file :1• To line 47 :47

Page 45: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

vi keysEditing

Operation Key

• Removing a line dd• Removing five lines 5dd• Replacing a character r• Removing a character x• Removing ten characters 10x• Undo last action u• Join current and next lines J

Page 46: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

vi keysSearching

Operation Key

• Search for “asdf” /asdf• Search backwards for “asdf” ?asdf• Repeat last search forwards /• Repeat last search backwards ?

Page 47: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

vi keysSaving and Quitting

Operation Key

• Quit :q• Quit without saving :q!• Write and quit :wq• Write, without quitting :w• Reload currently open file :e!• Write buffer to file asdf :w asdf• Open file hejaz :e hejaz• Read file asdf into buffer :r asdf

Page 48: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Example 1• Create file your_full_name in ~• Write your personal info in the file• Copy writen text 2 times in same file• Insert content from /etc/inetd.conf in the end of

this file• Save and exit from the file• Show the file content on display• Enter to the file• Delete part inserted from /etc/inetd.conf without

first two lines• Save and exit from the file

Page 49: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Shell Script or Shell “Program “

Shell script is a file that contain one or more shell or external commands.

Can be used to:- simplify some tasks,- replace more commands as a single,- automate the program installation,- write simple interactive application.

Page 50: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Programming vs. Scripting

Programming Langs.C++/C , Java, …

Scripting Langs.Bash, Perl, Tcl/tk, …

CPU instructions vs. executable filesScripting Langs are normally interpretive

SlowerEasier to use and debugSuitable for text processing, repetitive jobs, …

Page 51: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

BASH shall

Bourne again shell (BASH)

The most widely used shellIn Linux environment you are interacting with BASH on a daily basisOperating system maintenance scripts are generally bash scripts

Page 52: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Shell ScriptHow create a shell script:You must create a file and put the shell or external

command you want to be executed into that file.

The file must has execute permission !

EXAMPLE 1:

Create simple shell script named cls, which will clear screen.

Page 53: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Exampe 2: “Hello world” script

The traditional example:#!/bin/bash echo Hello World

To execute: chmod u+x hello./hello

What if we omit the first line ?

Page 54: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Exampe 3

Create shell scrip run, which will:show current directory,enter in your HOME directory,create tmp subdirectory in HOME,copy all files from HOME to tmp,compress and archive tmp directory,mount usb memorymove archived file to usbumount usbremove tmp subdirectory

Page 55: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Exampe 4

Create shell scrip minimum, which contains at least 7 different shell or external commands as you wish.

Page 56: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

VariablesAs is all program language, the use of variables is

very important. So, in BASH you can variables.

You assign a value to a variable simply:

variable=5

Notice that you do not have to declare a variable.This means that you store a character string into a

variable in the same way that you stored the integer into a variable.

variable=5variable=‘This is a string’

Page 57: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Variables

Environment variablesThere are no data typesNo need to declare variables

Example:

#!/bin/bash STR="Hello World!" echo $STR

Note that $ sign is used to dereference variablesWhat if we omit it?

Page 58: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Functions

BASH enables you to define your own functions.

The main advantages is for organizational purposes.

The syntax for creating a function is:

function f_name {shell commands}

You can invoke the function by entering the following command:

f_name

Page 59: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Local VariablesExample for function and local variable:

#!/bin/bashHELLO=Hellofunction hello {

local HELLO=World echo $HELLO } echo $HELLO hello echo $HELLO

Similar to internal function variables in C

Page 60: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Conditional SentencesBASH supports nested if/then/else statements.They use to perform conditional test in program.

Syntax:

if [expressions]; thancommands

elif [expressions]; thancommands

elsecommands

fi

Page 61: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Integer Operators

int1 –eq int2 equal (=)int1 –ge int2 greater or equal (>=)int1 –gt int2 greater (>)int1 –le int2 less or equal (<=)int1 –lt int2 less (<)int1 –ne int2 not equal (≠)

Page 62: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

String Operators

str1 = str2 identical stringsstr1 != str2 not identicalstr is not null (return true)-n str length is greater then zero-z str length is equal to zero

Page 63: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Logical Operators

! expr expr is not trueexp1 -a exp2exp1 and exp2 are trueexp1 -o exp2 exp1 or exp2 are true

Page 64: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

File Operators

-d fname fname is a directory-f fname fname is an ordinary file-r fname fname can be read by the process -w fname fname can be written by the process-s fnamefname has a nonzero length-x fnamefname is executable

Page 65: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Example for string

#!/bin/bashT1="foo" T2="bar" if [ "$T1" = "$T2" ]; then

echo expression evaluated as true else

echo expression evaluated as falsefi

Page 66: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Example for integer

#!/bin/bashN1=5N2=10 if [ $N1 -eq $N2 ]; then

echo numbers are equal else

echo numbers aren’t equalfi

Page 67: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Iteration statement: Loops (for)Execute commands within it a specified number of times.

Syntax:for VARIABLE [in list]; do

commandsdone

Example:

#!/bin/bashfor i in 1 2 3 4 5; do

echo $i done

Page 68: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Example

#!/bin/bash for i in ~ /home /etc; do

echo $ils –l $i|more

done

What is doing this script ?

Page 69: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Input arguments$0 – Stores the firs word of the enter command$1 – Stores the firs argument of the enter command$? – Stores the exit value of the last command that was executed.

if [ -z "$1" ]; then

echo usage: $0 directory_name !!!!!exit

fils –la $1

Explain this script file

Page 70: OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, PhD, ... installed on Slackware Linux. ... Compressing files is easy:

Reading InputExample:

echo Please, enter your firstname and lastnameread FN LN echo Hi! $LN, $FN !


Recommended