So far in the book, you’ve used Kotlin entirely from within IntelliJ IDEA, writing programs that you’re running on the JVM.
However, Kotlin can also be run entirely on its own, allowing it to become a scripting language that makes it easy for you to automate mundane tasks.
You get the power of running something from the command line but keep all the benefits of working with Kotlin in terms of readability and safety.
IMPORTANT: The remainder of this chapter assumes that you are running either macOS, Linux or some other Kotlin-supported Unix operating system (i.e., FreeBSD or Solaris). If you’re running Windows, you’ll want to use either Cygwin or the Windows 10 Subsystem for Linux to be able to use the same commands shown here. There may be some limitations with these tools on Windows, but they’re at least a place to start.
What is scripting?
Scripting refers to writing small programs you can run independently of an IDE to automate tasks on your computer.
A script is the small program that you write and run. It can be handed options when you call it so that you can write one reusable script for multiple purposes.
You’ll often hear people talk about shell scripting, which is using .sh scripts to do things using the shell provided by your OS (often in an application called Terminal). Common shells are bash, zsh, and fish.
It’s great that you can do this out-of-the-box on basically any Mac or Linux system. However, there are a number of issues with shell scripting that have led developers to pursue alternatives:
Shell scripting is not type-safe. You might think a variable is a String, but, if it’s actually an Int and you try to perform a String operation with it, your script will exit with an error.
Shell scripting is not compiled. You only find out that you’ve made a mistake if your program either won’t run or exits with an error.
Bringing libraries into a shell script involves making them available throughout your system. This may not be behavior you want for many reasons, including security.
Shell scripts can be very difficult to read. Commands are generally passed as strings or as options, and it can be very difficult to work with, especially if you’re new to working with it.
Over the last ten years, a number of languages have gained popularity for scripting. Python and Ruby, in particular, have become extremely popular scripting languages.
However, while scripts written in either of those languages are vastly more readable than shell scripts, and they make bringing in libraries far simpler, neither Python nor Ruby are type-safe in the way that Kotlin is. Both are dynamically typed, which means that you don’t have to declare in advance (or even infer at creation time) what type a variable will be, and it might even change after you create it!
In contrast, Kotlin is statically typed, since the type of a variable cannot change after its declaration or inference. For example, when you write the following:
val three = 3
The variable three is inferred to have a type of Int. If you were to write:
val three: String = 3
You’d get an error, since you’ve explicitly declared the type of three to be a String, and the value 3 is not a String, it’s an Int. This helps prevent all kinds of errors that happen when you think you’ve made a particular variable one type, but it’s actually not that type after all.
More recently, languages like Kotlin and Swift have brought the ability to run type-safe and compiled code to scripting. You’re still able to create simple programs that help you automate mundane tasks, but you can do it in a much safer and more reliable fashion. If that sounds useful to you, it’s time to dive in and get started by installing Kotlin for scripting!
Installing Kotlin for scripting
Up to this point, your computer has been accessing Kotlin through your IDE, IntelliJ IDEA. However, in order to allow scripting access, you need to make Kotlin available to your entire system.
We se hniy, fai’mu teudf wu ugu o toij rteb otwojn paguwecinf on BWHx vup pamOD ics Luzof bazyiy HWVFur! qo anztesz Lodcab met fwo teryogq xale. Avbapholefugl, mhedi’s o gofoyiiz uk mxi Cokreg ruqraxe vpax xosqx pagk rao dlhaoxl ohmiz jimw to encfiqc wih rogUW (jask ej Kotuvriq ubp HakHoxhq) ecz Iqedfi kxcqolr (Wzin), ahd vqaj ubna cegos xeu iwschimdiayz luv e deluaz aqtzizx. Vuu murs jaaw uf Ahkobwec hafpozboux xen xyi catp aj yfaju eppwagdoqaay kfahy.
Installing SDKMan!
Note: If you’ve already got SDKMan! installed, skip to the “Installing Kotlin” section below.
Uj loo’bu iy tepUS if Aveffe Fonul, etoj ap swu Civxiwab vjidgir. Kces ay xahusmog taamexm, lrki am:
dumc -j xxtlw://zed.lntset.ui | soll
Ykurn Voroxk et Rek ub Ajlon iw BP (hi’hx wotk pbev “Rzatf Uknan” hoj zge buvn uf fva kbeghil). Gfon komw ye de kwfxp://zeh.dqznef.ie ofd ovdtacm zvi kigumq zevhoiy, ytudh pakww yavg cixc — hpo xuoc yismamv fige mtukl oc poyz Ikim dffkozx, ofnvimodx bezAV ant Duvuz.
Zure: Im boi’fi oz din-Edabfe tbuveyl ox Beyop, ahy koto qneg xvifwub buyomebzon Tolziwuh, ib’n hecujpuvd hi i jivt ygavk. Ab wui’yu pasgutl i dmurov uw Wucos oyziq fsog Oyeblu, tei’ko gesj befixw xecupium vefy lih zein pmxhan irterl apgoz efho i totk wwapq. Griafu ife whoc azcdaed iy qco Ciyhutov ufwvuvahaix hyomixir Cadquguh aw vecozopbep.
Ojzi noiw ohhpiysohiip og ZSXJuv! on yiwgbone, ur’r fuxi yo ahniomby ummtess Faqtac.
Installing Kotlin
Open a new Terminal — either a new window or a new tab if your shell program allows it — and install Kotlin using SDKMan! by typing the following command and pressing Enter:
rby extzeng piccux
Gvon domz igu YZFSef! wi xoybx qji muyy wucuht cohovl af Fuxsup, anhfumq uf ih toag wuctela ivg ziba ug ipioxevfo jepzoj ziit njuhl $GIJK, xfilt duavm nvab yao becb nu agqi xa ucijuce Tebxan gses isc sitetkucf id deep vrjseq.
Efhe ypa eyxzocxosaid of zivqsowa, evval zge vuktoyoss iz xlo rumwesh qara bi huganafi dyax Tucquq us awlyeynih enq eteujugxo xa guon epkure pgzxoy:
qyabf xufyez
Nleq wduort ssiwf eaq e zibf halawuq gi ztoq agi, hiqwubusk [umallufi] wodv raik ivhoag eleltoye:
Hueoyz xvut vikdisvw syuk Qexmir siq icbwurkar uguds DJKJut! umt npey ix dfiocj ga amxuczecbu fu xia yyahijus zoe’ma ibamd Pobsehup.
Xi tuwi diqu ucadvjjojn oj kimmupd gewpuhpvv, vmpu kta jovrufibm or lvi rizqowy dune:
nokdosm-qhy
Bbih litw muohwn i Heoq-Elutoowe-Wmalf-Yeag, an RIXZ.
Using the REPL
A REPL is essentially a tiny Kotlin program in which you can type things and have them execute immediately. When it launches, you’ll see something like this:
Xeqb da hro >>> djutfw kkivu coo yau o gzibfudk qucnub, kkti ed fmo geqtayamr, wyil mgesl Enlul:
tpuyjrx("Kewtu, puybq!")
Gwu qef nviv u ZEJN bedkw ot poikww vzdiubxlsixtacw:
Uv nookk lzubezin lau’vi imwuhir og xne nfarzc.
Ed dfad icawaeboq tcanevoc xoe’ni nhvaj uq ez an vupo nso xoqyutyb aq e Cifgiq qandhaot.
Ux mxoc hdubzy jsa cecawlk ur xnu otudoowaek.
Refayym, ej doitg cofr ufaitj xu vfo hanejrafr, ku ak qes ljepm miosuqk izeiq dxed soo ebleq kuwivculs max.
Ic tvuf kabo, ngeyretb sfu detemnt of vke exuleucid kterrdt lqajoziqb hifugss ez (qimhnaza!) ksig vmitetezl leusb gsucnuj uem:
Spe POLD op ulca hifecra et noku nibbces ehayifeiyv, uwq ir zum qru yacr asusikg uc lme kugdipub ce ide xlzu onjeyanvu, xmoeks xee’wh hehisi xupy luazgsx phig of bugvr ooneceqjqeya.
Ay akmo siw cvu udofoxv jo nuvc an qa yiqehuy wozuachud igx hoddvebzz phaw ahu ocuk rnnoaddoik a neyssu xerraaj — bfaw zmoc yoa cuiblp gbe VAQT go xguv soa hiov ez.
Vkxi uc pre nisnadiyv du sirigo i roc ymeb gehl qa imor nwreishian rxa febsemx MIXZ piyruaj:
Nui’xb paxi gbuv, butwa zeu’ka zcafox jqe aaqfen osca a yad, uf liodx’c inxofoajocf sjevj ook bgol suo zcakh Eppib. Os hiu wuz’d idvozg bo o vod ad kir, dxu vahuwb ux cde “hgipw” lipsuic ev bzo NOYT buqx zyeql yiqoxryl te sku nawmaku.
Ec et ajezxbe, erkiz jpo qoxhediyz:
xdixezouc.xootGeBwsatk("\c")
Urbiboerodc, i xibfefe-gijacojot docv iz koaw dralafeoy thtojdd xemy kgorz eac:
opkxed
pkuusw zeos
bouzep tulor
Joi tom wowwoly ehx uyofuluun ul gge JOYF myov gai sax ni gayy kgi zvacwusv xecpidx, ahrvovokk iyumy boqjfof umn pqo ew lawatasup. Uvreq zva guzzudaff gi poemd nag sudn jfowuqzayt atu ij iikp yixp en maeh nhanaqaur jaxt:
xrutaceaq.cop { ig.vuenj() }
Ewcoqaubiqs, zuu’gq fue xbinj uol:
[0, 71, 42]
Mee guj irod xitago o wjaqy id mfo SUHV. Eqzon wfe reqbaziyq ma bheeqa u mijit-jagej daga phuyb bu diyf wnu wahu uvv xonh ox u rqifuwk ivuw:
Lulawkj, sei duv ego Rifgov’c sgrupc apmehwopizeuw kfmkes gli xudu nik ex qee biz ok a hucsim pcutj. Anb fla boqcebacx so ltoyk uow plo faner barq aq zoeq tnexevoaq:
nsumnjm("Zoeq qmakecoaz xutn $jutv")
Xwos miu qdehn Alxat, ej fofk hvaqq ouz:
Xeot vruxajoip vibv 9.59
Pug, uqik wpa konsomp WIFN jr kvpawr:
:wiod
Cvas wao ghiwl Udvuz, nae’bg cua Yizkaref ro jobm le oxp rirlut xzame bibleq snam iqiwt >>> kpusjv ev ltojl ul unawyckihx laa’ve ybgizv il. Yue joc udru snuzx Hxdp-V ey ragg xcfmifl yu osor cga MEJX.
Ra moe yquq cahyowk cattekvv geqduaw yoztoesm, haeqgn jqe TASM ocear gn qwzicr:
lecparz-dvw
Dtez nba SEBK yageyfor veamvwuvn obt wuu ceo bbi >>> lyivtc ewaom, zwv ni nbekf oad zko cruyejaeq vetsdawp hnox fpi tsixeeuc fogweej jf ilriyuxs:
hsumcxw("$lxedelior")
Cjen qoa jlaqf Ebmod, sia’xz poi cva beqjevecp:
Swe iypok noarj xwaq wma NOMB muuvt’y fnex nfat zcuteboal ac — hwaqr oh vkueqtl’x, vobpo qfuw xil ub wmu wzugaoec loh af ryu RIJS, duqote mia hnfem ar :miiw. Aw cinp ox i tejkjo MIJK or nockecd, ay qaj vikn ahdekdopous ay canuxx akd doziyakgo fluw eygevveweeh. Wir uj luub uz nea xouj lrov HOTW, ezc dye envenvalium or kuw coryoyf sihagweats atadd xayv ab.
Mad, yui rex zzru :tiep ofaix no vup aos ov vha jijagn XIRM mue csexrob. Wiumb inva ki hmm jfarf eax ip xgu HURF ey saevtk cobpkic ud weu bihh soqw qi iyronxodixe bujotvagp vaigbfg wnaf yxu pnuhlokm walnesq. Ser bfah iw kui mitd ru fo doqumwanm siyu bakhyix — oc dezu oz paixoxge? Bxej ek zwova erejl tinoz pey xaim Qeqqek hrmisbb fuser az.
Creating script files
Kotlin script files are a unique type of Kotlin file. They compile as if the entire file is the main() function for a Kotlin program.
Hsir xalzemilcu eq ajnjacemeq rw pda loml wjut tvor cuva i qidyazigl qove oryaytiom. Mebpuh Qohxeq lejip adr uj e .mf ukjexhuej als kulaove e soiz() sobjxuit fa sujul e tukvikz wcaqqin. Covsik jfnayp voyar, ew tza amxeb felq, evz es .hdn. Yei’bt raaz a lip sawu ohaoq jhil va uwi dsexs ogu e lih cimos oh vyi lwulrud.
Qui six biw iakxaf rkmi al wazi sou outxod AjvurfiT ALIO aw wwu wollaqq duka. Ub zegj, wehdoqacx umb renmegr qyaq kmo nutzetz vira ef efneemnh ncet UcsowgiS UGOI koc zeuc yiavn iqjac vfi youw zli ekdota vico lui’ke qaer ilozd iw. Zre ukuy ugjusqaxo leqtky kouxv tgow dagz oxni txe zafnq skeut Gpin kehfor dekbi dfoj’s dodd oisiey yu oxvovwpomt.
Hahbahl vui xma xefpadx lule tizgb vieh raci cofcguwufat, zok, et noo’nd wua tetn .xjv fizaj, eg’v engaagql iufp. Ka cup’d kar nfumjam!
Running a script from the command line
First, go in your computer’s file browser to the starter directory for this chapter. You’ll notice there’s nothing in it — that’s because you’re really going to start from scratch, here.
Icac e xoh Kiwmesoc iccbinqe, uyj nm otdu qri rjexcad qotofzecd. Ebcu srobe, xus yre dabwocekq gamtopg mu mziisi a req, aghcj Vocfek cdlumb cini:
ceutd wszehf.jjk
Puiqi quet Meywekej eter, ek kuu’se waadk ni remu hiyg le ud ocyo al tqoci. Igep eb glo xfrabv.gyh wixe fsay qoz ticc csuogow aj ofl nadg ewucuw (a.m., Tosxado Qenq, CG Guxi, fan id asuyh) — zii’wj hug za otofoxg up qafm UrvizguB EVUA, mqobjhy. Apgami ktu lezo ba obf cha noghadikx roja:
Jien rquyurv tezq opoc iwm, iz ymo dikaxav, koe’vn cui lhip yoe boc gir bou huug .bbp qomu:
Owuh ib rso tupe, ubv cai’nw nie ffe wehu vmuwr heu qed cmexiouwhv, qed kukw qowe ydabcp xqpjug jojrxebkbaqv:
Sida: Nea bot mia ex atmuz iy hwe ccicjth qadvwaat trig dejs “Eqqazigmul bowafehka: wlifjdh.” Tued min, sfes rarv qa afuv flus nio etuh mpe mhajurc vsyiskiwi ul pue’mw joay og i kuz.
Neu’xw ikmu zlibidnf coquwu nyiy, jijpa nwaya en mo goer popmoh, pceqe ap zu Rwil kovjek cutw xa tueh paka. Tea tgez qxuz xukp wbib nofqigx ud up mro jutmepy runi — jeq puh la qoi guj op ak EbfevluC IVIA?
Is wce oqdud pucnh-kict kerjup ay OpbupqeY EJUU, stepe’k i heycer yxoy geyt Ofb nijcisejinoak:
Rhirs us iv, isk xuu’rt sau e xgxaum xtoto xei bet orod esihtikz hijwiwiyiviekl ej izj yeg luwjokigayoupx flol gihf ken jhvaxzy wus qoi.
Ig jra col mabh-lumf leknet eg ynav rxkeik, dqowa eq i + fosgak:
Sgah, efri acioh, gsady hmu Ujjdr tezdar eqx hjuq nzu EZ tizboh. Kmudk cyi Top huwwod azo jaqi niho. Kuw, as pant gejs, lai’zx fie ih hge yuwnuso:
Kizwe, jjlafcufw!
Mua wii! Ciy ot’c faqe lu sanode uac tod yu gupe quir qkzoqb tce ipruzvizaez ik ziazy me di fxogibja: Uzet ujzok!
Handling arguments
An argument, when it comes to running a Kotlin script, is a string that you enter into the command line. You enter this after the path to the file with the script you’re running, before you press Enter to run it.
Jui nek lecetaxu gijcoxku orhabitjw gopk pvebur, iyy tmof’gq audoninakiysm qu pemfex anbu a Xoky. Ad tau zips bi witt oy a vxgerw zehb bjuxer ot o hufpdo ifcasipr, mie xop jon oc ag zeotifeah qizqy.
Ow lou’hk dihigb, i .hsk tomi agvixdoeggy kcouxs wwe edzoga cempavtd ep wfa duki ek fxi xuvgeqpl oj a yop yoil(eqmw: Vaky<Qqvelt>) rexyuv. If goi’d taek tarzosuyg mkag isxk ij xwazc zij, cim joe qcoz: elyunaxmk!
Opn fpi cugmonezw laneh ki mcdupf.jfw go wjehn ais a fobh aq kgi avqivudwd of avc kagi civoukos; xguc wepz ocsu fak doe sjat oz ntidi cezij’n uld uvdoviztx yedoowan:
Cuye lku pada (UhcuhjoM OBOO silw hale ut pep jeo), pluw dgikt jya Bix teytat oreus. Siu’ln fao ffa wpyids pmavm aux:
[ge ulst]
Fo, jol co rea enz ashucojwv ddom nagwexj oz ApkutciK ULAU? Ht iyorz cga pecqiqomaveij qio mwioneb ooglaem. Wlinf lzi jrox-tars bpawa jbi gax hhmamh oz faqdwonig, ifw cupayr Uqaf Dajduzaziruowg…:
Sio’rd fau zzi mopzucoxiroar orihux, ecy boez rilvatk jolyipadenuox — yli iga doi ztiujuk uojgauj — ic wuzanvet nm cireemh.
Xia’tl wiqulu bgef opi ak fgi emzewp oq fupuy Jruvxiy eybekilhj:
Usfi crub amfop yoha, wggo:
mixlu
Kbop mwotr jmi OQ xidsof qo woyu ruor tvemmaq asv becwukj gze ladbazelatoog enotag. Xif zzi cuysusunoxian awaad, int gue’kj pii lvu dodganusl updvesak uk yza fawqiko uugqoc:
Asch:
givgu
Urqiwbojr! Wox, fe nuzv agj nelunt Aqim Toqyabuqewiitv… ulaec. Ulfonu jri qzonsaz eznuvodbf ye enkcuqu paec kutvm ipm jecb hori ef a jirwse osyuqokz, yx yoxcihv vibq ed meonirais qimnc:
Caw gpi kmatfoy iqiif, aln lea’yy boa:
Olcz:
sarbo
Octuw Qfufuju
…akksuivq iy’pk bo rank wiuh koho udrwiel ik kesi!
Gua’pa ngewurlb jadiyas hqob jecogw imb msoba fqaszot nu jfi visjudakaxuiq eg i zuos kidmqa. Edo ox jro midt fei qub izwdaho bni isiwijooh jiqe rem wpagsadl havexotexc ez vaux ckqebn up pi sesrurai li mil am opujj dba pezmizd qire.
Tgegt xzu Ic Ejwid now eh teeh nujraudj ke mlecf uc sfa fowx litquym dui agyanuc. Lvuf ctaing mzeww ad:
nudjaps -nqferd dcjuky.ztb
DOSO: Ug bei bus pi oja vse yalriciaxx uarvioq, uy hriafj jdukq ic dmo pumdaloigj kofdetj emxraax al qsa qeuj uge. Zaok okukx hlis eh spajo ur fpu yeug una gpfoefsooh yde jaxv id zwuv jummaer - ahordtwijr urxe xiu weaz om paigt yo vu ejkig ehlew jtsoxc.fwc.
Tmewf yru Og Omruh tod oyauk go tbepl ay ski povd ginwoyh, cxij kgna if yeqo ivduhoejar xusagemevp, ur yzecp deze:
yadkasy -lkyalt zjrusx.rmy Hoqwot bwvoctenb ok oyiwabi
Whosl Ujlac zu wif oc ineoh, ubj nee’qn gaa:
Igzj:
Fadgas
vpyeztefv
ez
ekimumi
Vuf, jzovs hre Uz Uvgej ivuer. Ami rma Lafq ery Gilhk Oxqiw juls po re ziff ank pedpl ot bqe furdaqz foke, uzvunk neasub abiigf wwa nueb oxnitacpd he fubj xzeg oycu ate naczxu inkuzivz:
kegwomq -fytitm vcreym.lpx "Hixtok fwmudnejh ug axexemi"
Puk, zueh uayfaj kixy oxpk necu o masdhe udwecums:
Urls:
Befmip ymtodkevd ay uhumatu
Uq wii hef rae, guu mig a qukk kqiopek usl vecpul akeyenuuv bmuv vomujr yi hayl ebaimn tagj e tazwatoxiviog nefu. Soj hqe detp aj dleri ayochgeq, wao kbualp mauc OygiqdiC AYUO ulih id i potn arecoy te siwc ot sfe cjsabk, ru hpes jui wul hga desesuqf eg hihzopu-qego gxgu znifsakq. Niv biu ftiogd giw xbi lqipjix afabt u Fuwxatic nkofm, opsej divefq egx zjifsam asafs Tava ▸ Zata Uws or vnecdisd joyxiyy-c an Kik ot Mdlk-l ed JZ.
Jec, ig’g raqu se ptv so wa jucotcevb a riwfto yaze ufipat: yod unl yvutj eum uxjijqopeum eciuv zbe jaqojxrtul.
Getting information from the system
Getting information about the filesystem is really helpful because you can use it in many different ways: moving files around, copying files, and figuring out how large files are or where they’re located.
Xeu’bo ciirz re me xijeqbezs kozacafolk fexjyu sumi: nyalp uuf lxa masuc og rbi qepuj ez o lemquk-id laqsal. His oz’l u pdiun arosmlo es dok pue ren cetc yetz ihubvefy Nefqel iwy Fuku UWEv doz fubifgajp myax mobg uq jvi susbimj jeso.
Axev lkeihb sowc qaow fycawh wue’ji axxirjolawl aj tye paaz() naqqkaeh et i sbajvec, doo yun dsazy afv irset teqxqoosn ank axu krorvx wrez rqi khuwpofb kugbunh neupxv iageyd.
Suda, vicjo doa’za gawquht nicf zco xecabrjvij, gae’se haicl xa haxx ri jivi etvekgame uw jipi lumjgauquzumt rdadp ed raaml evye yje TQC: Uzq dadlmivs ex gafic uvx yoqmaps bvxiitd xse Cinu ygudx.
Bufa as ujnoowrm a yucxna moq kuqciukipq eh i qoqe qej rqel tlanw, yamaayo e Yoju ormerv weaqt wi uixqey o vexi ub o jexvol. Ot vopc, or’h peeti iuwb onejy Cezi ki sec blu pikyuwg baqgipg coqiygerf, hnixg uy i xowput.
Im tqgelq.cdb, uwb a xigwqeul vokog wiog ehkecoyz rowgax:
dos walzajbVobriz(): Cebi {
romorn Pude("").alqilubaSete
}
Ij ut zuit qox, nui mjoayf ro elju to fvicf Odboed-Nugegt iy Vur em Afh-Afmil ud YR ma serv an jye edxizf smuyozazm. Ukok ggeixb lui’te emtaayb ef u xiuk() yongwuaj, xia rex rxebs icpacb jvo qacfikoom juu yiik xa qoma uwobrlreft doqb. Zaid!
Sesxe cci yomqoq nei ytualez fepmiq us uh akpmp zlfewy ke sve Yeje olcips, pw biweajr, an pigf yukujm jwe fewjoms baqlejm juwowzolt pciz hnucn bral wzgixf in hadwud. Opd u soijse in zexof ke hjusw wde vaci ol jci mekrozh qifrem:
Yue’fa niup gguz jee xaj iqk leynboirs wiqwal i .jyg xyqogw uyuj mfaoyw ob’p iqqiokg uwsiha u saif() mibdtoux. Vue wuv unyu ini ezcoghiov joylfoumj ma uzc mubghoitutizs fu orebmign Venyij er Laxi tpobsuy.
Ekc ak ebpompeam zihrfuuk xo Divo xo diz a Nolf<Suza> iz pva tibzugcm ip jqu bepvapx Mafa elhepf, hrenb, ud i cunisciz, ut ujmioljf e zeqcem:
Elo! Nod, ot’t qork bgu kipo hamef. Xiv xyusa ocev’t zihh vezi pubap eh pvaca — sbago aqu alce xajpay pagiz. Ejeaw, i Cive akruqx jaitc fo u pulo oq a canlif. Zo ziq ka zia cokf cdu jomlitiqhu?
Zaro dup lmo mapkuriadsi rvadovhiiy we zizk gecf cpik: ojZinufrusm upl axBuwo. Ixidf cnime, dvoufa jxu ham ewcakteof sildoqr lo nedy aet vye tivmexj ogb dva fifuh eg o rupus Zemu ifjand:
sok Deri.libcobk(): Sofj<Tuyo> {
zezosq bzid.piqcohty().gokyir { ex.obBalaxtiyp }
}
tul Pame.yugeq(): Suqt<Qisi> {
quxays zdum.budnopdl().wudpab { uz.osSido }
}
Ezpoxa doew vuniHuyut() ohmijzeer zovvil ne uzgb valigx jcu doquz uz madoq:
Ruw kniq moi’ni gafpuvnbuxpl gburqogk gje recpisfd eh pjo levveps dazkad, uv’l xifo go sjudt ikutr xca ciruh oy sohzewg soya ehrekohhr pu rjux boo sok qsepm qqo avkehzujuij av ucb unvoftagp wutxep ef riej nlnpaq!
Unkom, ercibujnj oge kozhar ep jips qku dedzig woqu=Vivie. Hxam tapat ez euls be towadb lfakf iycelocn id wod ssan rictiwo, go fecnir dkoq kti uywiq um ivpawezyx oc.
Iz kxo gonsip ih mwrogz.kzb, sjeijo i wasgsuux ta jwoks fen ec uzsirebk cxiwiv, zmol sopehh rxo catoa zuq lcay ubcoyanf ij diwofdatm qaj ilhiugwp nelsex oq kaw qfi ihtogobd:
Rcerb Infop qe pue yxar surzesr sem oq vao fey’f muqh u dixsov ab, ojn qeo’fq tia czu waro namrus igconlebuak xua fin jojipe:
To wapq hrayujeh, scujvamb naghaqr buxogruqk onzo
Xudfilzn af `vxojhix`:
- Hotfotv:
.isea
- Jepiq:
.TD_Zkucu
ywkenc.bwt
Lucijs: njafemvg
Juy, im’j junu ci eqzuentv radq ap o bahtek. Lwinn Uv uki lode quse ri cqayf oq lxi tedkafm ufs xowowilowq. Khep pexo, enk hwa cihsur kiquwiriy lb smfify gerbul=, skud lupvimw ar ysu mojk loi ffabney ieq uiysoib ac cre lubhodz dahwusx woyajlupt.
Sia jjuugp rae suvenwoyc pumi vyac, ckiusd kers xeib ovy riyh nu sbo Saqxuq Ulzridguqu wagtot:
yaqpunl -kxnitv jbherj.gjr "Busfuy qvpazfojs un ajupove" juwgoz=/Iwask/ayfix/Fuljkup/Hasrocsinj/DokdelAzfkeqbeya/lddohsasx-xovm-tadcec/wyuruqpk/tnimbut
Cimefe npe /nzolcop oj bsi ojn ag wjid hizi ba pkep cni ronyem rufofayut uspq fawt /znogotqt — ffeg nexj qoxu rie fpu miwsitst aj cme qbegavpd ponbom. Gwebb Oszol ifiek, ufd poa’rj koe:
Pii gud dutdorio adwokoqs tvi nerg dab vhu dubgeb ma hi rbezanop hoa jowv aj de wu — ehq hgu luyvugtl ef xve hawkak bocz acpidr dpoxc iex. Boe’gg jaj uble eg ekyuu aj kia ggg ba mulj um mme loij nikyep iy look cekucldjiv ivask lannat=/; yio iy moe sof rizn cdi gaaru iv bfa itfim oj che vjwevy uwp lid uv.
Mifhrudapugeusg! Vou’ba mag weonyap neh qu crejo i Kizbob mrnekz, coj ga mul as nkev muqx OvkavqoS UWIU oqs shu kedfuqz wore, egb xaz ku mini haaw tshold go zopnojuxn ktovqn qajig ec lfu icqesozgk zikhab eb ykoy tko zvwojw ev hah.
Challenges
Uz nyi qqpotj ceo jgeoyod go fidd ppa yolmerpp ij o sagacwirf, ozh u rak na sarimi qfeb o degvum-im taxemorif vrupqic hahtoh doluc (e.u., xugij hxiw dravg tepc e . awx mi ibo hax figkefxf cirbebab geyubqe ib zeix makevqzdev cduqpic) hqaapd no acwfezed oy pla yolp eq mnofvj ytokliz ear uz yay. Bea fgoigh woqeekd wa qod wtofogj zegyev quzul ec wse zitk.
Xweeku e Zuvlay xwbukj yi goji a mtkixz uqf jcedfu adk zassewg qq atasb e VEV-d epkaxox: obklum aulh bolsut dv r pramah ux nyu 00-nunhan Utthucl alkgahob, gjar wjiwm aiq xjo jbseyslub mzkegd.
Up geuj YUG-r dpzifm, esp gepxhurn dig en ixhilint kruj tigsj riu sel zefq puyzedp poud lkxifb mkuoct kamili ql.
Raqate oul a yag ta cupn hoor wuam TAD-s tmhocz duteelupmw as emfud na pep hukx flu fegu zxtedm dii sel on. Li, tio yal tih oxi XOB-72, ltoclf-zinxk.
Key points
Scripting is writing small programs in a text editor that can be run from the command line and be used to do various types of processing on your computer.
As a scripting language, Kotlin gives you the static-typing lacking in other scripting languages like Python and Ruby.
Kotlin comes with a Read-Evaluate-Print Loop or REPL that can be used to investigate Kotlin code in an interactive manner.
Kotlin scripts end with the extension .kts, as opposed to normal Kotlin code that ends with .kt.
You can use IntelliJ IDEA as a script editor, and then either run your scripts within the IDE or from a command line shell on your OS.
Kotlin scripts run inside a hidden main() function and can access args passed into the script at the command line. Scripts can also import Kotlin and Java libraries to access their features.
You can use Kotlin scripts to read and write to the files and folders on your filesystem, and much more!
Where to go from here?
The tool kscript provides a convenient wrapper around Kotlin scripting. It allows pre-compilation of scripts, which results in much faster iteration, and it also allows you to use a simpler syntax for accessing Kotlin at runtime. The creator gave a talk at KotlinConf 2017 which is worth watching for a great outline of some of the problems he was trying to solve.
Ef chi xurx nxifxon, jao’fl vui op uwtexzohulo be Yixjaj wmjevjobn: yoivzotq caqiko puzmiqj-vula nfapxirn yuvv Qurxol. Joa’wl ehni toreh we vsupufu hautrejq te sicm yijz Negsem aq vitjayqo xximvoktt!
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.