You are on page 1of 23
Gia + shall Scripting: * a3oalag: \ | sh — bern shell bash — born again shell #$ cat [ete shells $ wheh bash * example 4) [bin] bash echo “Hello world” Schmod +x hellosh $3 «hello.sh Helo World + Vorables: Two types of Vartobles : System Verviables User defined Vavtobles $ echo $BASH [bin] bosh + Sysiem aviobles’ $ BASH ) SHome » SPwo 5» Cee + User Detined Voviables: 4, Name = Mark a. echo Snome 3. ech) The nome is $Mome, Scanned with CamiScanner Ye ..—r pe Toad comemond § 2 Taking the lp from user. 4. Ae Jbta| bash. a etho “enter name 3. lead =TName echo “Entend name + gnome,” $ )hello.sh Enter name: Max Eriered name: Mox > 1 ead name Tamed & echo “Nomes: gnanret, Snamog" + —> option jo emilee Fhe lp in Same line 4. HH [bin [bash & read -P ‘username » ' vser—Var 3. echo “username: Suger—VaOr 5 .Jholl.sh usumome : Max Username: Mox + [ES] alent (Dont display the ‘Ip while typing) + Ea] > omay 1.441 [btn | bash & echo "Enter names :” 8. head -a nanos —— —_ ~— scanned with Camstanner | 4 echo “Noxnes ! SLs §, § (amestiy}> + Def yoriable: 4. echo “Enter name" & veod 3. echo "nome : SREPLY" pasting arguments, 4, Bash- spt: L echo So $f $2 $3 ‘> echo $0 $4 9293' $ .|hello.sh Max Torn “Sohn. Jheilo.sh Max Tom John > echo $0 of $2 $3 @) dQ. args = ("30") & echo SKorgsle]} gtargstl} Scaraslal} $ hello. sh Max Tom John Mark Tom John _ (aw the agumends will bu pine) > echo $4 (No. of arguments) we Tp Stodoment then (0 4 ((Gndition)) then Statement fi canned wi ~amocanner eS llUlUrtrtrtt~™ 4 Toleger Comparision s eq , —ve, “jy Ger AES vho + Shing comparision: -z 3 Shing 1S null -e,has Zero length. # example: 4. 4) bin! bash & cunt =10 3. ff [$count -ey 10) + then 5 echo “condttton to tue" 6 & * In trrlegor compariston’s uy —ne —gt ' - ge eO J it -he Van ' WC) vV N 4 Tn Shing comparistons =| wy SS HCH “Zz Shing fs mull Scanned with CamS¢anner = a word = 0 w CC S worl ==" 'b" J thon echo “True” else echo “False” 4 S .|hallo.sh False a DAD MW eb be fa Word =a a EES word =="b"T] 3 then 4 o “Bb fs Tet 5. alt areas a"y) 6. then 4 echo “a %% Tye" 8. ase 3. echo “False” In. ¥ $ -[helhy.sh a §5 Thw Scanned with CamScanner fed] Ee] E3) 3 7 heck amply ov nob & &] & ly Teak Operators: + Fle Loe {bin} bash a echo -0 “enter the name of tle: \c" 3. ead filename & Wh [-o Séile-mme] 5 then 6. echo" $tle-name, -found." +t Oso 8 an "$filename net found" 3H $ touch test $ -Thello sh Enter the name of dle: test test found [Ee] = Te chee whether the fle exist wv nob EF] — Reular file — Te chock for divectoies — For Bluk Spectal file Cyideo, Traye,. Binary) — for chavocler spectal fle —5 Read permission —> Watte — purmssion — Extuute Permiasion Scanned with CamScanner eee eee Ht Aya 40. append Culpu fo the and ob tek on mon po pe lol te{ [bin] bash ocho -e “Enter tho name ob the files \c” reod file name rh C-£ $4lename J an. WC -w $filename] then. echo "Type Some text dota... TO quit press chyl+d" cat yy $#le—name alse echo “The Gile donk have write permissions K else echo “Sfilemane no exists! % $ ./hello.sh Enter the nwre of the Hilo: test Type Some oxt dota... To quit press chdtd. Helo world |! This is Moorty Bhat § ca tect Hello World |II This ig Moorty Bhat | Scanned with CamScanner + Logtcol- "AND! Opovador: 4 ape = a5 aH ('gage" gt 18] #9 C° Sage" -Lt 30] () a th (C'gage" -ett8 44 Sage" -Mt 30] @ & HL "Sage" gt 1@ -a “Sage” - Lt 20] + Logital ‘on’ operators te (eee gina ee) (ov) wee on J @) it [ ~o | [ua fiithmetio operations: Ltt Jbin| bash 4 Numt =60 3. TMMA= 20 4. ocho $(( num! + NUMA )) & -|halio.sh 10 (or) echo $(expr $num + gnumg) + Noles for mulhplication: echo ¢ (expr ¢mumL \#dnuma) scanned with CamScanner + Floating Point Math operations’ —» baste calculator 1. Numi = 20.5 a numas 5 3. echo "20.5+ 5" | be *Note® br pivighon: echo "sealo=a; a0-5]/5° | be # echo "Ss nume+ § nun | be } 3 Math Kbvey echo "Scole =3 a(S) [be -L # Case statermentt case exprossion i foctlexn ) Stotemrents 35 pationa) Statements 33 canned wi ~amocanner 4. 4 3 4 5. 6 t & 3. 40. Mt 4a 43 \t, 1 Vehtde= $4 a cas svehide fn 3 “car") 4 ocho “Rent Dy the vehicle fe Spo dolor" 5; I “\on" s ae “pont of the Svebicle ts 350 dollar's; echo ‘Unknown Vertole" 55 $ «[hello.sh cor Rent of the car fs S00 dollar. echo -e “Enter some chervaclers \o" read Value cosy $value tr {o-z] ) echo “Bewun oa to 2" 35 (a-z]) echo “Between & t0. 25 {o-1) echo “Between 0 to g"5; ?) echo" Bpecial chavacker "3 *) echo “unknown Inpurk” 3; esac canned wi amocanner SLANG = 6 The “LANG Grotvonment Vortable indicates the, languap, “o" ig the language sebking # Array Vartables: * 08 = Cubuntu' ‘windows’ 'kali’) => echo “$€05(@]}" C Ubuntu windows — Kali > echo “$hosfoJf" Ubunte = cs “3! os(@]}" | nd Value ola — cecho "$440S 101} 2 pnath oban 3 j $ fe) = 0s [3] = ‘mac! \ Adding an Kal? mae ¢ care “Kos Coy" olmrent Ubunty Windows — unset os [a] Removing ay ci eto | a ubuntw Windows mac f shing= dasfodscetgh echo “$4 shing Lo] }" dostallbeefgh, Scanned wi ‘”amScanner ea ——tsSG " > echo “St shinglod } dastadbedebgh. = co "8 § shing (1) }" \ nothing is pranted > echo “SLaeshing LO] Y" | Length 4 # While loops eed Towhle ( condition] 4 lbinlbash while loops Met while Con -le 10] 10 echo "sn" m= $(( net )) me, Stabe to BE Scanned with CamScanner & Shep g open. connrmnands } — 40 MeL a while (Cn < 10)) 3. do ‘ 3 { echo on" ay? 3. done (prints 4 to 9 with o delay of 1 eocond.) # Baad & fle content in ash’ while tread Pp do echo $P done < dile_name (or) do echo Sine done < filename, canned wii ‘~amScanner AL MeL & unitl [$n Be Lo] 3-do 4 echo én 5 N= S(O nt) 6. dong (prints 1 to 3) * FOR loops for VAREABLE tn L43...N do command 4 conpmand & comm and N done. (or) Scanned with CamScanner dor VAREABLE fh filer Hien 4iled...filen command. on SvAntABue command & command Nn done (od) for OUTPUT In $ (Linux-or- Unix command) command of gouTPUT Cornmanda on SOUTPUT cornimand N dono @ for (( ExPL 3 EXP 5 EXPS)) do command L cornmandL wommands done | exornplad 4 for tin Lrases to vino at 4 done (prints wm 1 to S) P dor tin dt. 104 do echo ot done bp dy Coty Cbdoa} do echo St done, (ints from 1 to 10 with Icreament- of a) (3579 ® for (( 205 145 5 ttt)) do echo 6f done * dor command in Le pwd date, > The olp will be, ; $ .[hello.sh ——-—---ls——-— —-—— + Desktop Downloads \idivs Douwnents Music pwd — — (4509) | CEST Q0a3 Scanned with CamScanner ® printing all divectovies in resend Wa for ttem tn 4 x do 3 W [-d dite] 4 then = echo $item ©. * select loop: Geect vor-nome in tot do command 4 Command command N done +h select name i Mork Tihn Tom Ben a do 3. echo "Srume solected" 4 done 3H hetlo.sh 1) Mork Q) Tohn 3) Tom 4%) Ben 42 2 Zohn Selected (ctrltc to out) a Scanned with CamScanner e 4 Seleck nome tm Mark John a do bs case gnamy tn 4 Mark ) S echo "Mark soected” 55 c ohn) t echo “John Ssduced "5; 8 £) a echo "Error" 1D. esac 11, done $ «| hello. sh. 4) Mark 4) Tohy HH? a Tohn | wet | Mork | | (cite to ouk from #2) + Break Y Continue: for (C ted 5 ide lo 5 HH) do ~ [st -et 5] (prints 4 to 5) canned wii ~amScanner + tor (C tet 5 ito y t+) do # ($i -e4 3 +0 9b ey 6 J continue K echo "Si" done (prints tS wierd 10) ® Functtons’ » functton Hello O + funcion print O L ho £ echo "Hello" echo $1 $4 $3 uit () a 7 print Hello Wild Again exit & -|hoo.sh _ Hello “ello wed Age echo “Hil™ oe oun quit ~S J helbnsh { Hello Hii Scanned with CamScanner | a # Local, Abortables: p function print 0 Nome = $4 echo “Thy Name 1 nome” 3 Tose = "Tom" echo “The name ts dname : Befre" | print Max _secho “The Name te SNome :-Aten” $ . | hello.sh The Name fe “Tor : Bere The nome te Max The Name %s Max 7 Atter 4% — tundion printO ¢ PRRRRG local Name = 44 a echo “The nane ts $name" .| hello.sh. The name 1s Thm :Beleve (wax The Nome so Max tol) The nome Ss Tom ‘Aer - Scanned with CamScanner 4 examples usage () echo "You need -fo Provide an Prgumnent” ocho "usage + go file_nasme * 3 is -File_exist O 4 al tile = "$1" CC -f “Sétle” J] gy return o Il yeturn 5 (LS -e¢ OJ] ey Usage iB C ts-Allo_exist "o1") then echo “File Found” else echo "Ft not Found" ti $ + hello. sh You need to provide an fagument : usage: -[hele. sh tileame File not -found S$ .[hollo.sh filet bt File found | — | canned with CamScanner Ee = = = =——etetésSS + feat. fonnen cornmandt Var = 34 readonly Vor Vor = 50 echo "Var =) ¢var" . [hello.sh. Var =} SL tine 3: var : readonly ‘Vovtable (warning!) Q2 PFO Pe +4] 5 To rake a function ‘readonly heno t echo “Halo Wevld" aaop gd yeodonly halto hello 4 echo “Hallo World fgaih” t g «[ hello.ch, Hello World line 6: Warning Sent > Geant With CaS canned wi ~amscanner * Signals g “Tays: cite —+ Tntenupt signal chl+z —» 1 stop the senpk trop “ocho Ext Signal 5 Delected “ st@INT (Preston —--—-— =) § -}hello.sh Coulgwh— - —) MC Exit signal $8 Detected * How to Debug a bash sont? bosh -x -/hell.sh St x Bh +x 5 Insidy tho code Scanned with CamScanner

You might also like