How to Run Perl Scripts in Linux Command Line

文章推薦指數: 80 %
投票人數:10人

Learn to learn Perl scripts in Linux command line. Also learn a few tricks about running Perl commands without saving them in files. Pleaseenteratleast3characters 0resultsfound Tips Commands Tutorial QuickTip Docker Explain Aug23,2022 • 3minread HowtoRunPerlscriptsinLinuxCommandLine SagarSharma TableofContents CallthemancientbutPerlscriptsarestillagreatwayofscriptingautomationtasksforvarioussysadmintasks.IfyouarenewtoitandwonderinghowtorunPerlscriptsintheLinuxterminal,justusethiscommand:perlscript.plThiswillworkevenifthescriptfiledoesn'thavetheexecutepermission.Youmayalsorunitlikeabashscript:./script.plButforthattowork,thefilemusthavetheexecutepermissionandthescriptmustbeginwiththehashbang:#!/usr/bin/perlAslongasthesyntaxisrightandPerlinstalledonyoursystem,youshouldbeabletorunPerlscriptseasily.HowdoyouknowifPerlisinstalledonyoursystem?Usethiscommandandifyouseetheversiondetails,youshouldbegood.perl--versionCheckingifPerlisinstalledIfyoursystemthrowsanerrorlike"Perlcommandnotfound,"useyoursystem'spackagemanagertoinstallitfirst.ThereareotherwaysofusingPerlinLinux.YoucanwriteaPerlscriptdirectlyintheterminalandrunitimmediately,withoutevensavingit.Let'sseethingsinabitmoredetail.ExecutingPerlscriptsfromthefilesThisistherecommendedwaytocreateandexecutePerlscripts.YouhavethescriptssavedandyoucanusetheminanIDElikeEclipseifthescriptiscomplicatedorpartofalargerPerlproject.Perlfiledoesnotneedtohaveanyextension,buta".pl"extensionisgoodpractice.Createanewfileusingthetouchcommandlikethis:touchmy_file.plTherecentlycreatedfilewillbeempty,I'llbeusingNanoeditortomodifytheemptyfile.nanomy_file.plMysamplescriptlookslikethis:#!/usr/bin/perl print"Enteryourname:"; $name=; print"Hello,${name}...youwillsoonbeaPerladdict!"; print"\n";UsingnanotoeditPerlscriptSaveandexitfromtheNanoeditorusingtheCtrl+Xkey.Noneedtogiveexecutepermissiontothescript.Useitlikethis:perlmy_file.plExecutingPerlscriptfromfileYoucanalsogivethescriptexecutepermissionandrunitlikethis:./my_file.plThat'seasy,right?ButifyouraimistojustcheckaPerlsyntax,youdon'tneedtocreateandsavescripts.ExecuteasinglelineofPerlcommandinLinuxThisisaquickmethodforquicklycheckingPerlsyntaxes.TorunyourPerlcommand,youjusthavetofollowthegivensyntax:perl-eYoujusthavetopayattentiontothequotesinbashshell.perl-e'print"Helloworld!\n";'ExecutingPerlscriptwithsinglequotes ItworkswithasinglelineofPerlcode.Youcanpassmultiplelineswithmultiple-eoptions.However,thereisabetterwayofwritingmultiplelinesofPerlcodewithoutsavingtheminafile.ExecutingPerlcommandswithoutsavingtheminafileThismethodisabitsimilartowhatyousawabove,butitislesscomplicated.StartthePerlpromptwiththegivencommand:perlThenyouwriteyourdesiredscript.Onceyou'redonewriting,youmustpressCTRL+DtoindicatethepromptEndofFile(EOF).ThePerlscriptwillbeexecutedimmediatelyasyoupressCtrl+D.Forinstance,Iwanttorunthefollowingscript:print"Enteryourname:"; $name=; print"Hello,${name}...youwillsoonbeaPerladdict!"; print"\n";Thissimplescriptissupposedtotakeinputfromtheuser(nameinthiscase)andwillreturnsomethinglikethis:RunningPerlscriptusingPerlcommandandpromptAsyoucansee,runningPerlscriptsinLinuxisnotmuchdifferentthanrunningabashshellscript.PerlisapowerfulscriptinglanguageandthoughPythonhasgainedpopularity,seasonedsysadminsstilluseitforcomplexscripting.Enjoy. QuickTip Share SagarSharma Asoftwareengineerwholovestotinkerwithhardwaretillitgetscrashed.Whilerevivingmycrashedsystem,youcanfindmereadingliterature,manga,orwateringmyplants. LinuxHandbookNewsletter Getthelatestinsightsdirectlytoyourinbox! Subscribe Great!Checkyourinboxandclickthelink. Sorry,somethingwentwrong.Pleasetryagain. Featuredposts AtoZLinuxCommands 01Feb2022 30InterestingToolsandServicestoMonitorYourLinuxServers 20Jul2021 TopFreeLinuxCloudServerstoTestorHostYourWebApplications 21Sep2020 Latestposts PrettyPrintJSONinLinuxCommandLine 19Oct2022 HowtoDeleteSoftLinksinLinux 17Oct2022 PrettyPrintXMLinLinuxCommandLine 16Oct2022 Youmightalsolike QuickTip • 4minread HowtoUseSIGINTandOtherTerminationSignalsinLinux Terminatingexecutingprocessismorethanjustkill-9.Herearesomeoftheprominentterminationsignalsandtheirusage.… SagarSharma • 15Oct2022 QuickTip • 5minread FindFilesbyNameinLinux FindingfilesbytheirnameisoneofthemostcommonscenariosoffindingfilesinLinux.Hereareafewexamplestohelp.… TeamLHB • 24Sep2022 QuickTip • 3minread HowtoKnowifYouAreUsingSystemdorSomeOtherInitinLinux IsyourLinuxsystemusingsystemdorsysvorsomeotherinitsystem?Here'showtofindout.… AbhishekPrakash • 22Sep2022 Great!You’vesuccessfullysignedup. Welcomeback!You'vesuccessfullysignedin. You'vesuccessfullysubscribedtoLinuxHandbook. Yourlinkhasexpired. Success!Checkyouremailformagiclinktosign-in. Success!Yourbillinginfohasbeenupdated. Yourbillingwasnotupdated.



請為這篇文章評分?