bash scripting for beginner and intermediate

153
Shell Scripting Shell scripting for beginners and intermediate levels BASH SCRIPTING

Upload: ahmed-gamil

Post on 15-Apr-2017

789 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Bash scripting for beginner and intermediate

Shell Scripting

Shell scripting for beginners and intermediate levels

BASH SCRIPTING

Page 2: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 1 ال رغب

بسم اهلل الرحمن الرحيم

احلند هلل الذي اعانين علي االنتواء من هذا الكتاب

Hamish Whittalللنؤلف Shell Scriptingهذا الكتاب هو تزمجة لكتاب ال

للنبتدئني واملستوي املتوسطباسلوب سول وبسيط Shell Scriptingوهو كتاب يشزح ال

حيت تصل املعلومى بسوولة ويسزضافة بعض الصور التوضيحية إولقد قنت برتمجتى و

بغي بى اال وجى اهلل الكزيهأا الكتاب االستفادة املزجوة وال متين ان حيقق هذا

ال تنسونا من صاحل دعائكه

Page 3: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 2 ال رغب

TABLE OF CONTENTS

What is Shell Scripting ...................................................................................7

Introduction to info, man and the whatis database .............................................9

info pages..................................................................................................9

Man Pages ............................................................................................... 12

The whatis Database ................................................................................. 13

Revising some Basic Commands .................................................................... 14

The who command.................................................................................... 14

The w Command ...................................................................................... 16

The "date" command................................................................................. 18

The 'echo' command ................................................................................. 19

Running a Shell Script .................................................................................. 20

File Commands......................................................................................... 20

Wildcards.............................................................................................. 20

Returning to file commands........................................................................ 23

ls ......................................................................................................... 23

cp ........................................................................................................ 24

mv....................................................................................................... 24

wc ....................................................................................................... 24

nl......................................................................................................... 24

System Commands ................................................................................... 24

The df and du commands ........................................................................ 25

the fdisk command................................................................................. 26

The free command ................................................................................. 26

the vmstat command ............................................................................. 27

the iostat command ............................................................................... 27

stdin, stdout, stderr ..................................................................................... 28

stdin ....................................................................................................... 28

stdout ..................................................................................................... 29

Using stdin and stdout simultaneously ......................................................... 30

Appending to a file .................................................................................... 31

stderr...................................................................................................... 32

Page 4: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 3 ال رغب

stdout, stderr and using the ampersand )&( .................................................. 33

Unamed Pipes .......................................................................................... 34

Introduction................................................................................................ 36

What is the login shell? ................................................................................. 37

The job of the shell ...................................................................................... 42

Command Interpreter................................................................................ 42

Allows for variables to be set ...................................................................... 43

I/O redirection ......................................................................................... 43

Pipelines .................................................................................................. 43

Customising your environment ................................................................... 43

Introduction................................................................................................ 44

What are regular expressions?....................................................................... 44

The fullstop (period) ”.” ............................................................................. 44

Let's explore "sed" syntax .......................................................................... 46

Square brackets ( [ ] ), the caret ( ^ ) and the dollar ( $ ) ............................. 48

Using sed and pipes .................................................................................. 50

The splat (asterisk) ) * ( ............................................................................ 52

The plus operator ) + ( ............................................................................... 53

Matching a specified number of the pattern using the curly brackets }{ .......... 54

A detour - Using a different field separator in sed pattern matching .............. 56

Using Word Encapsulating Characters ....................................................... 57

Returning from detour to our discussion on curly braces … ............................. 57

The tr command .......................................................................................... 57

The cut command ........................................................................................ 59

The paste command..................................................................................... 60

The uniq command ...................................................................................... 61

The Sort command ...................................................................................... 62

The grep command ...................................................................................... 64

grep, egrep and fgrep ............................................................................... 67

Section Techniques to use when writing, saving and executing Shell Scripts ........ 68

Detour: File Extension labels ................................................................... 68

Comments in scripts ............................................................................... 69

Page 5: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 4 ال رغب

Variables .............................................................................................. 69

Shebang or hashpling !# ......................................................................... 70

Exit ...................................................................................................... 70

Null and unset variables.......................................................................... 71

Variable Expansion ................................................................................. 73

Environmental vs shell variables .............................................................. 74

Arithmetic in the shell............................................................................. 75

Introduction................................................................................................ 77

Single Quotes or "ticks" ................................................................................ 77

Double Quotes ............................................................................................ 78

Backticks.................................................................................................... 80

Shell Arithmetic's with expr and back quotes ................................................... 81

Another tip when using quotation marks ......................................................... 82

Introduction................................................................................................ 84

Positional Parameters 0 and 1 through 9......................................................... 86

Other arguments used with positional parameters ............................................ 87

$# How many positional arguments have we got ?........................................ 87

$* display all positional parameters ............................................................ 87

Using the "shift" command - for more than 9 positional parameters ................. 88

Exit status of the previous command ........................................................... 88

Making Decisions ......................................................................................... 90

Testing for a true or false condition ............................................................. 90

The test command ................................................................................. 90

What is "true" and "false"........................................................................ 91

Different types of tests .............................................................................. 92

Testing a string ..................................................................................... 92

Has a variable been set or not ? ............................................................... 93

Numeric Tests ....................................................................................... 95

File test ................................................................................................ 96

Logical Operators ........................................................................................ 98

Conditions in the shell ................................................................................ 100

Using the "if" statement........................................................................... 100

Page 6: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 5 ال رغب

The "if" "then" "else" statement ................................................................ 101

The "elif" statement ................................................................................ 102

The "case" statement .............................................................................. 102

Debugging your scripts............................................................................... 103

The NULL command ............................................................................. 105

The || and && commands ..................................................................... 106

Introduction.............................................................................................. 109

The "for" loop......................................................................................... 109

while and until loops ............................................................................... 116

The break and continue commands ........................................................ 120

getopts Using arguments and parameters ..................................................... 123

Introduction.............................................................................................. 124

The read command .................................................................................... 124

Presenting the output................................................................................. 127

The echo command ................................................................................. 127

The printf command................................................................................... 128

The shell environmental variables pertaining to scripting................................. 132

The Source command................................................................................. 134

the exec command .................................................................................... 136

Other methods of executing a script or a series of commands ....................... 136

Execution with Round brackets .............................................................. 136

Execution with Curly brackets ................................................................ 137

Introduction.............................................................................................. 139

PARAM:-value ........................................................................................... 139

PARAM:=value .......................................................................................... 141

${param:+value}...................................................................................... 141

${variable%pattern} ................................................................................. 142

MAGIC%%r*a ........................................................................................... 143

variable#pattern ....................................................................................... 143

variable:OFFSET:LENGTH ........................................................................... 145

#variable.................................................................................................. 145

Re-assigning parameters with set ................................................................ 146

Page 7: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 6 ال رغب

Explaining the default field separator field - IFS ............................................. 147

Setting variables as "readonly" .................................................................... 147

The eval command .................................................................................... 149

Running commands in the background using & .............................................. 152

Page 8: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 7 ال رغب

CHAPTER 1. INTRODUCTION TO

THE COURSE STRUCTURE

WHAT IS SHELL SCRIPTING

غ ، زا و اض عذ غت ا ر system administratorارا اسدد ا ر

اعشع اشس ، الي عز ؽئز رلش ثطشوخ اثغؾ اكؼ

غزطغ scriptبدا بى manualكال رلؼ اي شء ثطشوخ scriptingارا اسدد ا رزؼ

ػ ري

???? shellب ا غت ا ؼ scripting زؼ

commands، ؽش و ثبعزوجب ا DOSشج ثب commands زشع shellا

زشعب command lineػ ا userاز رزجب ا

terminalكبرا هب ثزبثخ زا االش ك ا

# ls –l

، ق ؼشف ا ابرظ عظش ”l-“زا االش ، ق ؼشف ؼ ثزلز linuxق و ا

ؼشف ثبظذكخ ???ػ اشبشخ ،

shellال ، ال ؼشف ثبظذكخ ، ث ؼشف ػ ؽشن ا

shellاز رزت غ ثؼؼب اجؼغ ثذاخ ا commands غػخ ا shell scriptا

DOS batchوا ثظلخ ؼ، ض ض ا ز ػؼ ك ق اؽذ

ارا اسدب رلز ز االش :

# free

االش

# df -h

كبطشوخ اال ا و ثزلز اش ػ ؽذح

Page 9: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 8 ال رغب

لظ اؼالخ ”;“ا ا و ثزلز االش غ ثؼغ كت لظ اغطش ري ثػغ ػالخ

ػذب شذ ا شع سعبخ الضش شؾض E-mail addressاز رػغ ث ا

ركش هذ ؽذ ارا كز اطشوخ ركش او اغذ اهذ ، بصاذ بى ؽشوخ اكؼ

اضش ، اال ػغ ز االش ك ق ، ػذب شذ رلزب و كوؾ ثزبثخ اع اق

scriptزا اق غ كزظش ازغخ ػ اشبشخ ،

Page 10: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 9 ال رغب

INTRODUCTION TO INFO, MAN AND THE WHATIS DATABASE

، ا scriptsهج ازؼب غ ا info pagesا man pages الذ ازؼشف ػ ا

man pages اخزظبس manual pages رؾزي ػ ؼبد ضشح ػ ا

commands ك ا هذ ال غذ ب ؾزبعman pages زي هذ ؾزبط اinfo pages

INFO PAGES

ضشا اال اب رؾزي ػ ؼبد اضش، ؽشوخ man pagesرشجخ ا info pagesا

ثغطخ ، كبرا اسدب ا غزؼشع ؼبد ػ اش ؼ كو ثزبثخ info pagesاعزخذا ا

االش ثزا اش :

# info ls

ضال text filesاز رزؼب غ ا commandsا عزؼشاع ؼبد ػ غػخ ا

ػ ؽشن

# info coreutils

Page 11: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 10 ال رغب

از commands غػخ االداد ا ا core utilities اخزظبس coreutilsا

…… ,ls, mv, cpض text filesرغزخذ ك ازؼب غ ا

از رؾزي ػ : info page coreutilsا ب شي ك زا اش

File: coreutils.info اع ا file ازي ؾزي ػ ز اInfo

Node: Top خnode رؼtopic top رؼ ا زا اtopic ا topic ك

info pageز ا

Page 12: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 11 ال رغب

Next: introduction رؼ ا اtopic ب ازبخ اعintroduction زبة ا ا

topic ازبخ غزخذ اؾشف“n”

Previous: disk usage رؼ ا اtopic اغبثوخ اعب disk usage

”p“االزوب اب ػ ؽشن اؾشف

Up : (dir) رؼ اinfo page اغبثوخ االزوب اب ػ ؽشن اؾشف“u”

بى ػا غ الؽع ا menu هبئخ ا topics ا اعدح ك ز

info page ا زو ث از

ث ازبة جبششح ا ايtopic ك اظلؾخ ػ ؽشن اض ا اع اtopic

summarize، كبرا اسدب ضال ازبة enterص اؼـؾ ػ menuاعد رؾذ ػا

files topic كو ثبض ثبغ ا ص ؼـؾenter

ػذب شذ اؼدح ا طلؾخ اcoreutils اشئغخUp : dir ؼـؾ“u”

ارا اسدب ازبة ا ثذاخ اظلؾخnode: top ؼـؾ“t”

جؾش ػ خ ؼ ك اinfo page زجؼب اخ اشاد اجؾش ”/“و ثزبثخ

بػ

Page 13: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 12 ال رغب

/Directory

ارا اسدب ا جؾش ػ لظ اخ شح اخشي غزخذ

/<ENTER>

خشط اinfo page زت“q”

الزوب طلؾخ الػ غزخذ“backspace”

ػ ا زت helpخشط ا ”?“زت info pageاخبص ثب helpؾظ

“CTRL+x+0”

MAN PAGES

ػ اغبػذح ، اطشوخ Info pagesرؾذصب ػ ا عخ اػبكخ ؾظ

ػ اؼبد ك اظ ا man pagesاالعبعخ ؾظ

moreازي شج ا less commandرؼ خال ا man pageاؾووخ ا ا

command ؾزي ػ options اضش

ش :غزخذ اال man pageزشـ ا

man <command>

رغبي

man ls | less

ارا اسدب ا جؾش ػ خ ك اman page زجؼب اخ اشاد ”/“و ثزبثخ

اي forward searchب ب رو ثؼ info pageاجؾش ػب ب ؾذس ك ا

ا اخشب pageا اجؾش ػ اخ ا ا

ب ػ ارا اسدreverse search ص زجؼب ”/“ثذال ”?“كو ثبعزخذا اؼالخ

اخ اشاد اجؾش ػب اعل اق ا اػال

الزوب طلؾخ ز رب غزخذ اspace bar

الزوب طلؾخ ز رغجوب غزخذ اؾشف“b”

خشط اman page زت“q”

ػ غبػذح ك لخ اعزخذا ا ؾظman pages ازو ثذاخب ؼـؾ“h”

manظ lessاخبص ثبالش help، الؽع اظسح ازبخ ا اؼشع ا

page ال اman page غشد طلؾخ ػبدخ ز ػشػب ثاعطخ ا less

command

Page 14: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 13 ال رغب

: sections ا غػخ ا Man pageروغ ا

1- Name section اع ا :command

2- SYNOPSIS section ا اخض ا االخزظبس رؼ اظسح اخزظشح اجغطخ :

العزخذا االش

3- DESCRIPTION sectionاغضء ازي ششػ ظلخ االش :

4- SEE ALSO section ػ ضذ ا زلبط: زا اغضء ػؼ ثؼغ اطشم ؾظ

THE WHATIS DATABASE

Whatis command غؼ ب ثؼشكخ ظلخ اcommand ثش عشغ خزظش ؽش

nameػ اغضء اخبص ثب systemك ا manual pagesو ثبجؾش ك ا

section اعدح ك ز اman page ص رو ثزشرج رؼؼ كdatabase خبطخ

at nightب rebuildا update ز ػ

whatis nl

Page 15: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 14 ال رغب

ذب غزخذ االش : update databaseارا اسدب ا و ثؼ

makewhatis -u -w

REVISING SOME BASIC COMMANDS

ثؼغ ا زا اغضء ػ basic commandsعف زؾذس ك ا ؼشكخ ب ز commandsاـشع ب

ذق غزخ

THE WHO COMMAND

، ارا اعزخذب systemػ ا logged userك ؼشكخ ا commandغزخذ زا ا

كغظش ب ازب : argumentsا optionsثذ اي

commandاز ا رغزخذ غ زا ا optionsؼشع ا help ا غزخذ ا

who --help

Page 16: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 15 ال رغب

columnغ اظبس ػا logged usersؼشع ا

who -H

logged usersػ ا short listؼشع

who -s

Process ID‟sا logged usersؼشع ا

Page 17: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 16 ال رغب

who -u

، عف غذ ا headingا ا columnsكغف ظش اع ا ”u-“غ ”H-“اعزخذب ارا

userؼ ا ا idle time user، ض ا IDLEاشاثغ غ columnا

لزشح ص ؼ systemغزخذ ا

logoutؾذس .min 15ذح systemثبعزخذا ا userارا و ا

who -uH

THE W COMMAND

? w commandب ا

ر :كغغذ اال whatisثبعزخذا االش w commandارا هب ثبالعزؼال ػ ا

برا كؼا systemزا ؼ ا زا االش و ثؼشع االشخبص از دخا ػ ا

Page 18: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 17 ال رغب

غب اضب اغطش اال اظسح ا headingب شي ك headerا

User ا logged userاع

tty ا ttyاع ػب ا userا

login@ ا Userرهذ دخ

idle اهذ ازي terminalش ز اخش اعزخذا

JCPU عغ ا ازي هؼز processesاض child processesا ا اعزخذا terminalك

PCPU ا ازي هؼز active processاض ا اعزخذا خ ك terminalاؾب

What ا ا processاع ازي ؼ commandا

هبئخ ثب optionsز commandاغزخذخ غ زا ا

-h headerرغ ظس ا

-u Ignores the username while figuring out the current process and cpu times. To

demonstrate this, do a "su" and do a "w" and a "w -u".

-s ثؼشع short formatو ثؼشع ا PCPU ا JCPUا login timeال و

-f غ ظسfrom ازغخ

-V ا versionؼشع ؼبد ػ

user ؼ userؼشع ؼبد ػ

Page 19: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 18 ال رغب

ا رخض ق logged userز etc/utmp/ك

ؾبالد ا ؼبد ػ رخض ق loginز etc/security/failedlogin/ البشخ ك

THE "DATE" COMMAND

ك scriptاجغطخ اب عو ثؼ ا commandsب غزشعغ ز ا اغجت ك ا

commandsثز ا courseزا ا

أ ا زى ثذ الب دائب ؾزبط ا ؼشف scriptingك ا date commandا عؾزبط ا

script ك اؼ ز از ، ب و اdate command خ ثؼ ثؼغ االشبء اغ

، ب ازؾ ث human timeا ا unix timeاز ؾزبعب ض ازؾ ا

طؾ ػشع ازبسخ اخزلخ

# info date

اال كغغذ اش ازب : form ظشب ا ا

date [+ ]

وىو يساوي

date + “ “

” “اطثخ ث ا form ز ػغ ا

Page 20: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 19 ال رغب

THE 'ECHO' COMMAND

script اش ؿبخ ك االخ ال ؿ ػ ك اي echoاخش اش عزؾذس ػ اال االش

سإخ terminalاز رؾذصب ػب عبثوب رلزب ثغخ ػ ا commandsا ا

ارا كب ؽشوخ شخ ، ك رظش ازغخ ػ اشب scriptزغزب اؼب، ارا ػؼبب ك

اظبسب ػ اشبشخ ???

ازي و ثطجبػخ ابد ػ اشبشخ echoاطشوخ اعزخذا االش

echo ػ ، اب built in the shell االخشexternal عؼشف الؽوب ز

زبسخ بزب :غزخذ ع ا ا و ثزي كغف غزخذ ك ػشع ا

عزظش ازغخ بزب

Page 21: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 20 ال رغب

RUNNING A SHELL SCRIPT

ري ػ ؽشن اعزخذا execution permissionsغت ا scriptزشـ ا

االش :

Chmod u+x script.sh

Chmod g+x script.sh

Chmod o+x script.sh

Chmod x script.sh

com executable.ا exe.ص ازي ؼزجش ا اي ق ازذاد زا ثبطجغ خزق ػ اذ

file ثب اظ ظ ثextensions اعزخذاب“.sh” ظ ـextension اب

اي اب ػخ رظخ كوؾ ، اصبب كغز رلز file scriptرض كوؾ ػ ا زا ا

اؼب scriptا

ض : scriptح ؽشم زشـ ا بى ػذ

./script.sh

OR

sh script.sh

OR

”/.“وتنفيذه بدون استخدام bin/يمكه وضعو في ال

FILE COMMANDS

ازي غزخذ ”ls“ض filesاز رزؼب غ ا commandsعز ك زا اغضء ػ ا

Listing filesؼ

WILDCARDS

Page 22: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 21 ال رغب

از filesثبؾظ ػ ا از رغؼ ب characters ا غػخ wildcardا

جؾش ػب رؾذذا ؽغت ااطلبد از شذب ض البد از رجذأ ثؾشف ؼ ا رز

ثؾشف ؼ

wildcard charactersز ثؼغ ا

Symbole Name Purpose

* Splat رض ايcharacter ا غػخ اcharacters

? question

mark اؽذ كوؾ characterرض

[] square

brackets اؾظسح ث االهاط charactersرض غػخ ا

! bang رؼ ال

WILDCARD CHARACTERS WITH THE ASTERISK )*(

اؽذ ا اضش ض : characterـ matchingرو ثؼ

THE QUESTION MARK ( ? ) WILDCARD CHARACTER

(characterاؽذ كوؾ )اي characterـ matchingرو ثؼ

Page 23: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 22 ال رغب

THE SQUARE BRACKETS ) ][ (

ذي اعد ث االهاط matchingرو ثؼ

الؽع الشم ث :

ث

THE BANG ) ! (

زبئظ ب ػذا از رؾزي اي ا ز اؼالخ رو ثؼشع عغ ا inverse matchingرو ثؼ

ػ ز او

Page 24: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 23 ال رغب

امثلة :

touch {planes,trains}_{10,11}.{bak,bat}

ك عطش اؽذ، زظ ػ زا ا filesغػخ ا touchثؼ commandو زا ا

command ز اfiles

Planes_10.bak, planes_10.bat, planes_11.bak, planes_11.bat, trains_10.bak,

trains_10.bat, trains_11.bak, trains_11.bat

RETURNING TO FILE COMMANDS

از رزؼب غ ا bash scriptingاغزخذخ ك ا commandsعزؾذس ػ ا ا

files

LS

، directoryاعدح ك ا files ا Listغزخذ ك ػ commandزا ا

optionsا ؾزي ػ ثؼغ

-l ؼ :long list زا اoption و ثبظبس ثببد اضش رلظال ػ اfiles ض ا

permissions اlast modification time اsize اowner اgroup

-a و ثبظبس ا :hidden files ).. .( از رؾزي ػ

اؾب directoryاعدح ك ا hidden files ).( ا از رؾزي ػ filesا

parent directoryاعدح ك ا hidden filesاز رؾزي ػ )..( ا filesثب ا

Page 25: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 24 ال رغب

CP

copy filesثؼ commandو زا ا

MV

move filesثؼ commandو زا ا

WC

wordsا charactersا Linesؼذد ا countو ثؼ

NL

fileك ا linesػؼ ػذد ا

SYSTEM COMMANDS

,df, du, fdisk, iostat, vmstatض : systemاز رزؼب غ ا command ا

free

df و ثؼشع اfree disk space

du و ثؼشع اdisk usage

fdisk و ثؼشع اpartitions اعدح ك اhard disk

iostat و ثؼشع ؽبخ اinput/output ػ اhard disk

vmstat و ثؼشع ؽبخ اvirtual memory

Page 26: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 25 ال رغب

free و ثؼشع اfree memory

THE DF AND DU COMMANDS

df و ثؼشع ا :partitions غبؽخ البسؿخ ػ اغبص ؼبد ػ ض ا اعدح

ؿشب mounted partitionsاغبؽخ ازئخ ا

ػ ا checkو ثؼ scriptك ػ commandارا ك اعزخذا زا ا

partitions اظبسalarm ا ط ا ؼ thresholdػذ ازالئ

ؼشػب ثش غ ، byteثب free disk spaceو ثؼشع ا commandزا ا

ض : optionsهشاءر ك اعزخذا ثؼغ ا

df -hT

human readableرؼ ”h–“ؽش ا ا

,partition ext2, ext3,extended رػؼ ع ا typeرؼ ”T“ا

hardازي ػؼ اي البد ب ؽغ جش رغزي غبؽخ ك ا du اعزخذا االش

disk

Page 27: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 26 ال رغب

file systemػ اغبؽخ اخ اغزخذخ ا summaryؼشع ”s-“ اػبكخ

du -s

THE FDISK COMMAND

HDDاعدح ك ا partitionsو ك االعبط ػ اظبس ا fdiskا

rootاال ا غزطغ رلزال commandزا ا

THE FREE COMMAND

ب غبؽخ ا freeا usedا total memoryو ثبظبس ا command زا ا

swap اused اfree ب اؼب

Page 28: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 27 ال رغب

THE VMSTAT COMMAND

از رؼ ا processػذد ا how busy the system isثبظبس commandو زا ا

blocked process

ا swap daemonض وذاس اعزالى ا memory ب رو اؼب ثبظبس ؼبد ػ ا

memory ب هخ اbuffer اcache

ا usersاز خشعذ ثببد ػ ا swapاز دخذ ا process ػذد ا

system اidle and waiting time

THE IOSTAT COMMAND

ؼبد ػ systemك ا input/outputثؼشع ؼبد ػ ا commandو زا ا

system performanceا

Page 29: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 28 ال رغب

install sysstat ػ system performanceؼشكخ اضذ اؼبد ػ ا

package

STDIN, STDOUT, STDERR

اخشاط ازغخ ػ اشبشخ CLIازثخ ك ا commandsو اظ ػ رشـ ا

outputsا inputsزا ؼ ا اظ ؼزذ ػ ا errors ا ب ؾزي ػ ؽز

، شض ثشض ؼ errorsا

Type Symbol

standard input (stdin) 0<

standard output (stdout) 1>

standard error (stderr) 2>

ع اضالصخ file descriptorرغ (2 ,1 ,0)ز االسهب

file descriptor standard inputض (0)كب

file descriptor standard outputض (1)كب

file descriptor standard errorض (2)كب

STDIN

ظ ب : dataبى اؼذذ اطشم از ادخب ا

1- terminal ا :CLI ز زت كب ا اcommands

2- device ا :keyboard ا اdevice از و ثبدخب اجببد ب

Page 30: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 29 ال رغب

3- file :file ؾزي ػdata ض غزخذب اظ ك ػخ اخشي و ثب

cat < myfirstscript

cat 0< myfirstscript

cat myfirstscript

input fileا ؤخز ا catا طت commandزا ا ثذال myfirstscriptاع

ؼشػ ػ اشبشخ keyboardا

STDOUT

by default ظش اoutput ، كبرا هب ثزبثخ االش ػ اشبشخ

cat myfirstscript

عظش ػ اشبشخ outputكغغذ ا ا

ب ا ب اخش redirect outputؼ رـش ري ، ك ا و ث

cat myfirstscript > newscript

cat myfirstscript 1> newscript

Page 31: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 30 ال رغب

ػ اشبشخ، stdoutكظشد ا redirectionثذ ا myfirstscriptكؼذب ػشػب ؾزبد

new scriptغ fileزت ا stdoutعؼب ا redirection ػذب اعزخذب ا

fileػ اشبشخ رت ا ا outputك ظش ا

، اب ارا ب ا creationؿش عد اعبعب كغف و ثؼ newscriptارا ب ا

newscript عد غجوب كغف غؼ ؾزبر زت اdata اغذذح

stdout ا newscriptا stdout ا myfirstscriptارا كب ا

? newscript او ؾزبر ا stdinا myfirstscript برا ارا بى

ب ك اضب ازب : stdin از عزظجؼ screenاالعبثخ ب ا ا

زبر رؤخز ؾ stdin رغذ catكب ا redirectionغ ا catػذب اعزخذب اش

ظجؼ command lineثب input fileالعزؼبػخ ػ ا زي ر ا newscriptرؼؼب ك

inputsغطش ازب ازظش ؽز ذخ ا curserاجذخ ، كبزو ا input method ا

ا catؽز خجش ا CTRL+D (^D)و ثبؼـؾ ػ inputsثؼذ االزبء ادخب ا

ازذهذ خ االدخب ػ

USING STDIN AND STDOUT SIMULTANEOUSLY

ث البد copyغ ثؼؼب اجؼغ جذ ؼخ ا stdoutا stdin اعزخذا ا

cat myfirstscript newscript

Page 32: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 31 ال رغب

cat < myfirstscript > newscript

redirectionبعزخذا ا ث newscriptا myfirstscriptب هب ثغخ ؾزبد

APPENDING TO A FILE

fileرؼ اػبكخ اجببد ا ا appendingك ػخ ا redirection اعزخذا ا

ثذ غؼ اجببد اوذخ

newscript و ثبزبثخ ػ catكب ا (<<)غذ ب اب ػذب اعزخذب اؼالخ

خ اجببد اغذذح ااب هب ثبػبك

Page 33: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 32 ال رغب

STDERR

زظ ػ زغخ اص: commandػذ رلز اي

(valid output)غخ طؾؾخ ز -1

(error messageزغخ خبؽئخ ) -2

ؽش ظش الب ػ اشبشخ errorاؼبدي ؾذس غ ا outputب ؾذس غ ا

root userظ ى normal userى commandكبرا هب ثزلز زا ا

find / -name "*" -print

valid output errorكغزظش ازغخ ػ اشبشخ ب ثبظسح ؽش رؾزي ػ

message

اخش ؽز fileا errorكوؾ ك ا و ثبسعب ا valid output ارا اسدب اظبس ا

dev/null/ ؿبجب ب fileال ظش ػ اشبشخ ، زا ا

ال dev/null/ال ا windowsاعدح ك ا recycle binرشج ا dev/null/ا

linux( زي ك رغ shift + delete اسعبع ؾزبرب شح اخشي )ب ؾذس ك ا

black hole

stderrص ثب اخب file descriptorغت اعزخذا ا dev/null/ا ا errorالسعب ا

stderrا stdoutؽز غزطغ اظ ازض ث ا (<2)

find / -name "*" -print 2> /dev/null

اخش redirectionا commandغت ا scriptز رلز ك ا

Page 34: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 33 ال رغب

اؽذ ازي ظش ػ اشبشخ valid outputب غذ ا ا

troubleshootingا diagnosticsلذح ك ؽبخ ػ errorsهذ ر ز ا زي لؼ

ثذال errorsخبص خالخ االؽالع ػ ز ا fileا errorsز ا اسعب

nullاسعب ا ا

STDOUT, STDERR AND USING THE AMPERSAND )&(

كضال ارا اسدب رلز commandؼب ك لظ ا stderrا stdout اذظ ث ا

command ؼ اسعبvalid output ا file غvalidout اسعب اerror ا

file غerrorout ك رلز ري ك عطش اؽذ كوؾ

find / -name "*" -print 2> errorout > validout

ؿش ب redirectionرشرت ا

find / -name "*" -print 2> errorout > validout

find / -name "*" -print > validout 2> errorout

normal userثب findشح اخشي .... ارا هب ثزلز االش

find / -name "*" -print

valid output errorكغزظش ازغخ ػ اشبشخ ب ثبظسح ؽش رؾزي ػ

message

Page 35: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 34 ال رغب

كوؾ redirect valid outputػ كغز (<)ارا هب ثبعزخذا ػالخ

كوؾ redirect errorكغز ػ (<2)بعزخذا ػالخ ارا هب ث

ب (&)ؼب و ثبعزخذا اؼالخ errorا redirect valid output و ثؼ

find / -name "*" -print 2> samefile 1>&2

validص شع ا same fileا ق اع errorثبسعب ا commandؽش و زا ا

output ا لظ اfile ازي ر اسعب اerror زبثز ثزا اش ،

find / -name "*" -print 1> file 2>&1

ص شع ا fileا ق اع valid outputثبسعب ا commandؽش و زا ا

error ا لظ اfile ازي ر اسعب اvalid output

&>file

fileؼب ا ا valid outputا redirect errorو ثؼ

UNAMED PIPES

، ا شع fileا commandالي outputؽز اال اعزطؼب ا شع ا

اخش زا ا ا ؾظ ػ ازغخ اطثخ ??? commandا inputـ outputزا ا

ض (|) pipeري ػ ؽشن ا االعبثخ ؼ !!!

ls -la /etc | less

Page 36: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 35 ال رغب

ا زا ثؤخز وص etc/لبد اعدح ثذاخ long listثؼشع ”ls -la“ب و ا

output ذخ ـinput ا االشless ازي و ثؼشع ازغخ اال طلؾخ طلؾخ

ػذح شاد ض : pipe اعزخذا ا

ls –la | grep a| less

redirectionغ ا pipe اعزخذا ا

Page 37: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 36 ال رغب

CHAPTER 2. THE SHELL

INTRODUCTION

? shellب ا

عذ ػب : commands زشع ا shellا

1- Login shell

ا usernameاز ظش كب ا system اغئ ػ دخي ا ا

password

2- Non-login shell

، bash, sh, ksh, cshض ا commandsاز زت كب ا terminal ا

اعزخذاب non-login shell 50بى ب وشة

Page 38: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 37 ال رغب

bashغت ا ؼشف ق رؼ ، غت ا ؼشف اب عغزخذ ا shell ل ا

بد اظال االضش اعزخذاب ك ؼظ رصؼ

اغزخذخ shell ؼشف ب ا

echo $0

WHAT IS THE LOGIN SHELL?

: اغئخ ػ login shellا

non-login shellرشـ ا -1

ض systemاز ؾزبعب ا environment variablesا ػ checkو ثؼ -2

system ازي غزخذ ا PATH, TERM, UID, GID userا

,USERNAME, HISTSIZE, HOSTNAMEض ا default variablesرو ثؼ ا -3

HOME

Page 39: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 38 ال رغب

: filesػ ػ ا checkثؼ login shellو ا systemػذ ثذاخ رشـ ا

1- User profile files

2- Shell rc files

رؾزي etc/ رعذ ك profile bashrcرغ filesكز ا systemثبغجخ

اغئ ػب ا systemاز رغزخذ ك ا functionsا aliasesػ ا

root

login shellاز غزؼ ا functionsا aliasesػ ا etc/profile/ؾزي ا

systemذخ ػ ا

Page 40: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 39 ال رغب

non-loginاز ؾزبعب ا functionsا aliasesػ ا etc/bashrc/ثب ؾزي ا

shell ػذ ثذاخ اذخ ا اterminal زؼشف ػ االػذاداد از غزخذب اuser ك

shellازؼب غ ا

Page 41: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 40 ال رغب

رعذ ك ا bashrc .bash_profile.كز البد رغ normal usersاب ثبغجخ

home directory اخبص ثuser رhidden غئخ ػ ا functions ا

aliases اخبطخ ثبuser لغ ، غزطغ اuser ازؼذ ػب االػبكخ ب

functionsا aliasesؾزي ػ ا bash_profile.ب شي ك اظسح ازبخ ا ق

ا ا ػذ ثذاخ systemاز ؾزبعب ا Userاخبطخ ثب user profileاذخ

Page 42: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 41 ال رغب

ك userاز غزخذب ا functionsا aliasesك ؾزي ػ ا bashrc.ثب ق

non-login shellازؼب غ ا اخبطخ ث( terminal)ا

login shellبلبد اخبطخ ثب ارا ك

/etc/profile >>>>> for system

.bash_profile >>>>> for user

loginػذ ا systemغزخذب ا system initialization dataػ رؾزي ز البد

ك : non-login shellثب البد اخبطخ ثب

/etc/bashrc >>>>> for system

.bashrc >>>>> for user

شذ ػب functionا aliasثبزؼذ ػ لبر اػبكخ اي User ا و ا

etc/profile /etc/bashrc/ اؽذ اوبدس ػ رؼذ لبد ا root user ا

ز رشـ ل terminal (non-login shell or bash)ك شح و ثزشـ ا

/etc/bashrc ~/.bashrc

ػ ب ػ ؽشن رلز االش از اػ ػ bashاخشي لظ ا bash اذخ

bash

Page 43: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 42 ال رغب

الزؽخ ، خشط terminalعذذح( ك لظ ا tabعذذح )ظ bashكز رشـ

زت االش

exit

-subا child bashاغذذح رغ bashثب ا parent bashاالطخ رغ bashا

shell

ال و ثزشـ ا systemاوذخ كب ا bashعذذح ا bashارا هب ثبذخ ػ

profile files شح اخشي

login shellا ا bashكغف خشط ا exitارا اعزشسب ثزبثخ االش

”rc“رؾزي ػ initialization filesك اظ ثش ػب غذ ا عغ ا

runlevelاغئخ ػ ا rc0, rc1بى vimاغئخ ػ رشـ ا vimrc.كبى

initialization زا

THE JOB OF THE SHELL

ثبالر :و shellكغغذ ا ا shellارا اسدب ؼشكخ اظلخ الؼخ

COMMAND INTERPRETER

اخبص ث، PATHػ ؽشن اجؾش ػ ك ا commandثزشعخ ا shellؽش و ا

كضال :

-command builtؼ ا زا ا command interpreterكب ا cdزخذا االش ػذ اع

in command كال ؾزبط زبة اPath ثزلز اخبص ث ؼشكخ ق و

ؼشكخ shellزي غت ػ ا built-in commandك ظ mv ػذ اعزخذا االش

لز غزطغ ر commandؼ زا ا

Page 44: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 43 ال رغب

ؼشكخ طؾؼ ا خبؽئ commandثزشعخ ا shellو ا

ALLOWS FOR VARIABLES TO BE SET

اخبطخ ث ازؼذ ػب ض ازؼذ ػ variablesثؼ ا userاؼب shellغؼ ا

HOSTNAMEا ا PATHا ا HISTFILESIZEا

I/O REDIRECTION

errorsا Outputا redirection input ب غؼ ثؼ

PIPELINES

pipe lineاؼب ا shellل ا

CUSTOMISING YOUR ENVIRONMENT

اخبط ث ػ ؽشن ازؾ ك environmentثبزؾ ك ا shell Userب غؼ ا

ؿشب promptش ا running scriptsا variablesا

Page 45: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 44 ال رغب

CHAPTER 3. REGULAR

EXPRESSIONS

INTRODUCTION

scripting ا االشبء ك ا regular expressionا

WHAT ARE REGULAR EXPRESSIONS?

(*) ظلخ ؼ ض characterب ذ ا regular expression patternا

، عزؾذس اال ػ ثؼغ ااع ا charactersي ػذد ا أل matchingازي و ثؼ

pattern

THE FULLSTOP (PERIOD) ”.”

match any characterرؼ fullstopا

ص ”n“ص ”i“ص ”l“ا ش جذأ ثـ patternجؾش ػ اي sedكبرا هب ضال ثبعزخذا االش

“u” ص و ثزجذ “l” ا“L” ا“i” ا“I” ا“n” ا“N” ا“u” ا“U” ص و

كغغزخذ االش ”X“ا كوؾ ازب patternثزجذ ا

sed 's/Linu./LINUX/g' fullstop_test

ؼ commandزا ا

s (search for) / Linu. / (replace with) LINUX / g (globally)

<filename to search>

---------------^-------^----------------------^-------------^---

---------------------

Page 46: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 45 ال رغب

pattern رزـش ز اعزجذاب ؽش غت ا ا ”linu“الؽع ك اظسح ازبخ ا

ػ زا ا ”sed“اخش ؽز طجن ػ اششؽ اال ك ز رطجن characterزجػب ثـ

pattern

number the linesرؼ ”nl“، ؽش ب ثبضب ازب pipeغ ا sed اعزخذا ا

in the file

sed 's/Linu./LINUX/g' fullstop_test | nl

Page 47: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 46 ال رغب

LET'S EXPLORE "SED" SYNTAX

اعزجذا ثآخش ، عطش عطش ؼ patternك و ثبجؾش ػ ، sedزؾذس هال ػ ا

sed ؼstream editor ا ،stream ؤر اfile اشاد اجؾش ك ػ اpattern

commandاؼبخ زا ا form ا

sed „[command] / pattern / [replace sequence] / [modifier]‟

[command]

sed 's/Linu./LINUX/g' fullstop_test

sed زجؼبcommand اخش ػؼ اaction ، ضبب كب زا ا ب كاشاد ػ

command „s‟ ؼsearch

ث ػالز patternص ؤر ثؼذ ا ك ضبب .linu ”/“اشاد اجؾش ػ ؼؼ

ػغ اؼب اغذذ ازي عؾ ؾ اوذ pattern ا replaced sequenceجزؼ ا

ك ضبب ”LINUX“ ”/“ث ػالز

ا و ثؼ sed، خجش ا globallyؼ ك ضبب زا ”modifier “gؤر ا ص

ال زهق ػذ ا زغ fileػ ا replaceزا ا

”g“ثذال ”1“كوؾ كؼغ اشه matched patternاب ارا اسدب ا غزجذ ا

”g“ثذال ”2“كوؾ كؼغ اشه matched patternاب ارا اسدب ا غزجذ صب

patternsاشاد اجؾش ثذاخ ػ ا fileص ؤر ك ابخ ا

Page 48: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 47 ال رغب

sed „/hi/p‟ myfirstscript

print Matched line و ثؼ ”g“ثذ ”p“ ا ؼغ

Page 49: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 48 ال رغب

”hi“از رؾزي ػ lines ا delete و ثؼ ”d“ ا ؼغ

SQUARE BRACKETS ( [ ] ), THE CARET ( ^ ) AND THE DOLLAR ( $ )

اؽذ ث match characterرؼ ا و ثؼ square brackets ([abc])ا

range of characters اعدح ث اوع

characterجذأ ثؤي از ر lines عغ ا matchرؼ ا و ثؼ caret (^hmed)ا

عد ث االهاط

character ز ثؤياز ر lines عغ ا matchرؼ ا و ثؼ Dollar (ahme$)ا

عد ث االهاط

sed „/^[ahl]/p‟ myfirstscript

”[ahl]^/^“ػ ؽشن اظـخ invert selectionك ػ ”^“غ ا sed اعزخذا

sed '/^[^ahl]/p‟ myfirstscript

a,h,lؿش characterجذأ ثؤي lineؼ اؽجغ اي commandزي كب زا ا

Page 50: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 49 ال رغب

sed '/^[^ahl]/d‟ myfirstscript

از رجذأ ثبي ؽشف ؿش عد ث االهاط linesزا ؼ اغؼ عغ ا

ؼذ ا ب رػغ ث ($) dollarغشي اؼب ػ ا (^) caret اؾب ك ا ب

brackets

sed '/[it]$/!d' myfirstscript

اغؼ ”t“ا ”i“از رز ثب linesاال رو ثغؼ ا commandؼ زا ا

اجبه

اعدح ك ا linesاال هب ثغؼ ا commandب شي ك ز اظسح ا ا

file: myfirstscript ا ظش ػ اي زغخ ال file : myfirstscript ظ ث ايLine

linesكوب ثغؼ ا ”l“ا ”h“ا ”a“ز ثب

غؼ ثبه ”i“ازي ز ثب lineثبالؽزلبظ ثب sedاضب هب ا commandثب ك ا

ض ك اضب اضبش linesا

Page 51: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 50 ال رغب

اغطش البسؽذالخ ػ ”$“غ ا ”^“ب اعزخذا ا

sed „/^$/d‟ myfirstscript

از اعزخذبب : expressionsخض ا

. any single character

[] a range of characters

^ start of line (when outside [ ])

^ do not (when inside [ ])

$ end of line

^$ empty line

* 0 or more of the previous pattern

+ 1 or more of the previous pattern

\{n\}

\{n, \}+

\{n,m\}

USING SED AND PIPES

Page 52: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 51 ال رغب

sedري ػذ اشؿجخ ك اعشاء اي ػخ ػ زغخ ا pipeغ ا sed اعزخذا ا

sed '/^[^ahl]/d' bazaar.txt | sed '/^$/d' | nl

ثبعزخذا pipeغ ثؼغ ثذ اعزخذا ا sed commands 2زكش اهذ دظ ا

sed 2اؽذح كوؾ غ ػغ ا command options ث اؼالز„ „

sed '/^[^ahl]/d;/^$/d' bazaar.txt | nl

رؼط لظ ازغخاخشي form اعزخذا

Page 53: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 52 ال رغب

cat myfirstscript | sed '/^[^ahl]/d;/^$/d'

THE SPLAT (ASTERISK) ) * (

patternاء ب زا ا ع هجب اعد matching paternثؼ ثو asteriskا

شس ا ال

sed '/ahm*/p' splatscript

grep “a*” splatscript

Page 54: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 53 ال رغب

ؿش ”*“اغبثن characterؽز ب ا patternؼشع ا get ز اظسح كب

1999 اغطش اال عد ب ك

THE PLUS OPERATOR ) + (

patternاعد هجب ثششؽ ا زا ا matching paternثو ثؼ plusا

عد

Page 55: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 54 ال رغب

MATCHING A SPECIFIED NUMBER OF THE PATTERN USING THE CURLY

BRACKETS }{

{}ذد اشاد ثبعزخذا ا ؼ ؼ patternؼ زا اؼا ا رشاس

a{4} = aaaaكزا اش

sed '/19{3}/p' splatscript

”{“ا “}“هج ا ”\“ غت ػغ

sed '/19\{3\}/p' splatscript

Page 56: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 55 ال رغب

ا

grep “19\{2\}” splatscript

form كغزخذ ا characters 3الي matching ارا اسدب ا ػ

grep “\<[a-z][a-z][a-z]>\” 3character

lettersزؾ ك ػذد ا encapsulationرغ (<\ and >\)ز اؼالخ

Page 57: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 56 ال رغب

ثزا اش command زبثخ زا ا

grep “\<[a-z]\{3\}>\” 3character

A DETOUR - USING A DIFFERENT FIELD SEPARATOR IN SED PATTERN

MATCHING

ك ق ؼ patternؾش ػ زا ا ارا اسدب اج

/home/hamish/some_where

ثزا اش sedك اعزخذا ا

sed „/\/home\/hamish\/some_where/!d‟ pathscript

اعزجذا عغ ا form طؼجخ ا ؽذ ب ، زغ زبثخ ز ا form ز ا

commandاؽذح كوؾ ك ثذاخ ا ”%“ثـ regular expressionsاز رػغ اب ا ”\“

بز

”\“غت ا غجوب اؼب ”%“غ االخز ك االػزجبس ا ػالخ

regular expressionا ب عف ؤر ثؼذب shellرو رخجش ا ”%“زا ال ػالخ ا

sed „\%/home/ahmed%!d‟ pathscript

Page 58: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 57 ال رغب

USING WORD ENCAPSULATING CHARACTERS

shell زؼب ؼ ا ”\“ػغ هج encapsulationرغ (< >)ز اؼالخ

(<\ >\)كظجؾا ثزا اش regular expresionsػ ا

ػ ازغخ اطث ثذهخ pattern رـق ا encapsulationظلخ ا ؾظ

الؽع الشم ك اظسح ث االش

ؽز 321ثبالسهب characters line 3ثبعزجذا ا shellؽش ا االش اال هب ا

characters 3ؾزي ػ اضش Lineا ب بى

كوؾ characters 3از رؾزي ػ linesثبعزجذا ا shellثب ك االش اضب هب ا

RETURNING FROM DETOUR TO OUR DISCUSSION ON CURLY BRACES …

ػ االه ػ ؽشن a” characters“ 2ـ matching ػ

sed „/a\{2,\}/!d‟ infinit

2ثؾذ اد matchكزا ؼ {2,4}ارا ػؼب داخ اوط 4ثؾذ اهظ

sed „/a\{2,4\}/!d‟ infinit

THE TR COMMAND

smallا ا capitalخ اؾشف ا ثزـش ؽب و translateؼ

Page 59: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 58 ال رغب

fileػ redirect outputثزا اش ػ command رلز ا

، ؽش غبػذب ػ ازخض reportsك رغ ػ ا tr command اعزخذا ا

غبػذ ك ازي option : -sاعدح ثضشح ك ري ػ ؽشن tabsا spaces ا

اؽذ كوؾ characterازشبث ا charactersػـؾ ا

اخش ، columnكغغذ ا ؾزي ػ اضش اغبكبد ث freeكضال ارا ػشػب االش

كبرا اسدب ا ؼـؾ ز اغبكبد عؼب غبكخ كغزخذ االش

free | tr –s “ ”

اخش شذ ػـط characterثؤي space اعزجذا ا

Page 60: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 59 ال رغب

THE CUT COMMAND

زؾذذ delimiterثبعزخذا lineغزخذ ك هطغ عضء ؼ ا command : cutا

از عز اوطغ ػ اعبعب الاط

IFS (Input Fieldثبع shell variablesعد ك ا delimitersز ا

Separators)

,cut t = tab, n=new lineاؼشكخ ذي ا delimitersا ب شي ك اظسح كب

“ ”= new line

free | tr -s ' ' | grep Mem | cut -d" " –f1-3

d: delimiter–ؽش

.f : field no–ا

Page 61: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 60 ال رغب

ثب fields اوخ از ثؼذ اغبكخ ، ثؼ ا اغطش وغ ا fieldا لظ

( delimiterغبكبد )

cut charactersؼ ”c-“ ا غزخذ

free | tr -s ' ' | grep Mem | cut -c6-13,15,17

اؽذ كوؾ characterػجبسح ػ delimiter ا spaceثذال tabارا اسدد ا رغزخذ

tabص CTRL+v، زي ؼـؾ IFSب ك ا ”t\“ثزا اش tabال ا زت ا

THE PASTE COMMAND

االخش lineث ”tab“ثؾش ؼغ files 2و ثذظ ؾزبد command : pasteا

paste pastescript pasteequal

ب ؼغ زا ا command: cutب ؾذس ك ا delimiters اعزخذا

delimiter ثذال اtab

Page 62: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 61 ال رغب

paste –d”;” pastescript pasteequal

THE UNIQ COMMAND

و ثؾزف اغطس اشسح ثششؽ ا ر ززبخ uniqاالش

اغزخذخ shellsاسدب ؼشكخ ب ا etc/passwd/كبرا هب ثبعزؼشاع ؾزبد ق

shellsكغذ ا اق ؾزي ػ اضش اغطس ازة ثب لظ ا

و ثؾزف اغطس ازشبث اؾلبظ ػ اخزق كوؾ uniqزخذا كو ثبع

cut –d”:” –f7 /etc/passwd | uniq

Page 63: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 62 ال رغب

shells، بصاذ رظش هب ثؾزف اغطس اززبخ ازشبث uniqا ب غذ

ثؼ sort fileزشبث ، كبؾ ا و ثؼ uniqاال ص و

cut –d”:” –f7 /etc/passwd | sort | uniq

THE SORT COMMAND

”z“ا ”a“و ثزشرت ؾزبد اق االطـش ا االجش sort commandsا

Page 64: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 63 ال رغب

ؼ redirectionثذال ػالخ ا sortغ االش option : -o اعزخذا ا

output

cut -d: -f7 /etc/passwd |sort -ruo test

sortاز رغزخذ غ االش optionsز ثؼغ ا

Option Action

Page 65: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 64 ال رغب

-o و ثؼredirect ػfile ثذال“>” -u ؼuniq sort

+t ا delimiter اخبص ثبsort از عزغبب fieldsػذد ا 3+-n زشرت اغطس ؽغت االسهب ظ اؾشف -r ؼ reverse sort

cut –d”:” –f3,7 /etc/passwd | sort –t: -rn

THE GREP COMMAND

regularب غزطغ ازؼب غ ا fileا ظش داخ filterـ grep ا غزخذ ا

expression

form ك ا غزخذ ز ا fileظش ثذاخ grepارا اسدب اعزخذا ا

grep “/bin/bash” /etc/passwd

داخ ػالز patternكوب ثػغ زا ا ”/“ازي جؾش ػ ؾزي ػ patternال ا

ص ”/“جذأ ثـ patternال جؾش ػ خ جؾش ػ grep"" ، زي غت ا ؼشف ا ا

“b” اخ

:كضال grepالذح optionsبى ثؼغ ا

Page 66: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 65 ال رغب

pattern و ثؼشع ػذد ؼ اغطس هج ا option : -B1 اػبكخ

patternؼشع عطش اؽذ ثؼذ ا A1– اػبكخ

ثؼ ا ؼشع اغطس ب ػذا ا reverse patternؼ v– اعزخذا

pattern

Page 67: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 66 ال رغب

(patternؾزي ػ ا )ؼغ سه اغطش ازي lable patternؼ n– اعزخذا

Option Action

-B1 ؼشع اpattern اline ازي غجو

-A1 ؼشع اpattern اline ازي -v ؼشع اغطس ػذا از رؾزي ػ اpattern -n ؼغ سه اغطش ازي ؾزي ػ اpattern ثغبج

working directoryاعدح ك ا files عغ ا ahmedارا اسدب ا غزخشط خ

: ازبخ form ك ا غزخذ ا patternازي ؾزي ػ زا ا Lineؼشكخ سه ا

، ب هب ثؼشع اع ا filesؽز جؾش ك ا ”*“ثـ fileؽش هب ثبعزجذا اع ا

file ازي ؾزي ػ اPattern اغطش ك ا سهfile

Page 68: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 67 ال رغب

filterـ grepاب ارا اسدب اعزخذا

grepبال ػ ا outputؽش ذخ ا pipeغ ا grepك ؽبخ اعزخذا ا يك ر

اطثخ data اعزخشاط ا filtrationكو ثؼ

ب ك اضب ازب : logsظش ري ثػػ ك ازؼب غ ا

tail -10 /var/log/messages |grep '19:[0-5][0-9]:[0-5][0-9]‟

4 اخشط filterكوب ثؼ grepعطس ادخب ػ output tail 10اخزب ا كب

عطس كوؾ

GREP, EGREP AND FGREP

grepااع ا 3بى

Type Function

Grep ا standard grep Egrep ضشح غ ا غزخذ ثextended regular expressions ثطئ Fgrep ال غزخذ غ اregular expressions زي ك عشغ

grepاعشع هال ا sedا

Page 69: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 68 ال رغب

CHAPTER 4. PRACTICALLY SHELL

SCRIPTING

SECTION TECHNIQUES TO USE WHEN WRITING, SAVING AND

EXECUTING SHELL SCRIPTS

ػ ا commandsغت ا جذأ ثزطجن ا scriptدػب زلن ػ اب ك ثذاخ ػ اي

command line ثؼذ ازؤذ ػب ثش عذ و ثوب ا اscript file

كو اال ثزطجن ا systemاغزخذخ ك ا shellsو ؼشكخ ا scriptكبرا اسدب ػ

command ا ػterminal

cat –d: -f7 /etc/passwd | sort –u

script.shك ق غ ضال commandص و ثؼذ ري ثػغ زا ا

عف زؼشف ػب اال script بى ثؼغ اواػذ غت ا ؼب ػ ا

DETOUR: FILE EXTENSION LABELS

extensionsاظ ال ز ثب -1

ب هذ ؼق االزذاد ا اع everything is a file اوبػذح االعبعخ ك ا ال ؼشك ال

اخ file script ززش ؾ ا زا ا fileا ا ق ظ

script

script.sh

ري ثزلز االش executableغت ا scriptا -2

chmod +x script.sh

fileا زا ا shellؽز ؼشف ا ”sh“ا ”/.“اب غت ا ؼغ scriptزلز ا -3

script

./script

sh script

Page 70: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 69 ال رغب

COMMENTS IN SCRIPTS

ػ اػزجبس scriptك ا زج ثذاخ ا scriptارا اسدب ا ششػ ظلخ عطش ؼ ك ا

اشاد اػزجبس ـ lineاب ا (#)ثزلز ، ؼ ري ؼغ shellال و ا commentا

comment

#A script to produce the unique shells using the /etc/passwd

file

cut -d: -f7 /etc/passwd |sort -u

VARIABLES

ثبع memoryز ؽغض غبؽخ ك ا ؽشوخ ئهز زخض اؼبد variablesا

variableػغ كب ا variable nameا

NAME=”ahmed”

variable ahmed، ا variable name NAMEؽش ا

variablesبى هبػذر زؼب غ ا

NAMEب شي خ capitalغت ا variableا -1

valueا ”“ال ث ا ”=“ػالخ variable nameال رعذ اي غبكبد ث ا -2

ػالخ valueاالعزلبدح ثب variableزا ا زؼب غ ”$“غت ا ؼغ اب

echo "$NAME"

scriptز اثغؾ اشب زبثخ ا

Page 71: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 70 ال رغب

SHEBANG OR HASHPLING !#

ؼ script، ارا هذ ثزبثخ بى اجؼغ هذ ال غزخذ bashاضش ب غزخذ ا

shells ع اخش ا كوذ ال ؼ ثش طؾؼ ػ Bashػ ا

ري ػ ؽشن اؽذ اطشوز :ؼ shellا ؼ ك scriptزي كغت اعجبس ا

shثبعزخذا االش scriptرشـ ا -1

sh script.sh

shebangاػبكخ ا -2

ك ثذاز scriptرػغ ثذاخ ا ”bang : !“ا ”hash : #“ ضظ ا shebangا

scriptا ازي عزؼب غ interpreterذالخ ػ ع ا

#!/bin/bash

EXIT

عزؼشف ػ اطشوخ از غت ا scriptجذاخ ا standardثؼذ ا ػشكب اطشوخ ا

ending statusري ػ ؽشن ب ؼشف ثب scriptثب ا

رػؼ ق از ػ زا اجشبظ ) از كؼوت ازبء ػ اي ثشبظ رخشط هخ ؼ

exit valueثش طؾؼ ا ث اخطبء( ز اوخ رغ ا

exit value =0كز ا errorsكإرا از اجشبظ ثش طؾؼ ؾذس

exit value = 1-255كز ا errorsاب ارا ز اجشبظ ثش طؾؼ ؽذس ثؼغ ا

Exit Value Exit Status

0 (Zero) Success

Page 72: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 71 ال رغب

Non-zero Failure

2 Incorrect usage

127 Command Not found

126 Not an executable my be the script is not executable

ػ ؽشن اػبكخ زا اغطش ك بخ scriptك بخ exit statusزي غت ؼشكخ ا

scriptا

echo $?

NULL AND UNSET VARIABLES

null unsetابخ از غت ا زؾذس ػب ض variablesبى ثؼغ ا

ب هخ ظ (set)ح عد variable ا ا variable nullؼ ا ا

(null) بزب

NAME=””

NAME=

Page 73: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 72 ال رغب

”“ػذ عد غبكخ ث ا الؽع nullعد هز Variable=NAMEب كب ا

عطش كبسؽ variableزظ ػ زا ا

زا ال غبي

NAME=” ”

زي ك رغبي valueرؼجش ػ ” “ال اغبكخ ث ا

NAME=”ahmed”

valueك رو ثغؼ ا Unsetاب ا ب variableاخظظ

unset NAME

، زؼؼ الشم اضش شي اضب empty valueا null valueغت ا لشم ث ا

:variablesالؽع الشم ث اضالس ازب

Page 74: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 73 ال رغب

VARIABLE EXPANSION

ب variable=NAME، كبرا ب variablesثزشعخ ا shellب عزؾذس ػ ق و ا

value = ahmed

NAME=”ahmed”

Valueهخ ا ؼشع echoهذ ثبعزخذا ا

echo $NAME

كغ ابرظ ب ثبظسح

ثزا اش command برا ارا زجب ا

echo $NAMEX

اعب variableكظ ا بى $زثخ بخ خق ا NAMEXعغذ ا ا shellكب ا

NAMEX عز ازغخ ػذب جؾش ػب ك غذبerror ب

Page 75: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 74 ال رغب

اظش ا اظسح ازبخ الؽع الشم عذا

ؽشوخ زلشن ث ا ث variableلغ اي شئ ا ؼغ ا variableاكؼ

curly brackets {}

echo ${NAME}X

غذ عضء ب X اعدح ث االهاط ا ا variablا ا shellعف ل ا

ENVIRONMENTAL VS SHELL VARIABLES

از ؼ ػب ؽبب كوؾ shellاز رطجن ػ ا variable ا shell variableال

ثبعزخذا shellلزؽخ داخ ا shellعذذح لزؾب ؽز ارا بذ shellرطجن ػ اي

Bashاالش

Page 76: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 75 ال رغب

وشأب systemا اعدح ك shellsرطجن ػ ا environment variablesال

loginػذ ا systemا

exportغزخذ االش environment variableا shell variableارا اسدد ا اؽ ا

export NAME

ARITHMETIC IN THE SHELL

عزؾذس ػ ا scriptingعزؾذس اال ػ ازؼب غ اؼالبد اؾغبثخ ثبعزخذا ا

integer arithmetic

: scriptsبى ػذح ؽشم زؼب غ او اؾغبثخ اظؾؾخ ثبعزخذا ا

(( ))$اؾغبث ا اؼبدخ اؾغبثخ ك هع ضدع expressionػغ ا -1

Page 77: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 76 ال رغب

scriptز ثؼغ اؼالبد اؾغبثخ از رغزخذ ك ا

Arithmetic operators action

ػالخ اغغ + طشػػالخ ا - ػالخ اؼشة *

ػالخ اوغخ / ػالخ اغجخ ائخ %

رئدي ا لظ ازغخ expressionالؽع ا ز اطشم زبثخ ا

((J=I+5))

J=$((I+5))

J=$(( I + 5))

$(( J = I + 5))

error عد غبكبد هج ثؼذ ػالخ "=" عف زظ ػ

J = $(( I + 5 ))

Page 78: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 77 ال رغب

CHAPTER 5. USING QUOTATION

MARKS

INTRODUCTION

ظلزب اخزالكب ػ االخش ، quotation marksعزؾذس ػ ا chapterك زا ا

quotation marksااع ا 3بى

Our term Real term Symbol

Ticks Single quotes „

Back ticks Back quotes ` ("ػ ؽشف ا "ر)

Quotes Double quotes “

SINGLE QUOTES OR "TICKS"

بخ ثلظ اش ؽز charactersارا اسدب ؽجبػخ ا single quotesغزخذ ا -1

ك ال رزشعب special charactersارا بذ رؾزي ػ

اصبء ازؼب غ ا -2 اعزخذا ، الؽع اش spacesاز رؾزي ػ filesال لؼ

ازب

Page 79: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 78 ال رغب

variablesال رغزخذ ك ازؼب غ ا set variables اعزخذاب ؼ -3

commandsاعدح ك ا

singleب الب ـلخ ثب value ahmed gamilا اػزجشد ا shellا ا الؽع

quotes

new line اعزخذاب ؼ -4

DOUBLE QUOTES

special charactersرغزخذ غ ا -5

ئدا ا لظ ازغخ commandsا ز الؽع ا

Page 80: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 79 ال رغب

ػبدخ ال charactersز ازؼب ؼب ـ ”“اعدح ثذاخ ا special characters ا

„ $ب ػذا ا expansionز ػ

”\“كؼغ ػالخ special characterػبدي ظ characterـ ”$“ارا اسدب اعزخذا ا

بزب

specialرو ثزؾ ا escape characterـ ”\“ؼب ا shellب غذ ا ا

character ازي ب اcharacter ط ال ز رشعزػبدي

غ shellازي و ثزـش ؽشوخ رؼب ا echo –eثش اخش غ االش ”\“ اعزخذا ا

ؽش ”\“ا

Action Command

\n اض غطش عذذ \t اػبكخtab

Page 81: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 80 ال رغب

BACKTICKS

غزخذ شح اخشي، كبرا outputؤخز ا commandلز ارا اسدب ا (``)رغزخذ ا

اسدب ػشع ازبسخ اؾب كغزخذ االش

date

ك كغت ا script ارا اسدب ا ؼشع ازبسخ اؾب اصبء رشـ ا ص variableؼؼ

ثزا اش scriptك ا Variableغزخذ زا ا

variable : DATEعزظ ب رزـش شح اخشي ال ا DATE الؽع ا هخ ا

backticksث ا commandزـش شح اؽذح كوؾ ارا ر رشـ ا

ارا كب اؾ ?

اؾب ػ ؽشن اػبدح رخظض dateك شح شذ ا ظش ا reset DATEغت ػ

شح اخشي variableا

DATE=`date`

ا dateاغذذح valueثوشاءح ا variable : DATEثزي و

Page 82: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 81 ال رغب

commandؼشع اهذ ؽذ ػ ؽشن زا ا variable : DATEب اعزخذا ا

TIME=`echo $DATE | cut –d‟ ‟ –f4`

TIME variableزجؼ رـش ك ا DATE variableثزي كب ازـش ك ا

ب ك اضب variableلغب ثذ رخظظب back ticks برا ارا هب ثزلز ا

ازب

ي ظش ا زلزب ، ز command lineعغذ ا االش ر رلز خشعذ ازغخ ا ا

error ال ال عذcommand رخظض ، 08:49:46اع variable backزي لؼ

ticks commands

سثب ؼؼت سإزب back ticks ال ا back ticksثذال ا ()$ اؼب اعزخذا ػالخ

scriptك ا

SHELL ARITHMETIC'S WITH EXPR AND BACK QUOTES

shellsاز رغزخذ ك اؼبد اؾغبثخ بى ثؼغ ا (())$ب عبثوب ػ وذ رؾذص

بالر exprاع commandاوذخ ال رلب زي ث االعزؼبػخ ػب ثـ

Page 83: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 82 ال رغب

i=0

i=$((i+1))

زا غبي

i=0

i=$(expr i+1)

غبي اؼب

i=`expr i+1`

ANOTHER TIP WHEN USING QUOTATION MARKS

هب ثؼشع ازغخ ls –alؼشع زغخ االش variable : fileارا هب ثزخظض

file=`ls -al`

echo $file

Unformattedكغزظش ازغخ ب ك عطش اؽذ

”“ ؼبظ ز اشخ كغت ا ؼق

Page 84: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 83 ال رغب

echo “$file”

Page 85: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 84 ال رغب

CHAPTER 6. SO, YOU WANT AN

ARGUMENT?

INTRODUCTION

غت ا بزب scriptب ػب ا زشـ اي

./script.sh

اؼب رشـخ بزب

./eatout.sh file.txt

ؼ fileػ scriptلذح ارا اسدب رلز ا form ز ا

argument، ز اخ رغ ب fileػ خ رعذ ثذاخ ا script رلز ا

./eatout.sh Word-in-file.txt

ػ اطبػ dataؾزي ػ script : eat.sh file : restaurant.txtلزشع ا ذب

Italianشذ ا غزخشط ثببد ع ؼ اطبػ

ؾزي ػ االر scriptا ا

Page 86: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 85 ال رغب

ش ازبو ثزبثخ االش ثب script و ثزلز زا ا

./eat.sh Italian

2$ 0$كب ؼب بى 1$ؾزي ػ اشض scriptالؽع عذا ا اغطش اضب ك ا

اخ

رو ثزوغ زا االش ا غػخ اعضاء shellؼ كب

./eat.sh Italian

0$ثبشض eat.sh/.ؽش رض

1$ثبشض italianا

اخ 2$كشض ثبشض italianاخش ثؼذ argumentارا ػؼب اي

positional parametersز اشص رغ

TYPE = $2عؼب ا scriptكبرا هب ثزؼذ ا

smartاضب argumentثببد ا غذ هب ثؼشع scriptهب ثزلز ا

اخش fileزـش ثؾش ػغ اي file : restaurant.txt اؼب ا غؼ اع ا

داخ ا بزب 2$ثبشض scriptشش ا

Page 87: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 86 ال رغب

ازبخ form غزخذ ا scriptػذ رلز ا

POSITIONAL PARAMETERS 0 AND 1 THROUGH 9

از رشش ا parameters غػخ ا positional parametersب رشب ا ا

scriptازي عغزخذ ك ا argument ب ا

0$اي script 10 parameters اعزخذا غ ا parametersاجش ػذد ا

9$ا

scriptا اع ا 0$شش ا

زا argumentا ا 1$شش ا

ثب scriptغ ا ارا اعزخذ scriptرشش ا اع ا 0$ غت ا رززش عذا ا ا

بزب commandاغزخذ ارا اعزخذب ـ shellرشش ا اع ا لظ

Page 88: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 87 ال رغب

رخظض رخظض variable argumentلؼ اعزخذا TYPE=$1ثؼ ا االكؼ

ب ك اغطش argumentظ ا scriptلغب ك ا variableا

grep $TYPE $FILE

زا عئدي ا رـش ؾزبد shift argumentsه عزؾذس ػ لخ ػ الب ثؼذ

اال argument رؾزي ػ لظ ا 1$ثبزب كب positional argumentsا

variableك positional argumentزي كبالكؼ ا و ثؾلع ا

OTHER ARGUMENTS USED WITH POSITIONAL PARAMETERS

ك اؾووخ اعزخذا positional argument 10ب رشب عبثوب ا ػذد ا

اضش ري

$# HOW MANY POSITIONAL ARGUMENTS HAVE WE GOT ?

ا ؼغ scriptاز غزخذب ك ا positional arguments ؼشف ػذد ا

scriptك ا ”#$“اشض

$* DISPLAY ALL POSITIONAL PARAMETERS

داخ ا ”*$“اغزخذخ غزخذ اشض positional arguments ؼشف ب ا

script

Page 89: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 88 ال رغب

USING THE "SHIFT" COMMAND - FOR MORE THAN 9 POSITIONAL

PARAMETERS

0$هب ا positional argumentوذ رؾذصب ػ ا 9$ا 1$ص ب scriptخظظ

ري ا كق ري ? positional argumentرشب ا بى اضش

ػ اضش ثؤي وذاس shift positional argumentsػ ؽشن ػ 9$ اؾظ

كبرا هب

shift 5

14$ <<< 9$ا 6$ <<< 1$عذذح رظجؼ ا 5$ : 1$كجزي ز اػبكخ

ا ز رخظض زي هب ا االك variable positional argumentؼ

EXIT STATUS OF THE PREVIOUS COMMAND

ر رلز commandخش ال exit status رػؼ ا ”?$“عزؾذس ػب اال $اخش

(error) ر رلز ثغبػ ا ا بى

ثش scriptؼ ري ا ر رلز ا exit status 0الؽع ب ا اضب اال ب ك ا

طؾؼ

ثش طؾؼ scriptرلز ا ك errorعد exit status 2 ك اضب اضب بذ ا

fakeغ fileؼذ عد

Page 90: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 89 ال رغب

ؼذ عد scriptرلز ا ك errorعد exit status 1ك اضب اضبش بذ ا

argument غegypt

command ز رلزexit status بby default ال رظش ػ اشبشخ اال

?$ : commandارا اعزخذب ا

Page 91: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 90 ال رغب

CHAPTER 7. WHERE TO FROM

HERE?

MAKING DECISIONS

ب، ثؼ ارا ؽذس زا actionؼ ػذ ؽذس commandضش االؽب ؾزبط ا رلز

ؼ زا ، اي اب ؾزبط ا ع ششؽخاك

if [ expression ]

then

OK #Do something

else

NOT OK #Do something else

fi

TESTING FOR A TRUE OR FALSE CONDITION

، هج ا زؾذس ػب كغو if-elseزي عزؾذس ػ ثؼغ اغ اششؽخ ض

testثبؾذش ػ االش

THE TEST COMMAND

Valuesث ا file type compareػ ا checkو ثؼ test commandكب

ازبخ form ك اعزجذا ا

if [ x -eq 9 ]

then

echo equal

else

echo no equal

fi

ازبخ form ثب

test $x -eq 9

if [ $? -eq 0 ]

then

echo equal

else

Page 92: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 91 ال رغب

echo no equal

fi

ؼشكخ ضذ ازلبط ػ ؽشن

info test

WHAT IS "TRUE" AND "FALSE"

?? shellق ز ازؼجش ػب ك ا falseا trueبرا ؼ

ز ازؼجش ػ ا shellثوخ ؼ، ك ا falseا trueك اي ـخ ثشغخ ز ازؼجش ػ

true ثبvalue 0 ازؼجش ػ ا false 1252000ثؤي سه اخش ض

ب ثبظسح ازبخ falseكضال ز ازؼجش ػ

بذ خبؽئخ ،ز ازؼجش ػ Ping ا زغخ رلز ا status code :1ؼ ظس ا

true ب

طؾؾخ who ا زغخ رلز status code :0ؼ ظس ا

Page 93: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 92 ال رغب

DIFFERENT TYPES OF TESTS

ض : parametersػ ػذح test command رلز ا

1. a string test

2. a numeric test

3. a file test

TESTING A STRING

commandsو ثزلز ز ا stringsػ ا checkثؼ test commandو ا

NAME="ahmed"

test $NAME = ahmed

echo $?

خعغذ ك اظسح ازب

form زؾون اؼظ ك ا conditionال ا 0اال ظشد form ا زغخ رلز ا

اضبخ

ا variable name : NAMEا بى غبكخ ث ا test commandعغذ اؼب ك ا

test condition : = اvariable value : ahmed

ك ري ػ ؽشن ا ال NAMEاعب variableاب ارا اسدب ا زؤذ عد

Page 94: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 93 ال رغب

، ارا اسدب ازبذ regular expressionؼبزب ى $هبذ ثزشعخ ا ”“الؽع ا ا

HIرغ variableعد

ثزا االع variableزا ؼ ػذ عد “ status 1 “كغذ ا ا

‟„ ticksث ػالز NAME$ ارا ػؼب

$ expansionرو ثبـبء ؿ ‟„ ticksػ اشؿ ا ا status = 0كغغذ ا ا

كق ري ?

variablesػ ا checkكب ال و ثؼ ‟„ ticksارا عذ ا test commandال ا

، ثبطجغ ا ال ticks stringػ اخ اعدح ث ا check و ثؼ

status = 0كزظش ا stringغذ اب

checkؽز و ثؼ variable nameؽ ا ”“زي غت اؾزس عذا ػغ ػالز ا

stringsظ ػ ا variablesػ ا

test "$NAME" = "hamish"

test "$NAME" = hamish

HAS A VARIABLE BEEN SET OR NOT ?

Page 95: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 94 ال رغب

ا ال اي variable valueعف زؤذ زا ا variableذب عد ا ثؼذ ا رؤ

Variable set or unsetزا ا

ري ز ثطشوخ ثغطخ ا غزخذ ض ز ا form ازؤذ

test "${NAME}x" = x

x{NAME}$كب زغخ ahmed (set)ب هخ variable : NAMEكبرا بذ ا

status = 1ا falseكزظجؼ ازغخ (x) ثبطجغ ال رغبي (ahmedx)رغبي

رغبي x{NAME}$كب زغخ (unset)ظ ب هخ variable : NAMEاب ارا بذ ا

(x) كوؾ رغبي اطشف اال ابدخ(x) كزظجؼ ازغخtrue اstatus = 0

stringوخ اخشي زؤذ ؽ ا بى اؼب ؽش

String test Meaning

-z Zero – length

-n Non zero – length

غبكبد اسدب ا زؤذ ارا بذ 5رز valueؾزي ػ variableكبرا ب ذب ا

ػ ؽ ا checkكو ثؼ non valueا ال valueؾزي ػ variableز ا

value ا ؽ اstring

Page 96: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 95 ال رغب

valueال ؾزي ػ اي variableؼذ اضب شح اخشي غ عؼ ا

كبى اضش اؼالبد از ”=“ض ػالخ test conditionعزطشم اال

ض string testاعزخذاب غ ا

String test condition Name

غبي =

ال غبي =! اه ا غبي => اجش ا غبي =< اه > اجش <

string testخبط ثب conditionsز ا

NUMERIC TESTS

رغزخذ numeric testؤر اال ا اؾذش ػ ا string testثؼذ ا رؾذصب ػ ا

ك ازؼب غ االسهب ، ض :

x=101

test "$x" -lt 10

echo ?$

كبرا ؼ ? ”lt-“ب الؽع عد اشض

numericكب ااع ا less thanؼ numeric test conditionؼزجش ا ”lt-“اشض

conditions

String test condition Name

-eq غبي -neq ال غبي

Page 97: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 96 ال رغب

-le اه ا غبي -ge اجش ا غبي

-lt اه -gt اجش

string test conditionsغزخذ ا stringd 2ارا كؼذ اوبسخ ث

numeric test conditionsكغزخذ ا valuesاب ػذ اوبسخ ث ا

FILE TEST

file directory، غزخذ زؤذ ب ارا ب زا ا صبش اخش ع عز ػ

غ options، ز ري ػ ؽشن اعزخذا ثؼغ ا hard linkا soft linkا normal fileا

testاالش

Option Type

-f File -d Directory -L , -h Symbolic link -s The size is greater than zero

عزل ثزا عؤخز ثؼغ االضخ: optionsبى اضش ا

Page 98: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 97 ال رغب

directoryخش ػ ؽشن ا ضب ا

اخشا ؤخز زا اضب

f, -L–ك ا status = 1زي كبذ ا current directory ا ”.“ا

d–ك 0ب رغبي

Page 99: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 98 ال رغب

LOGICAL OPERATORS

: One conditionلخ اعزخذا غ testوذ ازب اؾذش ػ ا

test if “x” is true then “y”

ض : conditionغ اضش test برا ارا اسدد ا اعزخذ ا

test if “x” OR “y” is true then “z”

ض logical operatorsك ز اؾبخ عغزخذ ب ؼشف ثب

NOT شض ب ثبشض“!”

AND شض ب ثبشض“-a”

OR شض ب ثبشض“-o”

كغف NOT ارا اعزخذب ب ؼ ا

!1=0

!0=1

”F“ثؾشف falseا ”T“ثؾشف trueسضب ANDارا اعزخذب

0 -a 0 = 0

0 -a 1 = 1

1 -a 0 = 1

1 -a 1 = 1

زي

T and T = T

T and F = F

F and T = F

F and F = F

”F“شف ثؾ falseا ”T“ثؾشف trueسضب ORارا اعزخذب

0 -o 0 = 0

Page 100: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 99 ال رغب

0 -o 1 = 0

1 -o 0 = 0

1 -o 1 = 1

زي

T or T = T

T or F = T

F or T = T

F or F = F

ؼ stringرؼ ا زا ا ”T” “-n“ا NAME” = ahmed true$“ب كب زغخ

“non zero length” زا ؼ اtrue ا“T” اؼب كز ازغخ

T OR T = T

ك اضب اضب

F OR T = T

ك اضب اضبش

Page 101: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 100 ال رغب

T AND T = T

ك اضب اشاثغ

F and T = F

ب : file ا زؤذ ع ا

test \( -f .bashrc \)

test \( ! -f .bashrc \)

CONDITIONS IN THE SHELL

، بى ػذح ؽشم scriptingؼب ك ا ا االشبء از غت ا ز conditionsا

conditionsزؼب غ ا

USING THE "IF" STATEMENT

: if statement? ا اثغؾ اشب ا if statementق زؼب غ

if condition

then

do some operations

fi

Page 102: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 101 ال رغب

ثـ: if statement ب شي ك اضب غت ا رجذأ ا

1- “if”

”then“ب ك اغطش اضب خ -2

ػغ commandsا ب ك اغطش اضبش -3 ا غبكز tabاز شذ رلزب )لؼ

(commandsهج ا

”fi“ثـ statementص و ثببء ا -4

THE "IF" "THEN" "ELSE" STATEMENT

ز رغؼ ب ثبػبكخ خبساد ا if statementػ ا featuresو اال ثبػبكخ ضذ ا

اضش

if condition

then

<condition was TRUE, do these actions>

else

<condition was FALSE, do these actions>

fi

ػ ؽشن test ػ اضش

if condition

then

<condition was TRUE, do these actions>

else

if condition

then

<condition was TRUE, do these actions>

else

<condition was FALSE, do these actions>

fi

fi

Page 103: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 102 ال رغب

THE "ELIF" STATEMENT

زي ك else thenاضش if statementب شي ك اضب االخش بى اضش

elifثـ else ifاد ض اعزجذا ػغ ثؼغ االخزظبس if statementو ثزجغؾ ش ا

if condition

then

<condition was TRUE, do these actions>

elif condition

then

<condition was TRUE, do these actions>

else

<condition was FALSE, do these actions>

fi

اؽذ كوؾ ”fi“ل elifغ ”fi“ثبعزخذا ز االخزظبساد ك ؾزبط ا عد

شاد ? else if 10ك عغزخذ conditions 10 برا ارا ب ذي

ثبطجغ ال كزا عئ ، كبرا اؾ ?

if statementثذ case statementاؾ اعزخذا

THE "CASE" STATEMENT

conditionsػ ا matcheingب رو ثؼ looptغذ case statementا

ثطشوخ اع

case expression in

pattern1 )

statements ;;

pattern2 )

statements ;;

* )

Page 104: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 103 ال رغب

statements ;;

...

esac

ث ث matchingص و ثؼ expand expressionثؼ caseؽش و ا

pattern

ا statementكو ثزلز ا matched patternكبرا عذ ا ;;ؽز ظ

statementص لز ا ”*“غ ا matcheكو ثؼ matched patternا غذ اي

statusاخشاط ا case statement و ثببء ػ ا ص

اع scriptز اطشوخ رغؼ ازؼب غ ا

DEBUGGING YOUR SCRIPTS

ؼ ، ك اؾووخ بى ػذح ؽشن debug scriptعزؾذس اال ػ لخ ػ

debugging : ض

scriptا ا عطش ك ا x–اػبكخ -1

#!/bin/bash -x

Page 105: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 104 ال رغب

ثز اطشوخ scriptرشـ ا -2

bash -x eatout.sh

عطش عطش ب شي ك اظسح script رلز ا ؽشوخبر اطشوز رغزخذب ك ػشع

ازؤذ عضء عضء غ ػذ االزوب عضء ا اخش اال ثؼذ scriptاطشوخ اضبضخ رلز ا

ض scriptا ا linesزغز ، ز ري ػ ؽشن اػبكخ ثؼغ ا

echo #?

echo "Hit any key to continue"...

Page 106: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 105 ال رغب

read

THE NULL COMMAND

ثزا اش scriptا ا ”:“ اػبكخ ا null commandا

if :

then

echo “....”

fi

Page 107: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 106 ال رغب

trueدائب ”:“ال ا form true دائب ز ا

THE || AND && COMMANDS

كغزطغ كؼ ري ثبعزخذا || && if statementارا ب جؾش ػ ؽشوخ خزظشح زلز ا

ق زا ?ك عطش اؽذ ثبعزخذا ، commandاؼذذ ا لز ازي غؼ ثز

ب الؽع الشم : commands 2و ثزلز ا

Command 1

grep italian restaurants.txt || echo "sorry no italians here"

Command 2

grep egypt restaurants.txt || echo “sorry no italians here”

Page 108: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 107 ال رغب

ثب ك اضب اضب ك italian recordالؽع ا ك اضب اال بذ ازغخ ا

sorry no italians“زي ظشد سعبخ restaurant.txt fileثذاخ ا Egypt recordبى

here”

? زاكبرا ؼ

ا زا غبي

if there are italians inside resturants.txt

then

return them

[else]

return the string “sorry no italians here”

كزا ؼ shellثبغجخ

if the result of the grep command is TRUE (0)

then

you will get the lines containing the word italian from

the file resturants.txt

if the result of the grep command is FALSE (1)

then

print 'sorry no italians here'

اضب commandاال ، كال رلز ا commandرؾون ا )ارا ORرؼ ||ارا كبؼالخ

اضب( commandاال لز ا command زؾون ا ارا

echo “Oh, you're looking for italian, here they are : “ && grep

italian restaurants.txt

Page 109: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 108 ال رغب

اال ؽز زو ا ا command)غت ا زؾون ا andارا كبؼالخ && رؼ

command )اضب

مثال:

اضب commandاال خطؤ ك لز ا commandب شي كب ا

مثال أخر :

اضب commandثزلز ا اال ر رلز ثش طؾؼ كوب commandب شي ا ا

مثال اخر :

اضب خطؤ ، كؼشع commandاال ثش طؾؼ ا commandب هب ثزلز ا

اال كوؾ commandزغخ ا

Page 110: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 109 ال رغب

CHAPTER 8. LOOPS

INTRODUCTION

shell اعزخذا غ ا loopsااع ا 3بى

1- for loops

2- while loops

3- until loops

ب عشي syntaxعؼ لظ ا

THE "FOR" LOOP

syntaxب زا ا for loopا

for variable in list

do

...

done

رو for loopؤ ا

for every element in the list (1,2,3,4)

Page 111: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 110 ال رغب

do something (echo $i in output)

ا commandsا variablesا –اعبء –سهب ا ر اي شئ )ا Listز ا

اي شئ اخش( ؼ ا fileؾزي

for loopال االسماء في مثال علي

for loopفي ال commandsمثال اخر علي استخدام ال

داخ ``ث commandsالؽع اب ػؼب ا ( )$ ػؼ

fileو ثؼشع ع ا fileاالش

Page 112: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 111 ال رغب

الؽع ك عطش file و ثؼشع ls –lظ command ls -1 (one)ا ا

الشم ك اظسح ازبخ

for loopفي ال filesمثال اخر علي استخدام محتويات ال

غ االػذاد for loopضب اخش ػ اعزخذا ا

Page 113: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 112 ال رغب

print numeric sequenceثؼ seqؽش و االش

scriptاعدح ثذاخ ا ”in“ش ثذ اعزخذا خ ضب اخ

Page 114: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 113 ال رغب

”--i“ثـ ”i=i-1“اعزجذا ”++i“ثـ ”i=i+1“ اعزجذا

Page 115: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 114 ال رغب

ك values عؼغ ز ا scriptاعدح ثذاخ ا ”in“غزطغ االعزـبء ػ خ

ب scriptا اخبسط ثغبت

و 1هز variable count ػغ for loop ز ؽشوخ اخشي ك اعزخذا ا

for loopثؾغبة ػذد اؼبد از هبذ ثب ا

form الؽع اب اعزخذب ا

$((count=count+1))

زي عزظش ازغخ ب shellال رلب ا form ز ا

Page 116: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 115 ال رغب

count اخبص ثضبدح هخ ا line 6 اظبش غذ ا زظ ثغجت ا errorارا ظشب ا ا

زهق ػذ ري ث هب countصبدح هخ ا form ثزلز ز ا shellؽش هب ا

5ا 4ا 3ا 2اع commandزي ك غذ commandثزلزب ؤب

ارا كب اؾ ?

بى ػذح ؽشم ؾ ز اشخ

ز بزب form اب ز ا ( ”:“ noop) ػغ الطريقة االولي

: $((count=count+1))

اخشي ض form ة form اعزجذا ز ا الطريقة الثانية

count=$((count+1))

Page 117: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 116 ال رغب

اخشي form ة form اعزجذا ز ا الطريقة الثالثة

count=`expr $count + 1`

WHILE AND UNTIL LOOPS

ب لظ until loopا while loopب ا loopsاال ػ ػ اخش ا عزؾذس

syntaxا

syntaxب while loopا

Page 118: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 117 ال رغب

while <condition is true>

do

Action

done

syntaxب until loopا

until <condition is true>

do

Action

done

ارا كب الشم ث

كوؾ status 0از ب true conditionsرو ثزلز ا while loopا

ا ا false conditionsعزو ثزلز ا until loopثب ا true conditionؽز رظ

مثال

i=5

while test "$i" -le 10

do

echo $i

done

Page 119: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 118 ال رغب

اعزجذا

while test "$i" -le 10

form ثب

while [ "$i" -le 10 ]

Page 120: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 119 ال رغب

مثال اخر

while [ !-d `ls` ]

do

echo "file"

done

directoriesغذ filesزؤذ ا ز ا command : lsو ثزلز ا scriptزا ا

fileص خشط خ

مثال اخر

ؽشم ا ال، كبى ػذح systemؼ هذ هب ثبذخ ػ ا userارا اسدب ا ؼشف ب ارا ب

ؼ ري

Userػ زا ا who commandو ثزلز scriptاال : و ثؼ

user=$1

while `who | grep "$user" > /dev/null`

do

echo "User logged in"

done

ؽز ذخ ا ”User not logged in“ازي و و ثؼشع عبخ Untilا ثبعزخذا

user ا اsystem ا كزهقscript ػ اؼ

user=$1

until `who | grep "$user" > /dev/null`

do

echo "User not logged in"

done

dev/null/زي ػؼب command outputب ال ب ا

زظش scriptعو ثؼشع بئ اشعبئ ، زي ا غؼ ا script زا ا

ب sleep 10شح اخشي ري ػ ؽشن اػبكخ checkهج ا ؼز ا صا 10ذح

Page 121: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 120 ال رغب

#!/bin/bash

user=$1

until who |grep "$user"> /dev/null

do

echo "User not logged in"

sleep 10

done

echo "Finally!! $user has entered the OS"

exit 0

THE BREAK AND CONTINUE COMMANDS

loopػ اؼ اصبء رلز غش ا scriptط اؽبب ا ا هق ا هذ ؾزب

زذاخخ ض : loopsجش ؾزي ػ scriptكضال ارا ب ذب

for

do

for

do

if

then

break;

fi

done

done

breakاذاخخ غت ا و ثغشب ػ ؽشن loopكخشط ا

ثؼذ سه ا breakب ػؼ لخ اعزخذا ا زا ض ك ػ عذ ك طلؾخ ازشذ و

column سه اrow زبثز ك اخبخ اخبطخ ثب ك اغذ

Page 122: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 121 ال رغب

/!#bin/bash

# Start by warning the browser that a table is starting

echo "<TABLE BORDER='1'>”

#Start the ROWs of the table (4 rows)

for row in `seq 4`

do

#Start the row for this iteration

echo "<TR>”

#Within each row, we need 3 columns (or table-data)

for col in `seq 3`

do

#If this row 2, then break out of this inner (column) loop,

returning to

if [ $row -eq 2 ]

then

break;

fi

#If this is NOT row 2, then put the cell in here.

echo " <TD>$row,$col</TD>”

done

#End this ROW

echo "</TR>”

done

#End this table.

echo "</TABLE>”

exit 0

Page 123: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 122 ال رغب

breakا 2ػ ؽشن اػبكخ اشه inner loops 2 غش

break 2

كب command : breakازي ؼزجش ػظ ا command : continueعزؾذس اال ػ ا

command : break و ثبخشط اloop ثب اcontinue و ثبؼدح ا ثذاخ ا

loop شح اخشي زغب ايcommand ؤر ثؼذ

ثؤي ش ض script outputاسدب ازؼب غ ا ارا أخش ب عزؾذس ػ ك زا اغضء

كب capital lettersا output small lettersا رؾ ا fileا outputاسعب ا

doneثؼذ خ scriptري ز ك اخش ا

redirectionؼ كغغزخذ ػالخ ا fileك loop outputكشػب اب شذ ؽلع ا كبرا

ب scriptك بخ ا

for ((i=0;i<10;i++))

do

echo "Number is now $i"

done > forloop.txt

Page 124: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 123 ال رغب

capital lettersا small lettersزي ارا اسدب ؾ ا

for ((i=0;i<10;i++))

do

echo "Number is now $i"

done |tr '[a-z]' '[A-Z]' > forloop.txt

GETOPTS USING ARGUMENTS AND PARAMETERS

ثؼذح اشب ض : scriptغزطغ ا لز ا

./eatout.sh -a -i -r <parameter>

./eatout.sh -ai -r <parameter>

./eatout.sh -air <parameter>

./eatout.sh -r <parameter> -ai

getting options from theرؼ getting options اخزظبس Getoptsا

command line ازؤذ ا اshell ا زؼب ثز غزطغparameters ثبء ػ

falseا trueخشط

? getoptsارا ق زؼب غ ا

Page 125: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 124 ال رغب

CHAPTER 9. USER INPUT TO A

SCRIPT

INTRODUCTION

interactive scriptاال عزؼ ق و ثؼ CLIؼ ا scriptغزطغ اال ػ

THE READ COMMAND

shells ،built-in shell commandغ عغ ااع ا read commandؼ ا

ا و ثؼ االر command built-in commandؼشكخ ب ارا ب ا

type command

كبرا زجب command : readؼد ا ا

read X Y

X,Yكغض ا اغطش ازب زظش زبثخ هخ enterػـطب

Page 126: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 125 ال رغب

كغ بزب scriptثذاخ ا commandارا ؽجوب زا ا

ازي عو Input رخجش ػ ع ا userؼشع سعبخ option : -pاػبكخ ا

ثبدخب ض :

ازي عزج Inputا زظش curserب ب شي كب ا

Page 127: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 126 ال رغب

ب ا ل files هشاءح ؾزبد ا read commandبى ظلخ اخشي بخ عذا

ا ا delimiterا ظ غبكخ( ”,“ ػالخ delimiter)لؼ

ؾزي ػ : restaurant.txt fileب غذ ا ا

ثبزشرت rating، دسعخ اطؼ tel، سه ازل place، ب typeع اطؼ

ك رـش زا ازشرت read commandشذ اعزخذا ا

tel، سه ازل place،ب rating، دسعخ اطؼ typeع اطؼ

read commandا ؾزي ػ ا scriptزي غزخذ زا ا

separator رػؼ ع ا IFS input field separatorالؽع ا ا ازي علظ

اخش fieldث

By default كب اIFS=$‟ \t\n‟ رؼspace اtab اnew line ب ك زا اضب

IFSهب ثزـش ا

STDINـ scriptك اخش ا fileالؽع اؼب اب ػؼب ا

Page 128: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 127 ال رغب

PRESENTING THE OUTPUT

THE ECHO COMMAND

echo commandبى ػ خزل ا

1- built in shell

2- external program /bin/echo

echo programاخبص ثب helpارا اعزؼب ثب

از رؼغ echo commandاز اػبكزب غ ا special charactersبى ثؼغ ا

ض : commandػ ا featuresضذ ا

\n النزول اليnew line

\t اػبكخTab \c شء ك عطش اؽذ طجغ

غؼ ب ثؼذب \r غؼ ب هجب \b Back space اخقغؼ ؽشف

special charactersو ثزبثخ ب ث االهاط ب ال و ثزشعخ ا echo ا

ثزشعخ ال و ثزبثزب ب ? ”\“ ، ارا كق عو

Page 129: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 128 ال رغب

ض echoاز رغزخذ غ ا optionsثؼغ ا اؾ ا بى

-e يسمح بترجمة الspecial characters

-n

اضخ :

”e-“ظشد ب ك اضب اضب ثؼذ اػبكخ ”n\“ك اضب اال ب شي ا ا

”n\“ثزشعخ ا shellهبذ ا

tabالػبكخ ”t\“زغخ ك لظ اغطش ا ك اظبس ا ”c\“زا ضب اؼب ػ اعزخذا ا

THE PRINTF COMMAND

دائب ب outputهب ثؼشع ثؼغ االضخ ، الؽظب ا ا echoثؼذ ا رؾذصب ػ ا

unformatted ، ك بى ؽشوخ ا غؼ ثب اoutput ? ظش ثش اكؼ

خظغ ز ا printf، ا formatting outputؼ printfؼ ، ك اعزخذا ا

form

printf(%[flags][width][.precision]type)

printfاثغؾ اشب ا

Page 130: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 129 ال رغب

printf(%type)

:از عز ؽجبػزب charactersا typeا

s Srting

d Decimal

o Octal

x Hexadecimal

u Unsigned integers

ثطضب ا ب commandعػؼ printfثبعزخذا ا ahmedجبػخ خ : عو

stringعو ثطجبػز

printf '%s' ahmed

الب خجش ا ahmedر ػشػب ثغاس خ promptالؽع ا ا ظ ك عطش غزو

command ا ؼشع اب شذnew line ثؼذ رلز اcommand ،خجش ا غت ا ؽش

printf ؼشع عطش عذذ ثؼذ رلز ا ثب شذ ؽشكب زي ارا اسدب اcommand ا اػبكخtab

كغت اػبكخ echoضب كؼب ك ؽبخ ا optionsا اي

\n النزول اليnew line

\t اػبكخTab \c طجغ شء ك عطش اؽذ

غؼ ب ثؼذب \r غؼ ب هجب

\b Back space غؼ ؽشف اخق

Page 131: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 130 ال رغب

:مثال اخر

:از اػبكزب زؾذذ دهخ اؼشع options اؽذ ا precitionثبػبكخ

printf „%.5d‟ 12

الؽع الشم ث ث اظسح digits 5 ثبعزخذا 12ؽجبػخ اشه printfب ؽجب

ازبخ

مثال اخر :

“-“ا ”+“ض flags ثبػبكخ ا

“-“ا ”+“ثبالشبسح اخبطخ ثب عاء بذ digitsػش ع ا shellرطت ا ”+“ا

مثال اخر :

الؽع اظسح ازبخ الشم ث االضخ اضالصخ :

Page 132: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 131 ال رغب

كوؾ ثذ اعزخذا اي typeثبعزخذا ا string: flyingك اضب اال هب ثؼشع ا

options

flyingخببد ؼشع خ 10ازي و ثزخظض size optionك اضب اضب هب ثبػبكخ

خببد 10ك اخش ا flying، كوب ثؼشع

خببد 10ك ثذاخ ا flyingؼشع “-“ flagك اضب اضبش هب ثبػبكخ ا

بال flyingكوب ثؼشع flyingاخظض ـ sizeك اضب اشاثغ هب ثزظـش ا

Page 133: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 132 ال رغب

CHAPTER 10. ADDITIONAL

INFORMATION

THE SHELL ENVIRONMENTAL VARIABLES PERTAINING TO

SCRIPTING

ازؼوخ ثب environment variablesا shellعو ظشح ػ ا

رؾذصب ػ environment variableا shell variableوذ رب عبثوب ػ الشم ث ا

subshellلخ اذخ اخشط ا subshellا

غزخذ االش environment variablesؼشكخ ا

Env

startupب ػذ ا setاز ز ػ environmental variablesز ثؼغ ا ا

Page 134: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 133 ال رغب

غزخذ االش shell variablesؼشكخ ا

set

shel variables ا ا

bash promptازب غ promptا

[root@localhost~]#

ازبخ form رؼزذ ػ ا promptؽشوخ زبثخ زا ا

”<“بهض commandاخش ظش ػذ زبثخ promptزب اؼب

PS2غ promptزا ا

Page 135: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 134 ال رغب

shell variablesؿشب PS1, PS2ز ا

”~“ ”-~“ض special environment variablesبى

home directoryا ا ”~“ؽش رشض

previous directoryا رشض ا “-~“ا

THE SOURCE COMMAND

ا commandsرلز ا scripts ؽشوخ زشـ ا source commandا

current shell ثذ كزؼnew sub shell

الؽع ا بى غبكخ ثؼذ ا scriptا ا commandهج ا “ .“ز ري ػ ؽشن ػغ

“.”

? scriptارا دػب ززش ق ز رشـ ا

ثبطشوخ ازبخ : scriptز رشـ ا

./shellscript

ؿبجب ب جذأ ثـ scriptك ال ا scriptلز ا new subshellثلزؼ shellكو ا

“#!/bin/bash”

اؼدح subshellثببء ػ ا shellكو ا exit 0ثـ scriptك ؼظ االؽا ز ا

parent shellا ا

? source commandارا كب ازي ؾذس ارا اعزخذب ا

Page 136: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 135 ال رغب

. ./shellscript

scriptثزلز ا current shellو ا

، اب ارا بى current shellكب ع ػ ا exit 0ز ثـ scriptب ا كبرا

exit 0 كب اcurrent shell عزظ لزؽخ

command، زي كب ا shebangو ثبـبء ػ ا sourcingزا ؾذس ال ا

exit 0زب ك ؽبخ عد ، ثب current shellرؼ ػ ا scriptاعدح ك ا

login shellزت ا ا current shellكبب خشط ا

? sourcingارا كب ظلخ ا

user ػ اsystem variables خبطخ ث ز ؽلظب كfiles

از رؼ ػذ ثذاخ رشـ ا startup scriptsػ غػخ ا systemؾزي ا

system

call filesثؼ scriptsرو ز ا systemػ ا loginثؼ Userػذب و ا

ب sourcingػ ؽشن ا user variablesاز رؾزي ػ ا

fileاخبطخ ث رعذ ك variablesلزشع ا ثببر ا ا ahmed gamilاع Userبى

varاع

ؾزي systemؼ ػذ ثذاخ رشـ ا vars.shغ startup scriptبى لزشع ا

ػ :

Page 137: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 136 ال رغب

variable file : varازي بدي ػ ا sourcing ا "var .”الؽع ب ا ا

ؾذس االر : scriptػذ رشـ زا ا

ؼ sourceاعزخذا ا اخش scriptداخ script ػغ

THE EXEC COMMAND

اخشي processة replace parent processو ثؼ exec commandا

commandكضال ارا هب ثزلز زا ا

exec ls -l

برا ? ، terminalهب ثبؿالم ا commandالؽع ا زا ا

execوب ثزلز ا رؼ ، ك processك اجذاخ ؾزي ػ اي terminalال ا

command ازي هب ثزلز اcommand : ls –l ثؼذ ري ؼد اparent process

login promptرـن ؼد ا ا shellزي كب ا parent process كؼب ال عذ اي

OTHER METHODS OF EXECUTING A SCRIPT OR A SERIES OF COMMANDS

ػ ظب :ج shellا

() round brackets ا -1

{} curly bracketsا -2

EXECUTION WITH ROUND BRACKETS

اعدح ث اوع commandثزلز ا shellرؼ ا و ا () round bracketsا

عذذ subshellك

كبرا لزب االش ازب

Page 138: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 137 ال رغب

Pwd

كب ازغخ عز

اغذذ path ث اوع كغو ثؼشع ا pwdرلز pathش ا ثب ارا هب ثزـ

زـش promptػ اشؿ ا ا

اعدح ث االهاط ك commands زـش ر رلز ا shellزا ؼ ا ا

subshell ص ؼد شح اخشي ا ا عذذحcurrent shell

EXECUTION WITH CURLY BRACKETS

اعدح ث اوع commandثزلز ا shellرؼ ا و ا {} curly bracketsا

عذذ subshellظ ك shellك لظ ا

مثال :

كبى tmp/error/ا error messagesرو ثبسعب ا commandsارا ب ذب ثؼغ ا

ؽشوز ؼ ري :

:الطريقة االولي

Command1 2>/tmp/errors

command2 2>/tmp/errors

command3 2>/tmp/errors

الطريقة الثانية :

{command1;command2;command3;} 2>/tmp/errors

Page 139: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 138 ال رغب

”;“اخش ػالخ commsndغت ا زجغ

pwdثؼذ ”;“ؼذ عد errorعزظ ػب form كز ا

{ls -l; pwd}

اظؾؾخ form ا

{ls -l; pwd;}

Page 140: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 139 ال رغب

CHAPTER 11. POSITIONAL

PARAMETERS &VARIABLES RE-

VISITED

INTRODUCTION

ػشكب ظلخ اؽذ ك ?$ ,#$ ,9$ : 0$ض parametersرب عبثوب ػ ثؼغ ا

ثش parametersعزؾذس اال ػ ثؼغ اطشم از ا زؼب ثب غ ا

خزق

value : ahmedؾزي ػ parameter : NAMEلزشع ضال ا ذب

NAME=ahmed

( set)اي ؽذس ب valueاطجؼ ب parametersزؤذ ا ا

parametersؤر اال ا ا ؽشوخ زؼب غ ا

PARAM:-VALUE

رؼ ال رؼغ اي ) nset: null or u parameterا زؤذ ا form رغزخذ ز ا

variable رؤذ كوؾ عدب )

commandزغخ زا ا valueكغزظش ا setا parameter valueكبرا ب ا

اعدح ك ا valueكغظش ا unsetا valueظ ب parameterاب ارا بذ ا

form

${PARAM:-value}

ahmed ثوخ set، هب ثؼ NAMEاغبثن parameterكضال ك ؽبخ ا

Page 141: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 140 ال رغب

NAME=ahmed

كبرا هب ثؼ

echo ${NAME:-'AMR'}

NAMEال AMRظ ahmedعظش ب ثزي هذ رؤذب اعبعب valueخظظ

ب valueب variable ا ا خظظ

ز االش شح اخشياػذب رل unset NAME ارا هب ثؼ

خظظخ ، زا ال ؼ ثبزؤذ valueظ ب variableزا ذ ػ ا ا AMRب ظش

AMRعذذح valueاطجؼ ب variableا ز ا

ػ اشؿ اب لزب ػب ا unsetبصاذ NAMEب شي ك اظسح اغبثوخ ا ا

form ذ ػ ا ز ااغبثوخ ، زا form رغزخذ زؤذ كوؾ عدvariable

value

Page 142: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 141 ال رغب

PARAM:=VALUE

? set parameterارا كق و ثؼ

شج عذا ثبغبثوخ form و ثبعزخذا

PARAM:=value

ذح اغذ valueكؼغ ا parameter : (unset or null)رؼ ا ارا بذ ا form ز ا

كوؾ ا ر عدح( value)رؼ ػغ ا اوذخ valueاال كبرشى ا

ا ظ ب unsetك ؽبخ اب variablesا ا set parameters رو ثؼ

value ب ثبظسح ازبخ

ا unsetا setزشي Variableػ ا checkرو ثؼ form ارا كز ا

value ا ال

كزو valueا ظ ب unsetكززشب ب ، اب ارا بذ valueا ب setكبرا بذ

اغذذح valueثبػبكخ ا

${PARAM:+VALUE}

كبرشب ب اال parameter : (unset or null)رؼ ا ارا بذ ا form ز ا

كوؾ ا عذد( value)رؼ اعزجذ ا اغذذح value اعدح ثب valueكبعزجذ ا

Page 143: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 142 ال رغب

form ا ا، رزـش value : amr الؽع اخش عطش ك اظسح ازي ػؼ ا ا

ز ا valueا setبذ كوؾ زؤذ اؼب عد ”{NAME:+ahmed}$“اغبثوخ

parameter

${VARIABLE%PATTERN}

بز رو ثؾزك match patern (variable value)ثؼ رو formز ا

لغب ( valueػشع اجبه ) زا ال ئصش اؼب ػ ا

match the shortest pattern from the end of it رؼ ”%“ا

الؽع اضب ازب :

formك ز ا wild cards اعزخذا ا

Page 144: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 143 ال رغب

characters ثؼغ ؽزك ب ث ”a” “l“هب ثبجؾش ػ اهشة ب

رو ثؼ formكب ا valueشس اضش شح ك ا paternالؽع ا ارا ب زا ا

match غ اهشةpatern

MAGIC%%R*A

matching longestرو ثؼ formاغبثوخ غ الشم ا ز ا form ض ا

pattern ظ اshortest ب ك اform اغبثوخ

اظش اضب ازب :

اعدح ث charactersػ ثؼغ ؽزك ؽذس ؼ ا ”h” “d“اثؼذ ب هب ثبجؾش

VARIABLE#PATTERN

Page 145: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 144 ال رغب

ك عذ، value بخ ا matchingاغبثوخ ا ا formsالؽع ك ا

matching ثذاخ ا value ?

ثبعزخذا اظـخ value ثذاخ ا Matchingاالعبثخ ؼ ، ػ ا

variable#pattern

matching shortest patternب رو ثؼ ”#“ا كب ”%%“ا ”%“ض ا

بثوخ(ع formاعدح ك ”%“)ػظ value ثذاخ ا paternاهشة

ؽزكب ؽزف ب value ثؼغ ثذاخ ا ”a” “m“ الهشة Matchingب هب ثؼ

charactersث ا

ػ ثذاخ ا pattern اثؼذ Matching longest patternرو ثؼ ”##“ا

value

ؽزكب ؽزف ب valueػ ثؼغ ثذاخ ا ”a” “m“الثؼذ Matchingب هب ثؼ

charactersث ا

بخ ا ”%“ا value ثذاخ ا matchingرو ثؼ ”#“ارا ق ززش ا ا

value ?

، دائب ب ر عدح ك scriptsك ا commentرغزخذ ؼالخ ”#“ب ؼ ا ا

رغزخذ دائب ك اجذاخ ”#“ا ، زي كب commentثذاخ اغطش اشاد ػ

رغزخذ دائب ك بخ اؼخ اؾغبثخ ؾظ ػ ا ”%“ب ؼ ا ػالخ ا

percentage زي ك رغزخذ دائب ك ابخ ،

”#“ا longestرذ ػ ا ”%%“ا ا shortestرذ ػ ا ”%“ارا ق ززش ا ا

اؼب زي ?

Page 146: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 145 ال رغب

shortest غشد ػالخ اؽذح ك رذ ػ ا ”%“ا ززش ري ارا اػزجشب ا ا

longestػالز اي اب اجش كجزي رذ ػ ا ”%%“ثب

VARIABLE:OFFSET:LENGTH

ال variable اي عضء ك ا charactersؼذد ؾذد ا cutرو ثؼ formز ا

ا بز variable ثذاخ ا شزشؽ ا

ؽزف اجبه ض : variableك رؾذذ اي عضء ا variable=ahmedكبرا ب ا

ahm اme اhme

رؼ االر : formز ا

1- Variable ا :variable از عؼ ػب ضahmed

2- Offset ػذد ا :characters ( ثذاخ ا از عزغبب variable زا )

ػ ؽشك كجزي ”ah“غت ا زغب ؽشك ”me“ؼ اب ارا اسدب اؾظ

offset = 2ا

3- Length ػذد ا :characters از عغزخشعب اvariable كبرا اسدب

length=2كب ا ”me“اعزخشاط ؽشك

كب اظـخ عز بزب : variable : ahmedا ”med“كبرا اسدب اعزخشاط ؽشف

variable:OFFSET:LENGTH

ahmed:1:3

#VARIABLE

valueاز رز ب ا charactersثؼشع ػذد ا formثبخزظبس رو ز ا

Page 147: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 146 ال رغب

RE-ASSIGNING PARAMETERS WITH SET

، ب عغزخذ shell variablesازي غزخذ ك ػشع ا setرؾذصب عبثوب ػ االش

اال ثش اخش

assign parameters ا غزخذ ك ػ set commandكب

ػشػ parameters 3ازب ثوشاء scriptو ا

ر ازغخ ب

ب scriptك ا set برا ارا ػؼب

Page 148: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 147 ال رغب

كغزظجؼ ازغخ بزب

ؽذس ?كبرا

ثبػبدح رخظض set، هبذ one, two, threeا 3$ ,2$ ,1$ثزخظض scriptثؼذ ا هب ا

a, b, cا 3$ ,2$ ,1$ا

EXPLAINING THE DEFAULT FIELD SEPARATOR FIELD - IFS

اخبطخ ثب delimiter ا IFS: input field separatorرؾذصب اؼب ك اغبثن ػ ا

cut command

رـشب ػ ؽشن (space and tab and new line)زا ؼ

IFS=”,”

ؼ كوؾ ري ػ ؽشن scriptػذ ازؼب غ value رـش ز ا

(IFS=',';script.sh)

كوؾ subshellزا ذ ػ ا زا ازـش عز ك ()الؽع عد هع غزذش

االطخ IFS رزـش ا اشئغ shellظ ك ا

SETTING VARIABLES AS "READONLY"

ثؾش readonlyعؼب variableعزؾذس ػ لخ رؾظ ا chapterاخشا ك زا ا

ب killا هذ ثؼ shellال رـشب اال ارا اؿوذ ا

ز ري ػ ؽشن ز اظـخ:

Page 149: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 148 ال رغب

Readonly NAME=ahmed

ب unsetاخبطخ ثب ا ؽز ػ value رـش ا variable NAMEثزي كب ا

Page 150: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 149 ال رغب

CHAPTER 12. BITS AND PIECES –

TYING UP THE LOOSE ENDS

THE EVAL COMMAND

”|“ pipeاعزخذب ا commandارا هب ثزلز زا ا

ls |wc –l

بزب ? variableثؾش اعزجذا ثب variableك ”|“ػغ ا

PIPE=‟|‟

ls $PIPE wc -l

االعبثخ ال ، اظسح ازبخ رػؼ ري

ثذ ا ls commandاال ص رو ثزلز ا variable expansionرو ثؼ sellال ا

، wcاخش غ ‟|„غ fileبجؾش ػ و ث ص variableؼذ هشاءح اوخ اغذذح

error : No such file or directoryزي ظش ا

ثؼذ اعزجذا ا command lineك و ثبػبدح هشاء ا eval commandب رؤر ظلخ ا

variable

Page 151: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 150 ال رغب

مثال اخر :

ظسح ازبخ اب ػذب لزب ا ، الؽع ك ا اؼب pipeهب ثزشعخ ا evalالؽع ا ا

command ثذecho ظشerror

eval ػذب ػؼب

مثال اخر :

x=100 ptr=xارا ب ذب

100ظ xكغغذب عزغبي echoثبعزخذا ا ptrكبرا ػشػب هخ ا

Page 152: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 151 ال رغب

غت ا ؼغ ازغخ ػ اش ازب ptr=100 ؼشع ا

eval echo $x

formك ا xا ثؼ اخش عو ثبزؼغ ػ

eval echo $$ptr

ب عغذ ا ازغخ ظشد بزب

PIDػ اب ا $$ echoهب ثزشعخ ا $$عالؽع اب ػذب ػؼب ، ارا كغت ا لظ

{}اال اضبخ ثبعزخذا $ث ا

eval echo ${$ptr}

كال رغزطغ special characterػ اب {}اعدح خبسط ا $رل ا shellكب ا ب

رؼ form، ال ز ا special characters 2رؾزي ػ formا رزشع echoا

${$ptr} ال رؼ${x}

$هج ا ”\“زي غت ػغ

eval echo \${$ptr}

Page 153: Bash scripting for beginner and intermediate

طبؼ اذػـــــــــــــــــبء Translated By:AHMED GAMIL 152 ال رغب

RUNNING COMMANDS IN THE BACKGROUND USING &

ك commandك رلز اضش linux multi-task operating systemال ا

لظ اهذ

از ز رلزب رؼ ك اخلخ process ز ري غت ا غؼ ا

ػ اغذذح processعذذ لز ا curserغزطغ ا ؾظ

اال بزب commandثغبت ا ”&“شض ز ري ػ ؽشن اػبكخ ا

find / -name "ahmed" &

processاخبص ثب PIDالؽع ظس ا

formارا اسدب ا زؤذ ب رلز ز ا

echo $!