linux guide
DESCRIPTION
Linux GuideTRANSCRIPT
LINUX
TheUltimateStepbyStepGuidetoQuicklyandEasilyLearningLinux
TEDDAWSON
©Copyright2015byWECANTBEBEATLLC
ChapterOne:LINUXHISTORY
ChapterTwo:LINUXDISTRIBUTION(DISTRO)
Introduction
GUIDETOCHOOSINGDISTRIBUTION
LinuxMint
Ubuntu
DebianGNU/Linux
Mageia
Fedora
openSUSE
ArchLinux
CentOS
PCLinuxOS
SlackwareLinux
FreeBSD
ChapterThree:LICENSING
COMMUNITY
DEVELOPMENT
ChapterFour:INSTALLINGDEBIAN8
WhatisDebian
CustomizingyourSystem
Terminal
Installsudo
Setupthenetwork
Setupyourhostsfile
LoginviaSSH!
Installingthebasics
InstallingMySQL
SettingupMySQL
ChapterFive:INSTALLINGCENTOS7
InstallationofCenOS7
Step1:DownloadtheISOImage
Step2:MakeabootableDrive
Step3:BeginInstallation
Step4:SelectLanguageandKeyboard
Step5:ChangetheInstallationDestination
Step6:SelectthePartitioningScheme
Step7:CreateaSwapSpace
Step8:CreateaMountpoint
Step9:AcceptChanges
Step10:SetDateandTime
Step11:BeginInstallation
Step12:SetUpRootPassword
Step13:CreateaUserAccount
Step14:CompleteInstallation
ChangeandSetHostnameCommand
Method#1:hostnamectl
HowdoIseethehostnames?
HowdoIdeleteaparticularhostname?
HowdoIchangehostnameremotely?
Method#2:nmtui
Method#3:nmcli
Toviewthehostnameusingnmcli:
Tosetthehostnameusingnmcli:
ChapterSix:LINUXANDUNIXMANCOMMAND
Syntax
Description
GeneralOptions
MainModesofOperation
FindingManualPages
ControllingFormattedOutput
SectionNumbers
ExitStatus
Environment
Files
Examples
ChapterSeven:LINUXDIRECTORYCOMMAND
sampleoutputs
ListonlyfilesinUnix
Task:Createaliasestosavetime
ChapterEight:WORKINGWITHFILES
UNIXFileNames
LookingattheContentsofFiles
CatCommand
MoreCommand
HeadCommand
TailCommand
Copying,Erasing,Renaming
CopyingFiles
ErasingFiles
RenamingaFile
UsingtheCommandLine
StandardInputandStandardOutput
Redirection
UsingPipesandFilters
SomeAdditionalFileHandlingCommands
WordCount
ComparingtheContentsofTwoFiles:thecmpanddiffCommands
ChapterNine:NAVIGATIONANDFILEMANAGEMENT
PrerequisitesandGoals
NavigationandExploration
Findingwhereyouarewiththe“pwd”command
LookingattheContentsofDirectorieswith“ls”
MovingAroundtheFilesystemwith“cd”ViewingFiles
FileandDirectoryManipulation
CreateaFilewith“touch”
CreateaDirectorywith“mkdir”
MovingandRenamingFilesandDirectorieswith“mv”
CopyingFilesandDirectorieswith“cp”
RemovingFilesandDirectorieswith“rm”and“rmdir”
EditingFiles
ChapterTen:UNIXSHELLSCRIPTING
ShellScriptingIntroduction
ChapterEleven:SHELLBASICOPERATOR
ArithmeticOperators
RelationalOperators:
BooleanOperators
StringOperators
FileTestOperators
RunThe.ShFileShellScriptInLinux/Unix
.shAsRootUser
chmodCommand:RunShellScriptInLinux
ChapterTwelve:SHELLEMBEDDINGANDOPTIONS
Shellinstalling
Backticks
Backticksorsinglequotes
ShellAlternatives
Practice:ShellInstalling
ChapterThirteen:SHELLHISTORYSEARCHCOMMAND
EmacsLine-EditModeCommandHistorySearching
fccommand
Deletecommandhistory
FILENAMEGLOBBINGWITH*,?,[]
Chapterfourteen:UNIX-SHELLINPUT/OUTPUTREDIRECTIONS
ChapterFifteen:UNIXSHELLFUNCTION
CreatingFunctions
Example
PassParameterstoaFunction
ReturningValuesfromFunctions
Example
NestedFunctions
FunctionCallfromPrompt
Unix-PipesandFilters
ThegrepCommand
ThesortCommand
ThepgandmoreCommands
ChapterSixteen:UNIXUSEFULCOMMAND
FilesandDirectories
Manipulatingdata
MessagesbetweenUsers
ChapterSeventeen:REGULAREXPRESSION
Invokingsed
ThesedGeneralSyntax
DeletingAllLineswithsed
ThesedAddresses
ThesedAddressRanges
TheSubstitutionCommand
SubstitutionFlags
UsinganAlternativeStringSeparator
ReplacingwithEmptySpace
AddressSubstitution
TheMatchingCommand
UsingRegularExpression
UsingMultiplesedCommands
BackReferences
ChapterEighteen:FILESYSTEMBASICS
DirectoryStructure
ThedfCommand
TheduCommand
MountingtheFileSystem
UnmountingtheFileSystem
UserandGroupQuotas
ChapterNineteen:UNIX-USERADMINISTRATION
ManagingUsersandGroups
ModifyaGroup
DeleteaGroup:
CreateanAccount
ModifyanAccount
DeleteanAccount
ChapterTwenty:SYSTEMPERFORMANCE
PerformanceComponents
Unix-SystemLogging
The/etc/syslog.conffile
LoggingActions
LogRotation
ImportantLogLocations
ChapterTwenty-one:UNIXSIGNALSANDTRAPS
LISTofSignals
DefaultActions
SendingSignals
TrappingSignals
CleaningUpTemporaryFiles
IgnoringSignals
ResettingTraps
ABOUTTHEAUTHOR
LINKSANDRESOURCESDEDICATION
SPECIALTHANKS
ChapterOne:LINUXHISTORY
Thehistory of Linux began in 1991with the commencement of a personal
projectbyFinnishstudentLinusTorvalds tocreateanewfreeoperatingsystem
kernel.Sincethen,theresultingLinuxkernelhasbeenmarkedbyconstantgrowth
throughout its history. Since the initial release of its source code in 1991, it has
grown from a small number of C files under a license prohibiting commercial
distributiontothe3.18versionin2015withmorethan18millionlinesofsource
code under the GNU General Public License (GPL). He wrote the program
specificallyforthehardwarehewasusingandindependentofanoperatingsystem
because he wanted to use the functions of his new PC with an Intel 80386
processor.DevelopmentwasdoneonMINIXusingtheGNUCcompiler.TheGNU
CCompiler is still themain choice for compiling Linux today. The code can be
builtwithothercompilers,suchastheIntelCCompiler.AsTorvaldswroteinhis
book ‘Just forFun,’he eventually endedupwritinganoperating systemkernel.
On25August1991(age21),heannouncedthisnewsysteminaUsenetpostingto
thenewsgroup“comp.os.minix.”
TorvaldsfirstpublishedtheLinuxkernelunderitsownlicense,whichhada
restriction on commercial activity. The software used with the kernel was
developedaspartoftheGNUprojectlicensedundertheGNUGPL,afreesoftware
license. The first release of the Linux kernel, Linux 0.01, included a binary of
GNU’sBash shell. In the “Notes forLinux release0.01”,Torvalds lists theGNU
softwarethatisrequiredtorunLinux:Sadly,akernelbyitselfgetsyounowhere.
To get a working system, you need a shell, compilers, a library, etc. These are
separatepartsandmaybeunderstricter (oreven looser)copyright.Mostof the
toolsusedwithLinuxareGNUsoftwareandareundertheGNUcopyright.These
tools are not in the distribution - askme (or GNU) formore info. In 1992, he
suggested releasing the kernel under the GNU GPL. He first announced this
decisioninthereleasenotesofversion0.12.InthemiddleofDecember1992,he
publishedversion0.99usingtheGNUGPL.LinuxandGNUdevelopersworkedto
integrateGNUcomponentswithLinux tomakea fully functional freeoperating
system.Torvaldshasstated,“MakingLinuxGPL’dwasdefinitelythebestthingI
everdid.”Torvaldsinitiallyusedthedesignation“Linux”onlyfortheLinuxkernel.
Thekernelwas,however,frequentlyusedtogetherwithothersoftware,especially
thatoftheGNUproject.ThisquicklybecamethemostpopularadoptionofGNU
software.InJune1994inGNU’sBulletin,Linuxwasreferredtoasa“freeUNIX
clone,” and theDebianproject began calling its productDebianGNU/Linux. In
May1996,RichardStallmanpublishedtheeditorEmacs19.31,inwhichthetype
ofsystemwasrenamedfromLinuxtoLignux.Thisspellingwasintendedtorefer
specificallytothecombinationofGNUandLinux,butthiswassoonabandonedin
favor of “GNU/Linux.” This name garnered varying reactions. The GNU and
Debianprojectsusethename,althoughmostpeoplesimplyusetheterm“Linux”
torefertothecombination.
The largestpartof theworkonLinux isperformedby thecommunity: the
thousands of programmers around the world that use Linux to send their
suggestedimprovementstothemaintainers.Variouscompanieshavealsohelped
notonlywiththedevelopmentofthekernelsbutalsowiththewritingthebodyof
auxiliarysoftware,whichisdistributedwithLinux.AsofFebruary2015,over80%
ofLinuxkerneldevelopersarepaid.Itisreleasedbothbyorganizedprojectssuch
asDebianandbyprojectsconnecteddirectlywithcompaniessuchasFedoraand
openSUSE.Themembersoftheserespectiveprojectsmeetatvariousconferences
and fairs, in order to exchange ideas. One of the largest of these fairs is the
LinuxTaginGermany(currentlyinBerlin),whereabout10,000peopleassemble
annually,inordertodiscussLinuxandtheprojectsassociatedwithit.TheOpen
Source Development Lab (OSDL) was created in the year 2000, and is an
independentnonprofitorganization,whichpursues thegoalofoptimizingLinux
foremployment indatacentersand in thecarrier range. It servedas sponsored
working premises for Linus Torvalds and also for Andrew Morton (until the
middleof2006whenMorton transferred toGoogle).Torvaldsworked full-time
onbehalfofOSDL,developingtheLinuxkernels.
Chronology
1991:The Linux kernel is publicly announced on 25 August by 21-year-old
FinnishstudentLinusBenedictTorvalds.
1992:The Linux kernel is re-licensed under the GNU GPL. The first Linux
distributionsarecreated.
1993:Over100developersworkontheLinuxkernel.Withtheirassistance,the
kernel is adapted to the GNU environment, which creates a large
spectrumofapplicationtypesforLinux.Theoldestcurrently(asof2015)
existingLinuxdistribution,Slackware,isreleasedforthefirsttime.Later
that sameyear, theDebianproject is established.Today it is the largest
distributioncommunity.
1994:Torvalds judges all components of the kernel to be fully matured: he
releasesversion1.0ofLinux.TheXFree86projectcontributesagraphical
userinterface(GUI).CommercialLinuxdistributionmakersRedHatand
SUSEpublishversion1.0oftheirLinuxdistributions.
1995:LinuxisportedtotheDECAlphaandtheSunSPARCsystems.Overthe
followingyears,itisportedtoanever-greaternumberofplatforms.
1996:Version 2.0 of the Linux kernel is released. The kernel can now serve
several processors at the same time using symmetric multi-processing
(SMP),andtherebybecomesaseriousalternativeformanycompanies.
1998:Many major companies such as IBM, Compaq, and Oracle announce
theirsupportforLinux.TheCathedralandtheBazaarwerefirstpublished
asanessay(laterasabook),resultinginNetscapepubliclyreleasingthe
sourcecodetoitsNetscapeCommunicatorwebbrowsersuite.Netscape’s
actionsandcreditingoftheessaybringsLinux’sopensourcedevelopment
modeltotheattentionofthepopulartechnicalpress.Inaddition,agroup
ofprogrammersbegandevelopingthegraphicaluserinterfaceKDE.
1999:A group of developers began work on the graphical environment
GNOME, destined to become a free replacement for KDE,which at the
timedependedonthethenproprietary,QtGUItoolkit.Duringthisyear,
IBMannouncedanextensiveprojectforthesupportofLinux.
2000:DellannouncesthatitisnowtheNo.2providerofLinux-basedsystems
worldwideandthefirstmajormanufacturertoofferLinuxacrossits full
productline.
2002:Themediareported,“MicrosoftkilledDellLinux.”
2004:The XFree86 team splits up and joins with the existing X standards
bodytoformtheX.OrgFoundation,whichresultsinasubstantiallyfaster
developmentoftheXserverforLinux.
2005:The project openSUSE begins free distribution from Novell’s
community. In addition, the project OpenOffice.org introduces version
2.0whichthenstartedsupportingOASISOpenDocumentstandards.
2006:OraclereleasesitsowndistributionofRedHatEnterpriseLinux.Novell
and Microsoft announce cooperation for better interoperability and
mutualpatentprotection.
2007:DellstartsdistributinglaptopswithUbuntupre-installed.
2009:RedHatsmarketcapitalizationequalsSuns,interpretedasasymbolic
momentforthe“Linux-basedeconomy.”
2011:Version3.0oftheLinuxkernelisreleased.
2012:TheaggregateLinuxservermarket revenueexceeds thatof therestof
theUNIXmarket.
2013:Google’s Linux-based Android claims 75% of the Smartphone market
share,intermsofthenumberofphonesshipped.
2014:Ubuntuclaims22,000,000users.
2015:Version4.0oftheLinuxkernelisreleased.
Chapter Two:LINUX DISTRIBUTION(DISTRO)
Introduction
The bewildering choice and the ever increasing number of Linux
distributions canbe confusing for thosewhoarenew toLinux.This iswhy this
bookwas created. It lists 10 Linux distributions (plus an honorablemention of
FreeBSD,by far themostpopular of all of theBSDs), it is considered tobe the
mostwidely-usedbyLinuxusersaroundtheworld.Therearenofigurestoback
up this claim, and there are many other distributions that might suit your
particularpurposebetter,butasageneralrule,allofthesearepopularandhave
veryactive forumsormailing listswhereyoucanaskquestions ifyougetstuck.
Ubuntu,LinuxMint,andPCLinuxOSareconsideredtheeasiestfornewuserswho
wanttogetproductiveinLinuxassoonaspossiblewithouthavingtomasterallof
itscomplexities.Ontheotherendofthespectrum,SlackwareLinux,ArchLinux,
and FreeBSD are more advanced distributions that require a deeper
understanding, before they can be used effectively. openSUSE, Fedora, Debian
GNU/Linux and Mageia can be classified as good “middle-road” distributions.
CentOS is an enterprise distribution, suitable for those who prefer stability,
reliability,andlong-termsupporttocutting-edgefeaturesandsoftware.
GUIDETOCHOOSINGDISTRIBUTION
LinuxMint
LinuxMint,adistributionbasedonUbuntu,wasfirst launchedin2006by
Clement Lefebvre, a French-born IT specialist living in Ireland. Originally
maintainingaLinuxwebsitededicatedtoprovidinghelp,tipsanddocumentation
to new Linux users, the author saw the potential of developing a Linux
distributionthatwouldaddressthemanyusabilitydrawbacksassociatedwiththe
generally more technical, mainstream products. Since its beginnings, the
developers have been adding a variety of graphical “mint” tools for enhanced
usability; this includes mintDesktop - a utility for configuring the desktop
environment,mintMenu-anewandelegantmenustructureforeasiernavigation,
mintInstall - an easy-to-use software installer, and mintUpdate - a software
updater. Mint’s reputation for ease of use has been further enhanced by the
inclusionofproprietaryandpatent-encumberedmultimediacodecsthatareoften
absentfromlargerdistributionsduetopotentiallegalthreats.Perhapsoneofthe
bestfeaturesofLinuxMintisthefactthatthedeveloperslistentotheusersand
arealwaysfast in implementinggoodsuggestions.WhileLinuxMint isavailable
as a free download, the project generates revenue from donations, advertising,
andprofessionalsupportservices.
Pros:Superbcollectionof“minty”toolsdevelopedin-house,hundredsofuser-
friendlyenhancements,theinclusionofmultimediacodecs,opentousers’
suggestions.
Cons:Thealternative“community”editionsdonotalwaysincludethelatest
features;theprojectdoesnotissuesecurityadvisories.
Ubuntu
ThelaunchofUbuntuwasfirstannouncedinSeptember2004.Althougha
relative newcomer to the Linux distribution scene, the project took off like no
other, with its mailing lists soon filled with discussions by eager users and
enthusiastic developers. In the years that followed,Ubuntu grew to become the
most popular desktop Linux distribution and has contributed greatly toward
developing an easy-to-use and free desktop operating system that can compete
wellwith any of the proprietary ones available on themarket.On the technical
sideofthings,UbuntuisbasedonDebian“Sid”(unstablebranch),butwithsome
prominentpackages,suchasGNOME,Firefox,andLibreOffice,updatedtotheir
latestversions.Itusesacustomuserinterfacecalled“Unity.”Ithasapredictable,
6-month release schedule,with anoccasionalLongTermSupport (LTS) release
that issupportssecurityupdatesfor5years,dependingontheedition(non-LTS
releasearesupportedfor9months).OtherspecialfeaturesofUbuntuincludean
installable live DVD, creative artwork, desktop themes, migration assistant for
Windows users, support for the latest technologies, such as 3D desktop effects,
easyinstallationofproprietarydevicedriversforATIandNVIDIAgraphicscards,
wirelessnetworking,andon-demandsupportfornon-freeorpatent-encumbered
mediacodecs.
Pros:Fixed release cycleand supportperiod; long-termsupport (LTS)variants
with5yearsofsecurityupdates;novice-friendly;awealthofdocumentation,both
officialanduser-contributed.
Cons: Lacks compatibility with Debian; frequent major changes tend to drive
some users away, the Unity user interface has been criticized as being more
suitable for mobile devices rather than desktop computers; non-LTS releases
comewithonly9monthsofsecuritysupport.
DebianGNU/Linux
DebianGNU/Linuxwasfirstannouncedin1993.Itsfounder,IanMurdock,
envisaged the creation of a completely non-commercial product developed by
hundreds of volunteer developers.With skeptics far outnumbering optimists at
thetime,itseemeddestinedtodisintegrateandcollapse,buttherealitywasvery
different.Debiannotonlysurvived,itthrivedand,inlessthanadecade,itbecame
the largest Linux distribution and possibly the largest collaborative software
productevercreated!ThefollowingnumberscanillustratethesuccessofDebian
GNU/Linux.Over1,000volunteerdevelopersdevelopit,itssoftwarerepositories
containcloseto50,000binarypackages(compiledfor8processorarchitectures)
andisresponsibleforinspiringover120Debian-baseddistributionsandliveCDs.
These figures are unmatched by any other Linux-based operating system. The
actualdevelopmentofDebiantakesplace inthreemainbranches(or four ifone
includesthebleeding-edge“experimental”branch)ofincreasinglevelsofstability:
“unstable”(alsoknownas“sid”),“testing”and“stable.”However,thislengthyand
complexdevelopmentstylehassomedrawbacks:thestablereleasesofDebianare
not particularly up-to-date, and they age rapidly, especially since new stable
releases are only published once every 1 - 3 years. Users who prefer the latest
packagesandtechnologiesareforcedtousethepotentiallybuggyDebiantesting
distributions or unstable branches. The highly democratic structures of Debian
have led to controversial decisions and have led to infighting among the
developers. This has contributed to stagnation and reluctance to make radical
decisionsthatwouldtaketheprojectforward.
Pros: Very stable; remarkable quality control; includes over 30,000 software
packages; supports more processor architectures than any other Linux
distribution.
Cons:Conservative-duetoitssupportformanyprocessorarchitectures,newer
technologiesarenotalwaysincluded;slowreleasecycle(onestablereleaseevery1
-3years);discussionsondevelopermailing listsandblogscanbeunculturedat
times.
Mageia
Mageia
Mageiamaybe thenewestdistributiononthis list,but itsrootsgobackto
July1998whenGaëlDuval launchedMandrakeLinux.Atthetime, itwasjusta
branch of Red Hat Linux with KDE as the default desktop, better hardware
detection,andsomeuser-friendlyfeatures,butitgainedinstantpopularitydueto
positive reviews in the media. Mandrake was later turned into a commercial
enterprise and renamed toMandriva (to avoid some trademark-related hassles
andtocelebrateitsmergerwithBrazil’sConectiva)beforealmostgoingbankrupt
in2010.ARussianventurecapitalfirmeventuallysavedit,butthiscameatacost
when thenewmanagementdecided to layoffmostof the establishedMandriva
developersinthecompany’sParisheadquarters.Uponfindingthemselvesoutof
work, they decided to form Mageia, a community project that is a logical
continuationofMandrakeandMandriva,perhapsmoreso thanMandriva itself.
Mageiaisprimarilyadesktopdistribution.Itsbest-lovedfeaturesarecutting-edge
software, a superb system administration suite (Mageia Control Centre), the
ability to attract a large number of volunteer contributors, and extensive
internationalization support. It features one of the easiest, but more powerful
systeminstallersonitsinstallationDVD,whileitalsoreleasesasetofliveimages
withbothKDEorGNOMEdesktopsandcomprehensive languagesupport,with
theabilitytoinstallitontoaharddiskdirectlyfromthelivedesktopsession.The
distribution’s well-established package management features, with powerful
command-lineoptionsandagraphicalsoftwaremanagementmodule,alloweasy
access to thousands of software packages. The unique Mageia Control Center
continues to improvewith each release, offering apowerful tool for configuring
justaboutanyaspectoftheircomputerwithouteverreachingfortheterminal.
Pros:Beginner-friendly;excellentcentralconfigurationutility;verygoodout-of-
the-boxsupportfordozensoflanguages;installablelivemedia.
Cons:Lacks reputationandmindshare following its split fromMandriva, some
concernover thedevelopers’ ability tomaintain thedistribution long-termon a
volunteerbasis.
Fedora
AlthoughFedorawasformallyonlyunveiled inSeptember2004, itsoriginsdate
back to 1995when itwas launched by twoLinux visionaries—BobYoung and
MarcEwing—under thenameofRedHatLinux.The company’s first product,
RedHatLinux 1.0 “Mother’sDay,”was released the sameyearandwasquickly
followedbyseveralbug-fixupdates.In1997,RedHatintroduceditsrevolutionary
RPM package management system with dependency resolution and other
advanced features which greatly contributed to the distribution’s rapid rise in
popularityand itsovertakingofSlackwareLinuxas themostwidely-usedLinux
distribution in theworld. In later years, RedHat standardized on a regular, 6-
month release schedule. In2003, just after the release ofRedHatLinux9, the
company introducedsomeradicalchanges to itsproduct line-up. It retainedthe
Red Hat trademark for its commercial products, notably Red Hat Enterprise
Linux, and introduced Fedora Core (later renamed to Fedora), a Red Hat
sponsored, but community-oriented distribution designed for the “Linux
hobbyist”.Aftertheinitialcriticismofthechanges,theLinuxcommunityaccepted
the“new”distributionasthelogicalcontinuationofRedHatLinux.Afewquality
releaseswasall it took forFedora toregain its formerstatusasoneof thebest-
lovedoperatingsystemsonthemarket.Atthesametime,RedHatquicklybecame
thebiggestandmostprofitableLinuxcompany in theworld,withan innovative
productline-up,excellentcustomersupport,andotherpopularinitiatives,suchas
itsRedHatCertifiedEngineer (RHCE)certificationprogram.AlthoughFedora’s
directionisstilllargelycontrolledbyRedHat,Inc.andtheproductissometimes
seen—rightlyorwrongly—asatestbedforRedHatEnterpriseLinux,thereisno
denyingthatFedoraisoneofthemostinnovativedistributionsavailabletoday.Its
contributions to the Linux kernel, glibc and GCC are well-known and its more
recent integration of SELinux functionality, virtualization technologies, system
servicemanager, cutting-edge journaled file systems, and other enterprise-level
features aremuch appreciatedby the company’s customers.On anegative side,
Fedorastill lacksacleardesktop-orientedstrategy thatwouldmake theproduct
easiertouseforthosebeyondthe“Linuxhobbyist”target.
Pros: Highly innovative; outstanding security features; a large number of
supportedpackages;strictadherencetothefreesoftwarephilosophy;availability
ofliveCDsfeaturingmanypopulardesktopenvironments.
Cons: Fedora’s priorities tend to lean towards enterprise features, rather than
desktopusability;somebleedingedgefeatures,suchasearlyswitchtoKDE4and
GNOME3,occasionallyalienatedesktopusers.
openSUSE
The beginnings of openSUSE date back to 1992 when four German Linux
enthusiasts — Roland Dyroff, Thomas Fehr, Hubert Mantel and Burchard
Steinbild—launchedtheprojectunderthenameofSUSE(SoftwareundSystem
Entwicklung)Linux.Intheearlydays,theyoungcompanysoldsetsoffloppydisks
containingaGermaneditionofSlackwareLinux,but itwasn’t longbeforeSUSE
Linuxbecameanindependentdistributionwiththelaunchofversion4.2inMay
1996. In the following years, the developers adopted the RPM package
management format and introduced YaST, an easy-to-use graphical system
administrationtool.Frequentreleases,excellentprinteddocumentation,andeasy
availabilityofSUSELinuxinstoresacrossEuropeandNorthAmericaresultedin
growingpopularityofthedistribution.SUSELinuxwasacquiredbyNovell,Inc.in
late2003,and then fell into thehandsofAttachmate inNovember2010.Major
changes in the development, licensing and availability of SUSE Linux followed
shortly after the first acquisition - YaST was released under the GPL, the ISO
images were freely distributed from public download servers, and, most
significantly, the development of the distribution was opened to public
participationforthefirsttime.SincethelaunchoftheopenSUSEprojectandthe
releaseofversion10.0inOctober2005,thedistributionbecamecompletelyfree
in both senses of theword. The openSUSE code has become a base system for
Novell’scommercialproducts, firstnamedasNovellLinux,but laterrenamedto
SUSE Linux Enterprise Desktop and SUSE Linux Enterprise Server. Today,
openSUSE has a large following of satisfied users. The principal reason for
openSUSEreceivinghighmarksfromitsusersarepleasantandpolisheddesktop
environments (KDE and GNOME), an excellent system administration utility
(YaST), and, for those who buy the boxed edition, some of the best-printed
documentation available. However, the infamous deal between Novell and
Microsoft, which apparently concedes to Microsoft’s argument that it has
intellectual property rights over Linux, has resulted in condemnation by many
Linux personalities and has prompted some users to switch distributions.
AlthoughNovellhasdownplayedthedeal,andMicrosofthasyet toexerciseany
rights, this issue remains a thorn in the side of the otherwise very community-
friendlyLinuxCompany.
Pros: Comprehensive and intuitive configuration tool; large repository of
softwarepackages,excellentwebsiteinfrastructure,andprinteddocumentation.
Cons: Novell’s patent deal with Microsoft in November 2006 seemingly
legitimizedMicrosoft’sintellectualpropertyclaimsoverLinux;itsresource-heavy
desktopset-upandgraphicalutilitiesaresometimesseenas“bloatedandslow.”
ArchLinux
The KISS (Keep It Simple, Stupid) philosophy of Arch Linux was devised
aroundtheyear2002byJuddVinet,aCanadiancomputersciencegraduatewho
launched thedistribution thesameyear.Forseveralyears it livedasamarginal
project designed for intermediate and advanced Linux users and only shot to
stardom when it began promoting itself as a “rolling-release” distribution that
onlyneedstobeinstalledonceandisthenkeptup-to-datethankstoitspowerful
packagemanagerandanalwaysfreshsoftwarerepository.Asaresult,ArchLinux
“releases”arefewandfarbetweenandarenowlimitedtoabasicinstallationDVD
thatisissuedonlywhenconsiderablechangesinthebasesystemwarrantafresh
install. Besides featuring the much-loved “rolling-release” update mechanism,
Arch Linux is also renowned for its fast and powerful package manager called
“Pacman”,theabilitytoinstallsoftwarepackagesfromsourcecode,easycreation
of binary packages thanks to its AUR infrastructure, and the ever increasing
software repository of well-tested packages. Its highly regarded documentation,
complementedbytheexcellentArchLinuxHandbookmakesitpossibleforeven
less experienced Linux users to install and customize the distribution. The
powerful tools available at the user’s disposalmean that the distro is infinitely
customizabletotheminutestdetailandthatnotwoinstallationscanpossiblybe
the same. On the negative side, any rolling-release update mechanism has its
dangers:ahumanmistakecancreepin,a libraryordependencygoesmissing,a
newversionof anapplicationalready in the repositoryhas a yet-to-be-reported
criticalbug…Itisnotunheardoftoendupwithanunbootablesystemfollowinga
Pacmanupgrade.Assuch,ArchLinuxisthekindofdistributionthatrequiresits
users to be alert and to have enough knowledge to fix any such problems. In
addition,theinfrequentinstallmediareleasesmeanthatitissometimesnolonger
possible tousetheoldmediato install thedistributiondueto importantsystem
changesorlackofhardwaresupportintheolderLinuxkernel.
Pros:Excellentsoftwaremanagementinfrastructure;unparalleledcustomization
andtweakingoptions;superbonlinedocumentation.
Cons:Occasionalinstabilityandriskofbreakdown.
CentOS
Launched in late 2003, CentOS is a community project with the goals of
rebuilding the source code for Red Hat Enterprise Linux (RHEL) into an
installable Linux distribution and to provide timely security updates for all
included software packages. To put inmore bluntly, CentOS is anRHEL clone.
Theonlytechnicaldifferencebetweenthetwodistributionsisbranding-CentOS
replaced all Red Hat trademarks and logos with its own. Nevertheless, the
relations between Red Hat and CentOS remain amicable, and many CentOS
developers are in active contact with, or even employed directly by, Red Hat.
CentOSisoftenseenasareliableserverdistribution.Itcomeswiththesamewell-
testedandstableLinuxkernelandsetofsoftwarepackagesthatformthebasisof
itsparent,RedHatEnterpriseLinux.Despitebeingacommunityprojectrunby
volunteers, it has gained a reputation for being a solid, free alternative tomore
costlyserverproductsonthemarket,especiallyamongexperiencedLinuxsystem
administrators. CentOS is also suitable as an enterprise desktop solution,
specifically where stability, reliability and long-term support are preferred over
latestsoftwareandnewfeatures,likeRHEL,CentOSincludesapproximately7-10
years of security updates.Despite its advantages,CentOSmightnot be thebest
solution inalldeploymentscenarios.Thoseuserswhopreferadistributionwith
thelatestLinuxtechnologiesandnewestsoftwarepackagesshouldlookelsewhere.
MajorCentOSversions,whichfollowRHELversioning,areonlyreleasedevery2-
3years,while“point”releases(e.g.5.1)tendtoarrivein6-9monthintervals.The
point releases do not usually contain any major features (although they do
sometimes include support for more recent hardware), and only a handful of
softwarepackagesmaygetupdatedtonewerversions.TheLinuxkernel,thebase
system, and most application versions remain unchanged, but occasionally a
newerversionofanimportantsoftwarepackage(e.g.LibreOfficeorFirefox)may
be provided on an experimental basis. As a side project, CentOS also builds
updatedpackagesfortheusersofitsdistributions,buttherepositoriescontaining
themarenotenabledbydefaultastheymaybreakupstreamcompatibility.
Pros:Extremelywelltested,stableandreliable;freetodownloadanduse;comes
with7+yearsoffreesecurityupdates.
Cons:LackslatestLinuxtechnologies;occasionallytheprojectfailstoliveupits.
PCLinuxOS
Bill “Texstar” Reynolds first introduced PCLinuxOS in 2003. Prior to
creatinghisowndistribution,Texstarwasalreadyawell-knowndeveloperinthe
MandrakeLinux community of users for building up-to-dateRPMpackages for
the popular distribution and providing them as a free download. In 2003, he
decidedtobuildanewdistribution, initiallybasedonMandrakeLinux,butwith
several significant usability improvements. The goals? It should be beginner-
friendly, have out-of-the box support for proprietary kernel modules, browser
plug-in andmedia codecs, and should function as a live CDwith a simple and
intuitive graphical installer. Several years and development releases later,
PCLinuxOS is rapidly approaching its intended state. In terms of usability, the
projectoffersout-of-the-boxsupport formanytechnologiesthatmostWindows-
to-Linuxmigrants would expect from a new operating system.On the software
sideofthings,PCLinuxOSisaKDE-orienteddistribution,withacustomizedand
always up-to-date version of the popular desktop environment. Its growing
softwarerepositorycontainsotherdesktopsandoffersagreatvarietyofdesktop
packages for many common tasks. For system configuration, PCLinuxOS has
retainedmuchofMandriva’sexcellentControlCentrebuthasreplaceditspackage
management system with APT and Synaptic, a graphical package management
front-end.Onthenegativeside,PCLinuxOSlacksanyformofroadmaporrelease
goals.Despitegrowingcommunityinvolvement,mostdevelopmentanddecision-
making remains in the hands of Texstar, who tends to be conservative when
judging the stability of a release. As a result, the development process of
PCLinuxOS is often arduous. For example, despite frequent calls for a 64-bit
edition, the developers held off producing a 64-bit build until fairly recently.
Furthermore,theprojectdoesnotprovideanysecurityadvisories,relyinginstead
ontheusers’willingnesstokeeptheirsystemup-to-dateviatheincludedpackage
managementtools.
Pros:Out-of-the-boxsupport forgraphicsdrivers,browserplug-ins,andmedia
codecs;rolling-releaseupdatemechanism;up-to-datesoftware.
Cons: no out-of-the-box support for non-English languages; lacks release
planningandsecurityadvisories.
SlackwareLinux
Slackware Linux, created by Patrick Volkerding in 1992, is the oldest
survivingLinux distribution. Separated from the now-discontinued SLSproject,
Slackware 1.0 came on 24 floppy disks and was built on top of Linux kernel
version 0.99pl11-alpha. It quickly became themost popular Linux distribution;
with some estimates putting its market share as much as 80% of all Linux
installationsin1995.ItspopularitydecreaseddramaticallywiththearrivalofRed
Hat Linux and other, user-friendlier distributions, but Slackware Linux still
remains a much-appreciated operating system among the more technically
oriented system administrators and desktop users. Slackware Linux is a highly
technical, clean distribution, with only a limited number of custom utilities. It
usesasimple,text-basedsysteminstallerandacomparativelyprimitivepackage
management system that does not resolve software dependencies. As a result,
Slackwareisconsideredoneofthecleanestandleastbuggydistributionsavailable
today-thelackofSlackware-specificenhancementsreducesthelikelihoodofnew
bugs being introduced into the system. The entire system configuration is
completedbyediting text files.There isasaying in theLinuxcommunity that if
youlearnRedHat,you’llknowRedHat,but ifyoulearnSlackware,you’llknow
Linux.This isparticularly true todaywhenmanyotherLinuxdistributionskeep
developingheavilycustomizedproductstomeettheneedsoflesstechnicalLinux
users.While thisphilosophyof simplicityhas its fans, the fact is that in today’s
world,SlackwareLinuxisincreasinglybecominga“coresystem”uponwhichnew,
customsolutionsarebuilt,ratherthanacompletedistributionwithawidevariety
ofsupportedsoftware.Theonlyexceptionistheservermarket,whereSlackware
remainspopular,thoughevenhere,thedistribution’scomplexupgradeprocedure
and lack of officially supported automated tools for security updates make it
increasingly uncompetitive. Slackware’s conservative attitude towards the
system’sbasecomponentsmeans that it requiresmuchmanualpost-installation
workbeforeitcanbeturnedintoamoderndesktopsystem.
Pros: Consideredhighly stable, clean and largely bug-free, strong adherence to
UNIXprinciples.
Cons:Limitednumberofofficiallysupportedapplications;conservativeinterms
ofbasepackageselection;complexupgradeprocedure.
FreeBSD
FreeBSD,an indirectdescendantofAT&TUNIXvia theBerkeleySoftware
Distribution(BSD),hasalongandturbulenthistorydatingbackto1993.Unlike
Linuxdistributions,whicharedefinedasintegratedsoftwaresolutionsconsisting
of theLinuxkernelandthousandsofsoftwareapplications,FreeBSDisa tightly
integratedoperatingsystembuiltfromaBSDkernelandtheso-called“userland”
(thereforeusableevenwithoutextraapplications).Thisdistinctionis largely lost
onceinstalledontheaveragecomputersystem-likemanyLinuxdistributions,a
largecollectionofeasilyinstalled,(mostly)opensourceapplicationsareavailable
for extending the FreeBSD core, but these are usually provided by third-party
contributors and aren’t strictly part of FreeBSD. FreeBSD has developed a
reputation for being a fast, high-performance and extremely stable operating
system, especially suitable for web serving and similar tasks. Many large web
searchenginesandorganizationswithmission-criticalcomputinginfrastructures
havedeployedandusedFreeBSDontheircomputersystemsforyears.Compared
to Linux, FreeBSD is distributed under a much less restrictive license, which
allows virtually unrestricted use and modification of the source code for any
purpose.EvenApple’sMacOSX isknown tohavebeenderived fromFreeBSD.
Besidesthecoreoperatingsystem,theprojectalsoprovidesover24,000software
applications in binary and source code forms for easy installation on top of the
core FreeBSD. While FreeBSD can certainly be used as a desktop operating
system, although it doesnot comparewell tomorepopularLinuxdistributions.
The text-mode system installer offers little in terms of hardware detection or
system configuration, leaving much of the dirty work to the user in a post-
installationsetup. In termsof support formodernhardware,FreeBSDgenerally
lags behind Linux, especially in supporting cutting-edge desktop and laptop
gadgets,suchaswirelessnetworkcardsordigitalcameras.Thoseusersseekingto
exploit the speed and stability of FreeBSD on a desktop or workstation should
consider one of the available desktop FreeBSD projects, rather than FreeBSD
itself.
Pros: Fast and stable; availability of over 24,000 software applications (or
“ports”)forinstallation;verygooddocumentation.
Cons: Tends to lag behind Linux in terms of support for new and exotic
hardware, limited availability of commercial applications; lacks graphical
configurationtools.
ChapterThree:LICENSING
CodeiscontributedtotheLinuxkernelunderanumberof licenses,butall
codemustbecompatiblewithversion2oftheGNU(GPLv2),whichisthelicense
coveringthekerneldistributionasawhole.Inpractice, thatmeansthatallcode
contributions are covered either by GPLv2 (with, optionally, language allowing
distributionunderlaterversionsoftheGPL)orthethree-clauseBSDlicense.Any
contributions,whicharenotcoveredbyacompatiblelicense,willnotbeaccepted
into the kernel. Copyright assignments are not required (or requested) for code
contributed to the kernel. All code merged into the mainline kernel retains its
original ownership; as a result, the kernel now has thousands of owners. One
implicationofthisownershipstructureisthatanyattempttochangethelicensing
ofthekernelisdoomedtoalmostcertainfailure.Therearefewpracticalscenarios
where the agreement of all copyright holders could be obtained (or their code
removed from the kernel). Therefore, there is no prospect of a migration to
version 3 of the GPL in the foreseeable future. It is imperative that all code
contributedtothekernelbelegitimatelyfreesoftware.Forthatreason,codefrom
anonymous(orpseudonymous)contributorswillnotbeaccepted.
Allcontributorsarerequiredto“signoff”ontheircode,statingthatthecode
canbedistributedwiththekernelundertheGPL.Codewhichitsowner,hasnot
licensedas free software, orwhich risks creating copyright-relatedproblems for
the kernel (such as codewhich derives from reverse-engineering efforts lacking
proper safeguards) cannot be contributed. Questions about copyright-related
issues are common on Linux development mailing lists. Such questions will
normally receive no shortage of answers, but one should bear inmind that the
peopleansweringthosequestionsarenotlawyersandcannotprovidelegaladvice.
IfyouhavelegalquestionsrelatingtoLinuxsourcecode,thereisnosubstitutefor
talkingwithalawyerwhounderstandsthisfield.Relyingonanswersobtainedon
technicalmailinglistsisariskyaffair.
COMMUNITY
Linuxcommunitiescomeintwobasicforms:developeranduser.
OneofthemostcompellingfeaturesofLinuxisthatitisaccessibletodevelopers;
anybodywiththerequisiteskillscanimproveLinuxandinfluencethedirectionof
itsdevelopment.Proprietaryproductscannotofferthiskindofopenness,whichis
acharacteristicofthefreesoftwareprocess.Developercommunitiescanvolunteer
to maintain and support whole distributions, such as the Debian or Gentoo
Projects. Novell and Red Hat also support community-driven versions of their
products, openSUSE and Fedora, respectively. The improvements to these
communitydistrosarethenincorporatedintothecommercialserveranddesktop
products. The Linux kernel itself is primarily supported by its developer
community and is one of the largest and most active free software projects in
existence.Atypicalthree-monthkerneldevelopmentcyclecaninvolveover1000
developersworkingformorethan100differentcompanies(orfornocompanyat
all).
WiththegrowthofLinuxhascomeanincreaseinthenumberofdevelopers
(and companies) wishing to participate in its development. Hardware vendors
want to ensure that Linux supports their products well, making their products
attractive to Linux users. Embedded systems vendors, who use Linux as a
componentinanintegratedproduct,wantLinuxtobeascapableandwellsuited
tothetaskathandaspossible.Distributorsandothersoftwarevendorswhobase
theirproductsonLinuxhaveaclearinterestinthecapabilities,performance,and
reliability of the Linux kernel. Other developer communities focus on different
applications and environments that run on Linux, such as Firefox, OpenOffice,
GNOME, and KDE. End users, too, can make valuable contributions to the
development of Linux. With online communities such as Linux.com,
LinuxQuestions, and themany and varied communities hosted by distributions
and applications, the Linux user base is often a very vocal, but usually positive
advocateandguidefortheLinuxoperatingsystem.TheLinuxcommunityisnot
justaonlinepresence.LocalgroupsknownasLinuxUsersGroups(LUGs)often
meet to discuss issues regarding theLinux operating system, andprovide other
localuserswithfreedemonstrations,training,technicalsupport,andinstall-fests.
DEVELOPMENT
Linux is an operating system comprised of many different development
languages.Avery largepercentageof thedistributions’ code iswritten in either
theC (52.86%)orC++ (25.56%) languages.All of the restof the code falls into
single-digit percentages, with Java, Perl, and Lisp rounding out the top 5
languages.TheLinuxkernel itselfhasanevenmoredominantCpresence,with
over 95 percent of the kernel’s code written in that language. However, other
languagesmakeupthekernelaswell,makingitmoreheterogeneousthanother
operating systems. The kernel community has evolved its own distinct way of
operating which allows it to function smoothly (and produce a high-quality
product) inanenvironmentwherethousandsof linesofcodearebeingchanged
everyday.ThismeanstheLinuxkerneldevelopmentprocessdiffersgreatlyfrom
proprietarydevelopmentmethods.
The kernel’s development process may come across as strange and
intimidating tonewdevelopers,but therearegoodreasonsandsolidexperience
behindit.Adeveloperwhodoesnotunderstandthekernelcommunity’sways(or,
worse,who tries to flout or circumvent them)will becomevery frustrated .The
developmentcommunity,whilebeinghelpfultothosewhoaretryingtolearn,has
little time for those who will not listen or who does not care about the
developmentprocess.WhilemanyLinuxdevelopers,stillusetext-basedtoolssuch
asEmacsorVimtodeveloptheircode,Eclipse,Anjuta,andNetbeansallprovide
morerobustintegrateddevelopmentenvironments.
ChapterFour:INSTALLINGDEBIAN8
WhatisDebian
Debianisanall-volunteerorganizationdedicatedtodevelopingfreesoftware
and promoting the ideals of the Free Software community. The Debian Project
began in 1993, when Ian Murdock issued an open invitation to software
developers tocontribute toacompleteandcoherentsoftwaredistributionbased
on the relatively new Linux kernel. That relatively small band of dedicated
enthusiasts,originallyfundedbytheFreeSoftwareFoundationandinfluencedby
theGNU philosophy, has grown over the years into an organization of around
1026DebianDevelopers.
DebianGNU/Linux
The combination of Debian’s philosophy, methodology, GNU tools, the
Linux kernel, and other important free software, form a unique software
distribution called Debian GNU/Linux. This distribution is made up of a large
number of software packages. Each package in the distribution contains
executables, scripts, documentation, and configuration information, and has
amaintainer who is primarily responsible for keeping the package up-to-date,
tracking bug reports, and communicating with the upstream author(s) of the
packagedsoftware.Theextremelylargeuserbase,combinedwithabugtracking
systemensures thatproblemsare foundand fixedquickly.Debian’sattention to
detail allows them to produce a high-quality, stable, and scalable distribution.
Installations can be easily configured to servemany roles, from stripped-down
firewallstoscientificdesktopworkstationstohigh-endnetworkservers.Debianis
especiallypopularamongstadvancedusersbecauseofitstechnicalexcellenceand
its deep commitment to the needs and expectations of the Linux community.
Debianalso introducedmany features toLinux that arenowcommonplace.For
example, Debian was the first Linux distribution to include a package
managementsystemfortheeasyinstallationandremovalofsoftware.Itwasalso
the first Linux distribution that could be upgraded without requiring re-
installation. Debian continues to be a leader in Linux development. Its
developmentprocessisanexampleofjusthowwelltheOpenSourcedevelopment
modelcanwork—evenforverycomplextaskssuchasbuildingandmaintaininga
completeoperatingsystem.
ThefeaturethatmostdistinguishesDebianfromotherLinuxdistributionsis
itspackagemanagementsystem.ThesetoolsgivestheadministratorofaDebian
systemcompletecontroloverthepackagesinstalled,includingtheabilitytoinstall
asinglepackageorautomaticallyupdate theentireoperatingsystem.Individual
packagescanalsobeprotectedfrombeingupdated.Youcaneventellthepackage
management system about software you have compiled yourself and what
dependenciesitfulfills.Toprotectyoursystemagainst“Trojanhorses”andother
malevolent software,Debian’s servers verify that uploaded packages come from
registeredDebianmaintainers.Debianpackagersalsotakegreatcaretoconfigure
theirpackagesinasecuremanner.Whensecurityproblemsinshippedpackages
doappear,fixesareusuallyavailableveryquickly.
InstallingDebian8asaVirtualMachine.
First,youwillneedtodownloadandinstallVirtualBox,theOraclesoftware
thatallowsyoutorunanyOSon…anyotherOS,asavirtualenvironment.This
procedureisalotsaferthanadualboot:noneedtofoolaroundwithbootsector
ordiskpartitions,allisvirtuallycreated.ThecounterpartisthatthetwoOS’s
shareyourcomputerresources.Toavoidanyperformanceissue,wewillchoosea
lightweightbutefficientdesktopenvironment.ThisbookwillusetheXFCE
Debianedition,thatyoucandownloadusingthislinkbelow:
https://www.debian.org/CD/torrent-cd/
XFCEisnotthenewestdesktopenvironment,howeveritisamongthemost
powerful, lightweight, and customizable,while being easy to usewith its classic
desktopmetaphor.ImagineanimprovedversionofWindowsXP,agreatwayto
getstartedwithLinux.
CreateyourVirtualSystem
Once installed, startVirtualBox and follow these steps.Wewill begin the
VMconfiguration:
New>Name,Type(debian),Version64
General>Advanced>ActivateclipboardandDrag’n’drop
System>checkmemory,deactivateFloppy,checkthenbofCPU(2)
Display>maxvideomemory,enable3Dacceleration
Storage>selectyourDebianISOunder“ControllerIDE”
SharedFolders>choosewhichfoldertosharebetweenthesystems.We
willgetbacktothisfunctionlater.
Wearenowreadytorunthedistributioninstallation.
RunDebianinstaller
Selectyourvirtualmachineandclickthe‘Start’icon.TheDebiangraphicinstallerisstraightforward:justfollowthesteps,whenaskedenterarootpassword;createyouruser(name,username,andpassword).Onceyougettothepartitioning,select‘Guided,useentiredisk,allinonepartition.’Ifyouwishtomodifythesizeoftheswappartition,youmayselectthemanualpartitioning,orproceedlaterwithParted,thepartitioningutilitytool.
Proxy,part#1:selectyournetworkmirror.Ifyouneedtoconfigureaproxy,usethefollowingsyntaxintherequiredfield:http://user:pass@host:port/.
Proceedtotheinstallation.Onceithasfinished,youwillhavetochoosewheretoinstallGrub(thebootloaderpackage):becausethisisaVMinstall,choose/dev/sda(ata-VBOX_HARDDISK).Thiswouldbethetrickypartifyouwereinstallingonadual-boot,systemsoenjoythecomfortofaVM!
NowreboottheVM.
FirstBootandUpdates
AtstartuptheGrubmenuisdisplayed,stickwiththedefaultentryandwaitforyoursystemtoinitialize.AtXFCEfirststart,youwillbeaskedtochoosebetweentwopanelsetups,selectthedefaultconfig.
Check your time configuration. If incorrect, launch as root a dpkg-
reconfiguretzdataandselectyourcountry.
Proxy, part #2 (if needed): during install, you entered your proxy addressforaptconnection(repositoriesconnection).Itishasbeenwritteninsidethefile/etc/apt/apt.conf, and can bemodified any time. I usenanoas command linetexteditor,changeatwill.
Nowaddtheproxysettingfortheentiresystem:
Congratulations, youarenowa confirmedLinuxuser!Nowwecanupdate
andinstallsomeuseful tools, thenwewillmodifythedefaultUI.Tousethefull
capacity of your VM you need to install a complementary VirtualBox extension
calledGuestAdditions.Todoso,somedependenciesarerequired.
Checkyoursources,removetheCDentries,addthecontribandnon-freerepos:
Updateyoursystem:
InstalltheGuestAdditionsrequirements
InserttheGuestAdditionsCDimage(Host+D),gototherootofthemounteddriveandexecutethe
program:
CustomizingyourSystem…togetsomethingthatlooksalittlemoremodern.
Terminal
TheterminalisoneofthemostusefultoolsonaLinuxsystem.Spendsometime
making it pleasant to look at: enable your prompt color ink .bashrc, add your
aliases,andenablecolorsandautocompletioninrootbash.
uncommentthe“force_colored_prompt=yes”line
[root]$nano.bashrc
#setafancyredprompt
PS1=’${debian_chroot:+($debian_chroot)}\[33[01;31m\]\u@\h\[33[00m\]:\[33[01;34m\]\w\[33[00m\]\$‘
#enableauto-completion
if
[-f/etc/bash_completion]&&!shopt-oqposix;
then./etc/bash_completion
fi
Fonts
Enablefonthinting intheAppearancePanel togetthebest fontrenderingfor
your system. Settings > Appearance > Fonts > Enable anti-aliasing + Hinting
Slight+Sub-pixelorderRGB.ThisshouldbeenoughinJessie,noneedtocreatea
fonts.conffileorinstalltheInfinalityengine.
IThemesandicons
GTK Themes, windows decoration and iconsets are the most important
elementstocustomizingyourdesktop.Startwithinstallingsomecomplementary
gtkenginesthatareneededbysomethemes:
[root]$apt-getinstallgtk2-engines-murrinegtk2-engines-pixbufdmz-cursor-theme
Installsudo
Debiandoesn’tcomewithsudooutofthebox,thisisahandylittletoolfor
temporarilygivingyourootaccess.
Replace USERNAME with your username. The NOPASSWD flag removes the
requirementtoenterapasswordeverytimeyouusesudo.Thisisnotagoodidea
onaproductionserver!
Rebootyourmachinetomakethechangestakeeffect:$reboot
Fromnowonalloperationsthatrequirerootshouldberunwiththesudoprefix,
withouthavingtoenterthepasswordeachtime.
Setupthenetwork
LogintoyournewVMusingtheusernameandpasswordyouchoseduring
installation.We’regoingtoaddanetworkconnectiontoyourVMthatwillallow
youtooeasilySSHintotheserver.
HitCTRL+X,thenYandENTERtosavechanges.ShutdowntheVM.
AtthemainVirtualBoxscreen,hittheSettingsbutton.ThenselectNetworkfromthelist on the left, chooseAdapter 2 from the tabs, check ‘Enable Network Adapter’ andchoose‘Host-onlyAdapter’fromthedropdown,thenclickOK.
WhatwehavedoneissetupyourVMtouseastaticIPaddress.Thisisagoodidea
becauseitallowsustoalwaysaccessourVMusingasingleIPaddress/hostname
withouthavingtolookitupeachtimeweboot.
Bydefault,VirtualBoxutilizes the 192.168.56.1 address in yournetwork, and itassignsIPaddressesinthe192.168.56.1xxrangetoallyourVMs.
Byediting/etc/network/interfaceswetoldtheOSthatitshouldexpectanetworkresourcetobeavailableatthataddress.
Setupyourhostsfile
Nowyourserverisconfiguredletsaddthehostnametoyourhostsfile!
Simplyaddthefollowingentry intoyourhostsfile.Thisshouldbedoneonyour
hostmachine–beitWindowsorMacOSXorLinux,nottheVMitself.
KeepinmindthatforeverydomainyousetuponyourVM,youwillneedtoaddit
toyourhostsfile.
LoginviaSSH!
NowthatyouhavesetupthenetworkadapterinVirtualBox,andaddedthe
correct settings to theVM interfaces file, you’re ready toactuallySSH intoyour
server andbegin installing everything!Youmaybewonderingwhy youneed to
SSHandnotsimplyuse theVMwindow.ThereasonIdo it thisway is that the
serverdoesnotsupportcopy/paste!There’salotoftypingaheadandhavingthe
abilitytosimplycopy/pasteintoyourterminalisgoingtospeedthingsupquitea
bit!
ForSSHonWindows,IuseKiTTy.It’sanSSHclientthataddssomeniftyfeatures
to PuTTY. There is also Poderosa. For Mac/Linux just use the terminal! Since
you’vealreadyaddedthecorrectlinestoyourhostsfile,youcansettheaddressto
connect to as debian-vm (or whatever you chose during setup). Make sure to
actually start the VM from VirtualBox before attempting to login. Just start it,
thereisnoneedtologinfromtheVirtualBoxserverwindow.
Installingthebasics
Firstthingsfirst,we’regoingtoinstallthebasicnecessities,likemake,curl,
wget,aswellastheApacheServer,Mercurial,GitandSubversion:
EditthenewApache2configfile,$sudonano/etc/apache2/httpd.confandadd
YounowhavetheApacheserverupandrunning!Justpointyourbrowserto
http://debian-vmandbeholdthemagic.
NowletsenableApache’sModRewritemodule:
InstallingMySQL
WewillbeinstallingMySQL5.1fromrepo,asthisistheeasiestwayandworksfairlywell.
On the screens asking for aMySQL password, leave it blank and hit Enter.Sincethisisonlyforalocalserverthereisnopointinsettingupapassword.DoNOTuseablankpasswordinproductionenvironments.
SettingupMySQL
WeneedtoupdatetheIPaddressthatMySQLwilllistentoforconnectionsby
editingthemy.cnffile.
$sudonano/etc/mysql/my.cnf
Doasearchforbind-address(CTRL+W)andchangethesettingto:
Nowlet’sgrantrootMySQLuserallpermissions:
ChapterFive:INSTALLINGCENTOS7
This is a community-supported distribution derived from sources freely
providedtothepublicbyRedHatforRedHatEnterpriseLinux(RHEL).Assuch,
CentOS Linux aims to be functionally and compatible with RHEL. The CentOS
Project mainly changes packages to remove upstream vendor branding and
artwork. CentOS Linux is free and free to redistribute. Each CentOS version is
maintainedforupto10years(bymeansofsecurityupdates—thedurationofthe
support interval by Red Hat has varied over time with respect to Sources
released).AnewCentOSversionisreleasedapproximatelyevery2yearsandeach
CentOSversionisperiodicallyupdated(roughlyevery6months)tosupportnewer
hardware. This results in a secure, low-maintenance, reliable, predictable and
reproducibleLinuxenvironment.
InstallationofCenOS7
Finally,themuch-awaitedCentOS7isout.CentOS(CommunityEnterprise
OperatingSystem)areforkedfromRedHatLinux,aLinuxDistrofinetunedfor
servers.YouwilllearnhowtoinstallCentos7inafeweasysteps.
Step1:DownloadtheISOImage
TogetacopyofCentOS7downloadfromthelinkbelow:
http://mirror.centos.org/centos/7/
CentOS7 isonlyavailable for64-bitplatforms; currently there isno32-bit ISO
image.Thisisprimarilyduetothefactthatmostproductionserversare64-bit.
Step2:MakeabootableDrive
AfteryoudownloadtheISOimage,makeabootableUSBdriveusingUnetbootin.
Alternatively, you can burn a DVD using Brasero or your favorite CD/DVD
burningsoftware.
Step3:BeginInstallation
Tobegininstallation,clickontheInstalltoHardDriveicononthedesktop.
Step4:SelectLanguageandKeyboard
Selectyourpreferredlanguage.
Step5:ChangetheInstallationDestination
By default, the Anaconda installer will choose automatic partitioning foryour hard disk. Click on the INSTALLATION DESTINATION icon to change to custompartitioning.
Select theharddrivewhere youwant to installCentOS7 andunderOther StorageOptions,choose‘Iwillconfigurepartitioning’thenclickDone.
Step6:SelectthePartitioningScheme
Next, select the partitioning scheme to use for the mountpoints. In this case,chooseStandardPartition.
Step7:CreateaSwapSpace
You can create a swap space in one of the partitions and set the desiredcapacity,whichisdependentonhowmuchRAM,thesystemhas.ChoosetheFileSystem for swap spaceas swap, and select theReformat option.Youcanalsonameyour swap space to whatever name you like but a name like swap is moredescriptive.
Step8:CreateaMountpoint
The next step is to create themountpointwhere the root partitionwill be
installed.Dependingonyourrequirements,youmightneedtoputtheboot,home
androotpartitionsondifferentmountpoints.Inthisinstance,weshallonlycreate
onemountpoint/.
Next,settheLabelandDesiredCapacitytowhateveryouwish.Aruleofthumbistousedescriptive names for the label especially if the computer is to bemanaged bydifferent system administrators. Choose the file system as ext4 and select theReformatoption.
Step9:AcceptChanges
After Steps 7 and 8 have successfully completed click on Done button. Apromptwindowwillappearwithasummaryofthechangesthatwilltakeplace.IfyouaresatisfiedwiththemclickAcceptChanges.
Step10:SetDateandTime
Click on theClock icon under the localizationmenu and select your time zonefromthemapoftheworld,thenclickDone.
Step11:BeginInstallation
ClickontheBeginInstallationbutton.
Installationwill begin immediately and as it proceeds youwill need to set up auseraccountaswellastherootpassword.
Set
Step12:SetUpRootPassword
ClickontheROOTPASSWORDoptionandenterapasswordandconfirmationofthesamethenclickDone.
Step13:CreateaUserAccount
Thenextstep is tocreateauseraccount.Enter thedetailsand if this isan
administrator account, check Make this user administrator and Require apasswordtousethisaccountforsecuritypurposes.
Step14:CompleteInstallation
Theinstallershouldfinishinstallingthesoftwareandbootloader.
Hopefully,oncetheinstalliscompleteyouwillgetasuccessmessage,afterwhich
you can click Quit. Now logout from the live system and login to your newinstallation.Finally,onceyoulogintoyourCentOS7accepttheEULAagreement
andenjoy!
ChangeandSetHostnameCommand
OnaCentOSLinux7server,youcanuseanyoneofthefollowingtoolstomanage
hostnames:
hostnamectl:Controlthesystemhostname.Thisistherecommended
method.
nmtui:Controlthesystemhostnameusingtextuserinterface(TUI).
nmcli:ControlthesystemhostnameusingCLIpartofNetworkManager.
Method#1:hostnamectl
Letusseehowtousethehostnamectlcommand.
HowdoIseethehostnames?
Sampleoutputs:Statichostname:server1.cyberciti.biz
Prettyhostname:SenatorPadméAmidala’sLaptop
Transienthostname:r2-d2
Iconname:computer
Chassis:n/a
MachineID:b5470b10ccfd49ed8e4a3b0e953a53c3
BootID:f79de79e2dac4670bddfe528e826b61f
Virtualization:oracle
OperatingSystem:CentOSLinux7(Core)
CPEOSName:cpe:/o:centos:centos:7
Kernel:Linux3.10.0-229.1.2.el7.x86_64
Architecture:x86_64
HowdoIdeleteaparticularhostname?
Thesyntaxis:#hostnamectlset-hostname””
#hostnamectlset-hostname””—static
#hostnamectlset-hostname””–pretty
HowdoIchangehostnameremotely?
Useanyofthefollowingsyntax:#sshroot@server-ip-herehostnamectlset-hostnameserver1
ORsetserver1ashostnameonaremoteservercalled192.168.1.42usingssh:#[email protected]
Method#2:nmtui
Youcansethostnameusingthenmtuicommand,whichhastextuserinterfacefornewusers:#nmtui
Sampleoutputs:
UsetheDownarrowkey>selectthe‘Setsystemhostname’menuoption>Click
theOKbutton:
Youwillseetheconfirmationboxasfollows:
Finally,restartthehostnamedservicebytypingthefollowingcommand:
#systemctlrestartsystemd-hostnamed
Toverifythechanges,enter:
#hostnamectlstatus
Method#3:nmcli
Thenmcliisacommand‐linetoolforcontrollingNetworkManagerandreportingnetworkstatus.
Toviewthehostnameusingnmcli:
Thesyntaxis:#nmcligeneralhostname
Tosetthehostnameusingnmcli:
Thesyntaxis:#nmcligeneralhostnameR2-D2
#nmcligeneralhostnameserver42.cyberciti.biz
Finally,restartthesystemd-hostnamedservice:#systemctlrestartsystemd-hostnamed
Chapter Six: LINUX AND UNIXMANCOMMAND
OnLinuxandotherUnix-likeoperatingsystems,manistheinterfaceusedtoview
thesystem’sreferencemanuals.
Syntaxman[-Cfile][-d][-D][—warnings[=warnings]][-Rencoding][-Llocale]
[-msystem[,…]][-Mpath][-Slist][-eextension][-i|-I]
[—regex|—wildcard][—names-only][-a][-u][—no-subpages][-Ppager]
[-rprompt][-7][-Eencoding][—no-hyphenation][—no-justification]
[-pstring][-t][-T[device]][-H[browser]][-X[dpi]][-Z]
[[section]page…]…
man-k[aproposoptions]regexp…
man-K[-w|-W][-Slist][-i|-I][—regex][section]term…
man-f[whatisoptions]page…
man-l[-Cfile][-d][-D][—warnings[=warnings]][-Rencoding]
[-Llocale][-Ppager][-rprompt][-7][-Eencoding][-pstring][-t]
[-T[device]][-H[browser]][-X[dpi]][-Z]file…
man-w|-W[-Cfile][-d][-D]page…
man-c[-Cfile][-d][-D]page…
man[-hV]
Description
Manisthesystem’smanualviewer;itcanbeusedtodisplaymanualpages,
scroll up and down, search for occurrences of specific text, and other useful
functions.Eachargumentgiventomanisnormallythenameofaprogram,utility
or function. The pages associatedwith each of these arguments are then found
anddisplayed.Asectionnumber,ifprovided,willdirectmantolookonlyinthat
section of the manual. The default action is to search in all of the available
sections,followingapre-definedorderandtoshowonlythefirstpagefound,even
ifpagesexistinseveralsections.
GeneralOptions
-h,—help Printahelpmessageandexit.
-V,—version Displayversioninformationandexit.
-Cfile,—config- Useconfigurationfilefileratherthanthe
file=file defaultof~/.manpath.
-d,—debug Printdebugginginformation.
-D,—default Thisoption,whenused,isnormally
specifiedasthefirstoption;it
resetsman‘sbehaviourtoitsdefault.Its
useistoresetthoseoptionsthatmayhave
beensetin$MANOPT.Anyoptionsthat
follow-Dwillhavetheirusualeffect.
—
warnings[=warnings]
Enablewarningsfromthegrofftext
formatter.Thismaybeusedtoperform
sanitychecksonthesourcetextofmanual
pages.warningsisacomma-separated
listofwarningnames;ifitisnotsupplied,
thedefaultis“mac“.Seethe“Warnings”
nodeinthegroffinfopageforalistof
availablewarningnames.
MainModesofOperation
-f,—whatis Equivalenttothewhatiscommand;displaysashortdescriptionfromthemanualpage,ifavailable.
-k,—apropos Equivalenttotheaproposcommand;Searchtheshortmanualpagedescriptionsforkeywordsanddisplayanymatches.
-K,—global-apropos
Searchfortextinallmanualpages.Thisisabrute-forcesearch,andislikelytotakesometime;ifyoucan,youshouldspecifyasectiontoreducethenumberofpagesthatneedtobesearched.Searchtermsmaybesimplestrings(thedefault),orregularexpressionsifthe—regexoptionisused.
-l,—local-file Activate‘local’mode.Formatanddisplaylocalmanualfilesinsteadofsearchingthroughthesystem’smanualcollection.Eachmanualpageargumentwillbeinterpretedasannroffsourcefileinthecorrectformat.Nocatfileisproduced.Ifadash(‘-‘)islistedasoneofthearguments,inputwillbetakenfromstdin.Whenthisoptionisnotused,andmanfailstofindthepagerequired,beforedisplayingtheerrormessageitattemptstoactasifthisoptionwassupplied,usingthenameasafilenameandlookingforanexactmatch.
-w,—where,—location
Don’tactuallydisplaythemanualpages;insteadprintthelocation(s)ofthesourcenrofffilesthatwouldbeformatted.
-W,—where-cat,—location-cat
Don’tactuallydisplaythemanualpages,butdoprintthelocation(s)ofthecatfilesthatwouldbedisplayed.If-wand-Warebothspecified,printboth,separatedbyaspace.
-c,—catman Thisoptionisnotforgeneraluseandshouldonlybeusedbythecatmanprogram.
-Rencoding,—recode=encoding
Insteadofformattingthemanualpageintheusualway,outputitssourceconvertedtothespecifiedencoding.Ifyoualreadyknowtheencodingofthesourcefile,youcanalsousemanconvdirectly.However,thisoptionallowsyoutoconvertseveralmanualpagestoasingleencodingwithouthavingtoexplicitlystatetheencodingofeach,providedthattheywerealreadyinstalledinastructuresimilartoamanualpagehierarchy.
FindingManualPages
-Llocale,—
locale=locale
manwillnormallydetermineyour
currentlocalebyacalltotheC
functionsetlocalewhichchecksthe
valuesofvariousenvironment
variables,possibly
including$LC_MESSAGESand$LANG.
Totemporarilyoverridethe
determinedvalue,usethisoptionto
supplyalocalestringdirectlytoman.
Notethatitwillnottakeeffectuntilthe
searchforpagesactuallybegins.
Outputsuchasthehelpmessagewill
alwaysbedisplayedintheinitially
determinedlocale.
-msystem[,…],—
systems=system[,
…]
Ifthissystemhasaccesstootheroperating
system’smanualpages,theycanbeaccessed
usingthisoption.Tosearchforamanualpage
from(forexample)the“NewOS”manualpage
collection,usetheoption-mNewOS.
Thesystemspecifiedcanbeacombinationof
commadelimitedoperatingsystemnames.To
includeasearchofthenativeoperating
system’smanualpages,includethesystem
namemanintheargumentstring.This
optionwilloverride
the$SYSTEMenvironmentvariable.
-Mpath,—
manpath=path
Specifyanalternatemanpathtouse.This
optionoverridesthe$MANPATH
environmentvariableandcausesoption-mto
beignored.
Apathspecifiedasamanpathmustbethe
rootofamanualpagehierarchystructured
intosectionsasdescribedintheman-db
manual(under“Themanualpagesystem”).
Toviewmanualpagesoutsidesuch
hierarchies,seethe-loption.
-Slist,-slist,—
sections=list
listisacolon-orcomma-separatedlistof
`orderspecific’manualsectionstosearch.
Thisoptionoverrides
the$MANSECTenvironmentvariable.
(The–sspellingisforcompatibility
withSystemV.)
-esub-
extension,—
extension=sub-
extension
Somesystemsincorporatelargepackagesof
manualpages,suchasthosethataccompany
theTclpackage,intothemainmanualpage
hierarchy.Togetaroundtheproblemof
havingtwomanualpageswiththesamename
suchasexit,theTclpageswereusuallyall
assignedtosectionl(lowercaseL).However,
itisnowpossibletoputthepagesinthe
correctsection,andtoassignaspecific
“extension”tothem,inthiscase,exit(3tcl).
Undernormaloperation,manwill
displayexitinpreferencetoexit(3tcl).To
negotiatethissituationandtoavoidhavingto
knowwhichsectionthepageyourequire
residesin,itisnowpossibletogivemana
sub-extensionstringindicatingwhichpackage
thepagemustbelongto.Usingtheabove
example,supplyingtheoption-e
tcltomanwillrestrictthesearchtopages
havinganextensionof*tcl.
-i,—ignore-case Ignorecasewhensearchingformanualpages.
Thisisthedefault.
-I,—match-case Searchformanualpagescase-sensitively.
—regex Showallpageswithanypartofeithertheir
namesortheirdescriptionsmatchingeach
pageargumentasaregularexpression,as
withapropos.Sincethereisusuallyno
reasonablewaytopicka“best”pagewhen
searchingforaregularexpression,thisoption
implies-a.
—wildcard Showallpageswithanypartofeithertheir
namesortheirdescriptionsmatchingeach
pageargumentusingshell-stylewildcards,as
withapropos—wildcard.Thepage
argumentmustmatchtheentirenameor
description,ormatchonwordboundariesin
thedescription.Sincethereisusuallyno
reasonablewaytopicka“best”pagewhen
searchingforawildcard,thisoptionimplies-
a.
—names-only Ifthe—regexor—wildcardoptionisused,
matchonlypagenames,notpage
descriptions,aswithwhatis.Otherwise,this
optionhasnoeffect.
-a,—all Bydefault,manwillexitafterdisplayingthe
mostsuitablemanualpageitfinds.Usingthis
optionforcesmantodisplayallthemanual
pageswithnamesthatmatchthesearch
criteria.
-u,—update Thisoptioncausesmantoperformaninode-
levelconsistencycheckonitsdatabasecaches
toensurethattheyareanaccurate
representationofthefilesystem.Itwillonly
haveausefuleffectifmanisinstalledwith
thesetuidbitset.
—no-subpages Bydefault,manwilltrytointerpretpairsof
manualpagenamesgivenonthecommand
lineasequivalenttoasinglemanualpage
namecontainingahyphenoranunderscore.
Thissupportsthecommonpatternof
programsthatimplementanumberof
subcommands,allowingthemtoprovide
manualpagesforeachthatcanbeaccessed
usingsimilarsyntaxaswouldbeusedto
invokethesubcommandsthemselves.For
example,thecommand:
man-awgitdiff
displaysthemanualpage:
/usr/share/man/man1/git-diff.1.gz
Todisablethisbehavior,usethe—no-
subpagesoption.
Forexample:
man-aw—no-subpagesgitdiff
Willinsteadshowthemanualpagesfor
bothgitanddiff:
/usr/share/man/man1/git.1.gz
/usr/share/man/man3/Git.3pm.gz
/usr/share/man/man1/diff.1.gz
ControllingFormattedOutput
-Ppager,—pager=pager
Specifywhichoutputpagertouse.Bydefault,manusespager-s.Thisoptionoverridesthe$MANPAGERenvironmentvariable,whichinturnoverridesthe$PAGERenvironmentvariable.Itisnotusedinconjunctionwith-for-k.
Thevaluemaybeasimplecommandnameoracommandwitharguments,andmayuseshellquoting(backslashes,singlequotes,ordoublequotes).Itmaynotusepipestoconnectmultiplecommands;ifyouneedthat,useawrapperscript,whichmaytakethefiletodisplayeitherasanargumentoronstandardinput.
-rprompt,—prompt=prompt
Ifarecentversionoflessisusedasthepager,manwillattempttosetitspromptand
somesensibleoptions.Thedefaultpromptlookslike:
Manualpagename(sec)linex
wherenamedenotesthemanualpagename,secdenotesthesectionitwasfoundunderandxthecurrentlinenumber.Thisisachievedbyusingthe$LESSenvironmentvariable.
Supplying-rwithastringwilloverridethisdefault.Thestringmaycontainthetext$MAN_PNwhichwillbeexpandedtothenameofthecurrentmanualpageanditssectionnamesurroundedby“(”and“)”.Thestringusedtoproducethedefaultcouldbeexpressedas;
\Manual\page\\$MAN_PN\?ltline\%lt?L/%L.:
byte\%bB?s/%s..?\(END):?pB\%pB\%..
(presshforhelporqtoquit)
Itisbrokenintothreelineshereforthesakeofreadabilityonly.Foritsmeaningseethemanpageforless.
Theshellfirstevaluatesthepromptstring.Alldoublequotes,back-quotesandbackslashesinthepromptmustbeescapedbyaprecedingbackslash.Thepromptstringmayendinanescaped$whichmaybefollowedbyfurtheroptionsforless.Bydefault,mansetsthe-ix8options.
Ifyouwanttooverrideman’spromptstringprocessingcompletely,usethe$MANLESSenvironmentvariabledescribedbelow.
-7,—ascii WhenviewingapureASCIImanualpageona7-bitterminalorterminalemulator,somecharactersmaynotdisplaycorrectlywhenusingthelatin1devicedescriptionwithGNUnroff.ThisoptionallowspureASCIImanpagestobedisplayedinASCIIwiththelatin1device.Itwillnottranslate
anylatin1text.Thefollowingtableshowsthetranslationsperformed:somepartsofitmayonlybedisplayedproperlywhenusingGNUnroff’slatin1device.Thisoptionisignoredwhenusingoptions-t,-H,-T,or-ZandmaybeuselessforversionsofnroffotherthanGNU’s.
-Eencoding,—encoding=encoding
Generateoutputforacharacterencodingotherthanthedefault.Forbackwardcompatibility,encodingmaybeannroffdevicesuchasascii,latin1,orutf8aswellasatruecharacterencodingsuchasUTF-8.
—no-hyphenation,—nh
Normally,nroffwillautomaticallyhyphenatetextatlinebreakseveninwordsthatdonotcontainhyphens,ifitisnecessarytodolayoutthrewordsonalinewithoutexcessivespacing.Thisoptiondisablesautomatichyphenation,sowordswillonlybehyphenatediftheyalreadycontainhyphens.Ifyouarewritingamanpageandsimplywanttopreventnrofffromhyphenatingawordataninappropriatepoint,donotusethisoption,butconsultthenroffdocumentationinstead;forinstance,youcanput“\%”insideawordtoindicatethatitmaybehyphenatedatthatpoint,orput“\%”atthestartofawordtopreventitfrombeinghyphenated.
—no-justification,—nj
Normally,nroffwillautomaticallyjustifytexttobothmargins.Thisoptiondisablesfulljustification,leavingjustifiedonlytotheleftmargin,sometimescalled“ragged-right”text.
Ifyouarewritingamanpageandsimplywanttopreventnrofffromjustifyingcertainparagraphs,donotusethisoption,butconsultthenroffdocumentation;forinstance,youcanusethe“.na”,“.nf”,“.fi”,and“.ad”requeststotemporarilydisableadjustingandfilling.
-pstring,—preprocessor=string
Specifythesequenceofpreprocessorstorunbeforenroffortroff/groff.Notallinstallationswillhaveafullsetofpreprocessors.Someofthepreprocessorsandthelettersusedtodesignatethemare:eqn(e),grap(g),pic(p),
tbl(t),vgrind(v),refer(r).Thisoptionoverridesthe$MANROFFSEQenvironmentvariable.zsoelimisalwaysrunasthefirstpreprocessor.
-t,—troff Usegroff-mandoctoformatthemanpagetostandardoutput.Thisoptionisnotrequiredinconjunctionwith-H,-T,or-Z.
-T[device],—troff-device[=device]
Thisoptionisusedtochangegroff(orpossiblytroff’s)outputtobesuitableforadeviceotherthanthedefault.Itimplies-t.Examplesincludedvi,latin1,ps,utf8,X75andX100.
-H[browser],—html[=browser]
ThisoptionwillcausegrofftoproduceHTMLoutput,andwilldisplaytheoutputinawebbrowser.Thechoiceofbrowserisdeterminedbytheoptionalbrowserargumentifoneisprovided,bythe$BROWSERenvironmentvariable,orbyacompile-timedefaultifthatisunset(usuallylynx).Thisoptionimplies-t,andwillonlyworkwithGNUtroff.
-X[dpi],—gxditview[=dpi]
Thisoptiondisplaystheoutputofgroffinagraphicalwindowusingthegxditviewprogram.Thedpi(dotsperinch)maybe75,75-12,100,or100-12,defaultingto75;the-12variantsusea12-pointbasefont.Thisoptionimplies-TwiththeX75,X75-12,X100,orX100-12devices,respectively.
-Z,—ditroff groffwillruntroffandthenuseanappropriatepost-processortoproduceoutputsuitableforthechosendevice.Ifgroff-mandocisgroff,thisoptionispassedtogroffandwillsuppresstheuseofapost-processor.Itimplies-t.
SectionNumbers
The section numbers of the manual are listed below. While reading
documentation,ifyouseeacommandnamefollowedbyanumberinparentheses,
the number refers to one of these sections. For example, man is the
documentation of man found in section number1. Some commands may have
documentation in more than one section, so the numbers after the command
namemaydirectyoutothecorrectsectiontofindaspecifictypeofinformation.
Thesectionnumbers,andthetopicstheycover,areasfollows:
section# Topic
1 Executableprogramsorshellcommands
2 Systemcalls(functionsprovidedbythekernel)
3 Librarycalls(functionswithinprogramlibraries)
4 Specialfiles(usuallyfoundin/dev)
5 Fileformatsandconventionseg/etc/passwd
6 Games
7 Miscellaneous(includingmacropackagesand
conventions),e.g.man,groff
8 Systemadministrationcommands(usuallyonly
forroot)
9 Kernelroutines[Nonstandard]
ExitStatus
Whenitterminates,manwillreturnoneofthefollowingexitstatus:
0 Returneduponsuccessfulprogramexecution.
1 Returnediftherewasausage,syntax,orconfigurationfile
error.
2 Returnediftherewasanoperationalerror.
3 Returnedifachildprocessreturnedanon-zeroexitstatus.
16 Returnedifoneormoreofthepages,files,orkeywords
searchedfordidnotexistorwasnotmatched.
Environment
manmakesuseofthefollowingenvironmentvariables:
MANPATH If$MANPATHisset,itsvalueis
usedasthepathtosearchfor
manualpages.
MANROFFOPT Thecontentsof$MANROFFOPTare
addedtothecommandlineevery
timemaninvokestheformatter
(nroff,troff,orgroff).
MANROFFSEQ If$MANROFFSEQisset,itsvalueis
usedtodeterminethesetof
preprocessorstopasseachmanual
pagethrough.Thedefault
preprocessorlistissystem-dependent.
MANSECT If$MANSECTisset,itsvalueisa
colon-delimitedlistofsectionsanditis
usedtodeterminewhichmansections
tosearchandinwhatorder.
MANPAGER,PAGER If$MANPAGERor$PAGERisset
($MANPAGERisusedin
preference),itsvalueisusedasthe
nameoftheprogramusedtodisplay
themanpage.Bydefault,pager-sis
used.Thevaluemaybeasimple
commandnameoracommandwith
arguments,andmayuseshellquoting
(backslashes,singlequotes,ordouble
quotes).Itmaynotusepipesto
connectmultiplecommands;ifyou
needthat,useawrapperscript,which
maytakethefiletodisplayeitherasan
argumentoronstandardinput.
MANLESS If$MANLESSisset,manwillnot
performanyofitsusualprocessingto
setupapromptstringfor
thelesspager.Instead,thevalueof
$MANLESSwillbecopiedverbatim
into$LESS.Forexample,ifyouwant
tosetthepromptstring
unconditionallyto“mypromptstring”,
set$MANLESSto‘-Psmyprompt
string’.
BROWSER If$BROWSERisset,itsvalueisa
colon-delimitedlistofcommands,each
ofwhichinturnisusedtotrytostarta
webbrowserforman—html.Ineach
command,%sisreplacedbya
filenamecontainingtheHTMLoutput
fromgroff,%%isreplacedbyasingle
percentsign(%),and%cisreplaced
byacolon(:).
SYSTEM If$SYSTEMisset,itwillhavethe
sameeffectasifithadbeenspecified
astheargumenttothe-moption.
MANOPT If$MANOPTisset,itwillbeparsed
priortomanscommandlineandis
expectedtobeinasimilarformat.As
alloftheothermanspecific
environmentvariablescanbe
expressedascommandlineoptions,
andarethuscandidatesforbeing
includedin$MANOPTitisexpected
thattheywillbecomeobsolete.Note:
allspacesthatshouldbeinterpretedas
partofanoption’sargumentmustbe
escaped(precededwithabackslash).
MANWIDTH If$MANWIDTHisset,itsvalueis
usedasthelinelengthforwhich
manualpagesshouldbeformatted.Ifit
isnotset,manualpageswillbe
formattedwithalinelength
appropriatetothecurrentterminal
(usinganioctlifavailable,thevalue
of$COLUMNS,orfallingbackto80
charactersifneitheris
available).catpageswillonlybesaved
whenthedefaultformattingcanbe
used,thatiswhentheterminalline
lengthisbetween66and80
characters.
MAN_KEEP_FORMATTING Normally,whenoutputisnotbeing
directedtoaterminal(suchastoafile
orapipe),formattingcharactersare
discardedtomakeiteasiertoreadthe
resultwithoutspecialtools.However,
if$MAN_KEEP_FORMATTINGis
settoanynon-emptyvalue,these
formattingcharactersareretained.
Thismaybeusefulforwrappers
aroundmanthatcaninterpret
formattingcharacters.
MAN_KEEP_STDERR Normally,whenoutputisbeing
directedtoaterminal(usuallya
pager),anyerroroutputfromthe
commandusedtoproduceformatted
versionsofmanpagesisdiscardedto
avoidinterferingwiththepager’s
display.Programssuchasgroffoften
producerelativelyminorerror
messagesabouttypographical
problemssuchaspooralignment,
whichareunsightlyandgenerally
confusingwhendisplayedalongwith
themanpage.However,youmight
wanttoseethemanyway,so
if$MAN_KEEP_STDERRissettoa
non-emptyvalue,erroroutputwillbe
displayedasusual.
LANG,LC_MESSAGES Dependingonthesystemand
implementation,eitherorboth
of$LANGand$LC_MESSAGESwill
beinterrogatedforthecurrent
messagelocale,manwilldisplayits
messagesinthatlocale(ifavailable).
Files
Thesefilesareusedbyman:
/etc/manpath.config Theman-
dbconfiguration
file.
/usr/share/man Aglobalmanual
pagehierarchy.
/usr/share/man/index.
(bt|db|dir|pag)
Atraditionalglobal
indexdatabase
cache.
/var/cache/man/index.
(bt|db|dir|pag)
AnFHScompliant
globalindex
databasecache.
Examples
manman
Viewthemanpageforthemancommand.
man—nh—njman
Chapter Seven: LINUX DIRECTORYCOMMAND
LinuxorUNIX-likesystemsuse the ls command to list filesanddirectories.However, ls does not have an option to list only directories. You can usecombination of ls and grep to list directory names only. You can use the findcommandtoo.Inthisquicktutorial,youwilllearnhowtolistonlydirectoriesin
LinuxorUNIX.
ListalldirectoriesinUnix
Typethefollowingcommand:
$ls-l|grep`^d’ls-l|egrep`^d’
Trythefollowinglscommandtolistdirectoriesinthecurrentdirectory:
$ls-d*/
sampleoutputs
ListonlyfilesinUnix
Typethefollowingcommand:$ls-l|egrep-v‘^d’
$ls-l|egrep-v‘^d’
Thegrepcommandisusedtosearchesinput.Itwillfilteroutdirectorynames
bymatchingfirstcharacter‘d’.Toreversetheeffecti.e.justtodisplay
filesyouneedtopassthe-voption.Itinvertsthesenseofmatching,to
selectnon-matchinglines.
Task:Createaliasestosavetime
Youcancreatetwoaliasestolistonlydirectoriesandfiles.aliaslf=“ls-l|egrep-v‘^d’”
aliasldir=“ls-l|egrep‘^d’”
Putabovetwoaliasesinyourbashshellstartupfile:$cd
$vi.bash_profile
Appendtwolines:aliaslf=“ls-l|egrep-v‘^d’”
aliasldir=“ls-l|egrep‘^d’”
Saveandclosethefile.
Nowjusttypelf-tolistfilesandldir-tolistdirectoriesonly:$cd/etc
$lf
Sampleoutput:
-rw-r—r—1rootroot21492006-09-0423:25adduser.conf
-rw-r—r—1rootroot442006-09-2905:11adjtime
-rw-r—r—1rootroot1972006-09-0423:48aliases
-rw––-1rootroot1442002-01-1813:43at.deny
-rw-r—r—1rootroot1622006-09-2223:24aumixrc
-rw-r—r—1rootroot282006-09-2223:24aumixrc1
Listdirectorynamesonly:$cd/etc
$ldir
Sampleoutput:drwxr-xr-x4rootroot40962006-09-2216:41alsa
drwxr-xr-x2rootroot40962006-09-2020:59alternatives
drwxr-xr-x6rootroot40962006-09-2216:41apm
drwxr-xr-x3rootroot40962006-09-0702:51apt
drwxr-xr-x2rootroot40962006-09-0801:46bash_completion.dUsefindcommandtolisteitherfilesordirectories
Thefindcommandcanbeusedasfollows:tolistalldirectoriesin/nas,enter:find/nas-typed
find/nas-typed-ls
find.-typed-ls
Sampleoutput:10707858drwxrwxrwt8rootroot4096Jul507:12.
10707978drwx––2rootroot4096Jul407:22./orbit-root
10708438drwxr-xr-x2rootroot4096Jun1618:55./w
10707898drwxr-xr-x10rootroot4096Jun1714:54./b
10713408drwxr-xr-x2rootroot4096Jun1618:55./b/init.d
10715818drwxr-xr-x3rootroot4096Jun1618:55./b/bind
10715848drwxr-xr-x2rootroot4096Jun1618:55./b/bind/bak
10716178drwxr-xr-x2rootroot4096Jun1618:55./b/fw
10716288drwxr-xr-x8rootroot4096Jun1618:55./b/scripts
ChapterEight:WORKINGWITHFILES
ThischapterwillfirstdescribegeneralcharacteristicsofUnixcommands.Itwill
thendiscusscommands,whicharecommonlyusedtocreateandmanipulatefiles.
AsummaryofsomeofthemostcommonlyusedUNIXcommandsispresentedin
CommandComparisons.
UNIXFileNames
ItisimportanttounderstandtherulesforcreatingUNIXfiles:UNIXiscase
sensitive! For example, “fileName” is different from “filename.” It is
recommended that you limit names to the alphabetic characters, numbers,
underscore(_),anddot(.).Dots(.)usedinUNIXfilenamesaresimplycharacters
andnotdelimitersbetweenfilenamecomponents;youmayincludemorethanone
dotinafilename.Includingadotasthefirstcharacterofafilenamemakesthefile
invisible(hidden)tothenormallscommand;usethe-aflagofthelscommandto
displayhidden files.Althoughmany systemswill allowmore, the recommended
length is 14 characters per file name. Unix shells typically include several
important wildcard characters. The asterisk (*) is used to match 0 or more
character (e.g., abc* will match any file beginning with the letters abc), the
questionmark(?)isusedtomatchanysinglecharacter,andtheleft([)andright
(])squarebracketsareusedtoencloseastringofcharacters,anyoneofwhichis
tomatch.Executethefollowingcommandsandobservetheresults:
lsm*
ls*.f
ls*.?
ls[a-d]*
NotesforPCusers:Unixusesforwardslashes(/)insteadofbackslashes(\)fordirectories
LookingattheContentsofFiles
You can examine the contents of files using a variety ofcommands.cat,more,pg,head,andtailaredescribedhere.Of course, youcanalwaysuseaneditor;touseviin“read-only”modetoexaminethecontentsofthefile“argtest”,enter:
vi-Rargtest
Youcannowuse the standardvi commands tomove through the file;however,
youwillnotbeabletomakeanychangestothecontentsofthefile.Thisoptionis
usefulwhenyousimplywanttolookatafileandwanttoguaranteethatyoumake
nochangeswhiledoingso.
Usethevi“””commandtoexitfromthefile.
CatCommand
cat is a utility used to conCATenate files. Thus, it can be used to join filestogether,butitisperhapsmorecommonlyusedtodisplaythecontentsofafileonthescreen.
Observetheoutputproducedbyeachofthefollowingcommands:
cd;cdxmp
catcars
cat-vetcars
cat-ncars
The semicolon (;) in the first line of this example is a command separatorwhich enables entry of more than one command on a line. Whenthe<Return>keyispressedfollowingthisline,thecommandcdisissuedwhichchangestoyourhomedirectory.Thenthecommand“cdxmp”isissuedtochangeinto the subdirectory “xmp.” Entering this line is equivalent to having enteredthese commands sequentially on separate lines. These two commands areincludedintheexampletoguaranteethatyouareinthesubdirectorycontaining“cars”andtheotherexamplefiles.Youneednotenterthesecommandsifyouarealreadyinthe“xmp”directorycreatedwhenyoucopiedtheexamplefile.
The “-vet” option enables display of tab, end-of-line, and other non-printablecharacterswithinafile;the“-n”optionnumberseachlineasitisdisplayed.
Youcanalsousethecatcommandtojoinfilestogether:
catpage1
catpage2
catpage1page2>document
catdocument
Note: If the file “document” had previously existed, it will be replaced by the
contentsoffiles“page1”and“page2.”
Cautionstousingthecatcommand:Thecatcommandshouldonlybeusedwith “text” files; it should not be used to display the contents of binary (e.g.,compiledCorFORTRANprograms).Unpredictableresultsmayoccur,includingtheterminationofyourlogonsession.Usethecommand“file*”todisplaythecharacteristicsoffileswithinadirectorypriortousingthecatcommandwithanyunknownfile.Youcanusetheod(enter“manod”fordetailsonuseofOctalDump)commandtodisplaythecontentsofnon-textfiles.Forexample,todisplaythecontentsof“a.out”inbothhexadecimaland
characterrepresentation,enter:
od-xca.out
Warning:cat(andotherUnixcommands)candestroyfilesifnotusedcorrectly.Forexample,asillustratedintheSobellbook,thecat(alsocpandmv)commandcan overwrite and thus destroy files. Observe the results of the followingcommand:
catletterpage1>letter
Typically, UNIX does not return a message when a command executes
successfully. Here the UNIX operating system will attempt to complete the
requested command by first initializing the file “letter” and then writing the
currentcontentsof“letter”(nownothing)and“page1”intothisfile.Since“letter”
has been reinitialized and is also named as a source file, an error diagnostic is
generated. Part of the UNIX philosophy is “No news is good news.” Thus, the
appearance of a message is a warning that the command was not completed
successfully.
Now use the “cat” command to individually examine the contents of the files“letter” and “page1.” Observe that the file “letter” does not contain the originalcontentsofthefiles“letter”and“page1”aswasintended.
Usethefollowingcommandtorestoretheoriginalfile“letter”:
cp~aixstu00/xmp/letter.
MoreCommand
You may type or browse files using the more command. The “more”commandisusefulwhenexaminingalargefileasitdisplaysthefilecontentsonepage at a time, allowing each page to be examined at will. As withthemancommand,youmustpressthespacebartoproceedtothenextscreenofthefile.Onmanysystems,pressingthe<b>keywillenableyoutopagebackwardsinthefile.Toterminatemoreatanytime,press<q>.
Toexamineafilewiththemorecommand,simplyenter:
morefile_name
Themancommanduses themorecommand todisplay themanualpages; thus,thecommandsyouarefamiliarwithusingmanwillalsoworkwithmore.Not all Unix systems include the more command; some implementthe pg command instead. VTAIX includes both the more and pg commands.When using the pg command, press <Return> to page down through a fileinsteadofusingthespacebar.
Observetheresultsofenteringthefollowingcommands:
moreargtest
pgargtest
HeadCommand
Theheadcommandisusedtodisplaythefirst fewlinesofafile.Thiscommand
canbeusefulwhenyouwishtolookforspecificinformation,inthebeginningof
thefile.Forexample,enter:
headargtest
TailCommand
Thetailcommandisusedtodisplaythelastlinesofafile.Thiscommandcanbeusefultomonitorthestatusofaprogram,whichappendsoutputtotheendofafile.Forexample,enter:
tailargtest
Copying,Erasing,Renaming
Warning:ThetypicalUnixoperatingsystemprovidesno‘unerase’or ‘undelete’command.Ifyoumistakenlydeleteafileyouaredependentuponthebackupsyou
orthesystemadministratorhavemaintainedinordertorecoverthefile.Youneed
to be careful when using commands like copy and move, which may result in
overwritingexisting files. If youareusing theCorKornShell, you can createa
command alias , which will prompt you for verification before overwriting fileswiththesecommands.
CopyingFiles
Thecpcommandisusedtocopyafileorgroupoffiles.Youhavealreadyseenanexampleapplicationofthecpcommandwhenyoucopiedthesamplefilestoyouruserid.Now let’smakea copyofoneof these files.Recall thatyoucanobtaina
listingof the files in the currentdirectoryusing thels command.Observe theresultsfromthefollowingcommands:
lsl*
cpletterletter.2
lsl*
Note:Unlikemanyotheroperatingsystems,suchasPC/DOS,youmustspecify
thetargetwiththecopycommand;itdoesnotassumethecurrentdirectoryifno
“copy-to”targetisspecified.
ErasingFiles
Unixusesthecommandrm(ReMove)todeleteunwantedfiles.Toremovethefile“letter.2”whichwehavejustcreated,enter:
rmletter.2
Enterthecommand“lsl*”todisplayalistofallfilesbeginningwiththeletter“l.”Note that letter.2 is no longer present in the current directory. The removecommand can be used with wildcards in filenames; however, this can bedangerous as you might end up erasing files you had wanted to keep. It isrecommendedthatyouusethe“-i”(interactive)optionofrmforwildcarddeletes—youwillthenbepromptedtorespondwitha“y”or“Y”foreachfileyouwishtodelete.
RenamingaFile
The typical Unix operating system utilities do not include a renamecommand;however,wecanusethemv(MoVe)command(seeforadditionalusesof this command) to “move”WorkingwithDirectories) a file fromonename toanother.Observetheresultsofthefollowingcommands:
ls[d,l]*
mvletterdocument
ls[d,l]*
mvdocumentletter
ls[d,l]*
Note: The first mv command overwrites the file “document” which you hadcreatedinanearlierexercisebyconcatenating“page1”and“page2.”Nowarningis
issuedwhenthemvcommandisusedtomoveafileintothenameofanexistingfile.Ifyouwouldliketobepromptedforconfirmationifthemvcommandweretooverwriteanexisting file,use the “-i” (interactive) optionof themv command,e.g.:
mv-ipage1letter
Youwillnowbetoldthatthefile“letter”alreadyexistsandyouwillbeaskedifyouwish toproceedwith themvcommand.Answeranythingbut “y”or “Y” and thefile“letter”willnotbeoverwritten.
UsingtheCommandLine
The command interpreter (shell) provides the mechanism by which input
commandsare interpretedandpassed to theUnixkernelorotherprograms for
processing.Observetheresultsofenteringthefollowingcommands:
./filesize
./hobbit
./add2
ls-F
Observethat“filesize”isanexecutableshellscript,whichdisplaysthesizeoffiles.
Also note that “./hobbit” and “./add2” generate error diagnostics as there is no
command or file with the name “hobbit” and the file “add2” lacks execute
permission.
StandardInputandStandardOutput
Asyouhavecansee,Unixexpectsstandardinputtocomefromthekeyboard,e.g.,
enter:
cat
my_text
<Ctrl-D>
Standardoutputistypicallydisplayedontheterminalscreen,e.g.,enter:
catcars
Standard error (a listing of program execution error diagnostics) is typically
displayedontheterminalscreen,e.g.,enter:
lsxyzpqrz
Redirection
As illustrated above, many Unix commands read from standard input
(typically the keyboard) and write to standard output (typically the terminal
screen).Theredirectionoperatorsenableyoutoreadinputfromafile(<)orwrite
program output to a file (>).When output is redirected to a file, the program
outputreplacestheoriginalcontentsofthefileifitalreadyexists;toaddprogram
outputtotheendofanexistingfile,usetheappendredirectionoperator(>>).
Observetheresultsofthefollowingcommand:
./a.out
YouwillbepromptedtoenteraFahrenheittemperature.Afterenteringanumeric
value,amessagewillbedisplayedonthescreeninformingyouoftheequivalent
Centigradetemperature.Inthisexample,youenteredanumericvalueasstandard
input via the keyboard and the output of the program was displayed on the
terminalscreen.
Inthenextexample,youwillreaddatafromafileandhavetheresultdisplayedon
thescreen(standardoutput):
catdata.in
./a.out<data.in
Nowyouwillreadfromstandardinput(keyboard)andwritetoafile:
./a.out>data.two
35
catdata.two
Nowreadfromstandardinputandappendtheresulttotheexistingfile:
./a.out<data.in>>data.two
As another example of redirection, observe the result of the following two
commands:
ls-la/etc>temp
moretemp
Herewehaveredirectedtheoutputofthelscommandtothefile“temp”andthenusedthemorecommandtodisplaythecontentsofthisfileapageatatime.Inthenextsection,wewillseehowtheuseofpipescouldsimplifythisoperation.
UsingPipesandFilters
AfilterisaUnixprogram,whichacceptsinputfromstandardinputandplacesits
outputinstandardoutput.FiltersaddpowertotheUNIXsystemasprogramscan
bewrittentousetheoutputofanotherprogramasinputandcreateoutput,which
canbeusedbyyetanotherprogram.Apipe(indicatedbythesymbol“|”—vertical
bar)isusedbetweenUNIXcommandstoindicatethattheoutputfromthefirstis
to be used as input by the second. Compare the output from the following two
commands:
ls-la/etc
ls-la/etc|more
The first commanddisplays of all the files in the in the “/etc” directory in longformat.Itisdifficulttomakeuseofthisinformationsinceitscrollsrapidlyacrossthe screen. In the second line, the results of the ls command are piped intothemorecommand.Wecannowexaminethisinformationonescreenatatimeandcanevenbackuptoapriorscreenofinformationifwewishto.AsyoubecamemorefamiliarwithUNIX,youwillfindthatpipingoutputtothemorecommandcanbeveryusefulinavarietyofapplications.
Thesortcommandcanbeusedtosortthelinesinafileinadesiredorder.Nowenterthefollowingcommandsandobservetheresults:
who
sortcars
who|sort
Thewhocommanddisplays a list of users currently logged onto the system thesort command enables us to sort the information. The second command sortsthe lines in the file cars alphabetically by first field and displays the result instandard output. The third command illustrates how the result of the whocommandcanbepassedtothesortcommandpriortobeingdisplayed.Theresultisalistingofloggedonusersinalphabeticalorder.
The following example uses the “awk” and “sort” commands to select andreorganizetheoutputgeneratedbythe“ls”command:
ls-l|awk‘/:/{print$5,$9}’|sort-nr
Note:Curlybracesdonotnecessarilydisplaycorrectlyonalloutputdevices. In
theaboveexample,thereshouldbealeftcurlybraceinfrontofthewordprintand
arightcurlybracefollowingthenumber9.
Observethat theoutputdisplaysthe filesizeandfilenameindecreasingorderofsize. Here the ls command first generates a “long” listing of the files in thecurrentdirectory,whichispipedtothe“awk”utility,whoseoutputisthenpipedtothe“sort”command.
“awk” is a powerful utility which processes one or more program lines to findpatternswithinafileandperformselectiveactionsbasedonwhatisfound.Slash(/) characters are used as delimiters around the pattern to bematched and theactiontobetakenisenclosedincurlybraces.Ifnopatternisspecified,alllinesinthefileareprocessedandifnoactionisspecified,alllinesmatchingthespecifiedpattern are output. Since a colon (:) is used here, all lines containing fileinformation (the time column corresponding to each file contains a colon) areselectedandtheinformationcontainedinthe5thand9thcolumnsareoutputtothesortcommand.
Note:Ifthelscommandonyoursystemdoesnotincludeacolumnlistinggroupmembership, use{print$4,$8} instead of the “print” command option ofawklistedabove.
Herethe“sort”commandoptions“-nr”specify that theoutput from“awk”besortedinreversenumericorder,i.e.,fromlargesttosmallest.
Theprecedingcommandissomewhatcomplexanditiseasytomakeamistakein
enteringit.Ifthiswereacommandyouwouldusefrequently,wecouldincludeit
inashellscriptasinsamplefile“filesize”.Tousethisshellscript,simplyenterthe
command:
./filesizeor
shfilesize
Ifyouexaminethecontentsof this filewiththecatorvicommands,youwillseethatitcontainsnothingmorethepipingofthelscommandtoawkandthenpipingtheoutputtosort.Theteeutilityisusedtosendoutputtobothafileandthescreen:
who|teewho.out|sort
catwho.out
Hereyoushouldhaveobservedthatalistofloggedonuserswasdisplayedonthe
screen in alphabetical order and that the file “who.out” contained an unsorted
listingofthesameuserids.
SomeAdditionalFileHandlingCommands
WordCount
Thecommandwcdisplaysthenumberoflines,words,andcharactersinafile.
Todisplaythenumberoflines,words,andcharactersinthefilefile_name,enter:
wcfile_name
ComparingtheContentsofTwoFiles:thecmpanddiffCommands
Thecmpanddiffcommandsareusedtocomparefiles;the“comp”commandisnotusedtocomparefiles,butto“composeamessage.”
Thecmp command can be used for both binary and text files. It indicates thelocation(byteandline)wherethefirstdifferencebetweenthetwofilesappears.
Thediffcommandcanbeusedtocomparetext filesanditsoutputshowsthelineswhicharedifferentinthetwofiles:alessthansign(“<“)appearsinfrontoflines fromthe first filewhichdiffer fromthose in thesecondfile,agreater thansymbol(“>”)precedeslinesfromthesecondfile.Matchinglinesarenotdisplayed.
Observetheresultsofthefollowingcommands:
cmppage1page2
diffpage1page2
Lines1and2ofthesetwofilesareidentical, lines3differbyonecharacter,and
pageonecontainsablanklinefollowinglinethree,whilepage2doesnot.
ChapterNine:NAVIGATIONANDFILEMANAGEMENT
IfyoudonothavemuchexperienceworkingwithLinuxsystems,youmaybe
overwhelmed by the prospect of controlling an operating system from the
commandline.Inthisbook,wewillattempttogetyouuptospeedwiththebasics.
PrerequisitesandGoals
In order to follow alongwith this book, youwill need to have access to a
Linuxserver.Youwillalsowanttohaveabasicunderstandingofhowtheterminal
worksandwhatLinuxcommandslooklike.Thisbookcoversterminalbasics,so
youshouldcheckitoutifyouarenewtousingterminals.Allofthematerialinthis
book can be accomplished with a regular, non-root (non-administrative) user
account. You can learn how to configure this type of user account by following
yourdistribution’sinitialserversetupguide(Ubuntu14.04,CentOS7).Whenyou
arereadytobegin,connecttoyourLinuxserverusingSSHandcontinuebelow.
NavigationandExploration
The most fundamental skills you need to master are navigating the
filesystem.Wewilldiscussthetoolsthatallowyoutodothisinthissection.
Findingwhereyouarewiththe“pwd”command
When you log onto your server, you are typically dropped into your useraccountshomedirectory.Ahomedirectory is thedirectory set aside for youraccount to store files and create directories. It is the location in the filesystemwhere you have full dominion. To find out where your home directory is inrelationship to the rest of the filesystem, you can use the pwd command. Thiscommanddisplaysthedirectorythatwearecurrentlyin:pwd
Youshouldgetbacksomeinformationthatlookslikethis:
/home/demo
Thehomedirectoryisnamedaftertheuseraccount,sotheaboveexampleiswhat the value would be if you were logged into the server with an accountcalled demo. This directory is within a directory called/home, which is itselfwithinthetop-leveldirectory,whichiscalled“root”representedbyasingleslash“/”.
LookingattheContentsofDirectorieswith“ls”
Nowthatyouknowhowtodisplaythedirectorythatyouarein,wecanshow
youhowtolookatthecontentsofadirectory.
Currently,yourhomedirectorydoescontainmuch,sowewillgotoanother,more populated directory to explore. Type the following command in yourterminaltochangedirectory(wewillexplainthedetailsofmovingdirectoriesinthe next section). Afterward, we will use pwd to confirm that we successfullymoved:cd/usr/share
pwd
/usr/share
Nowthatweareinanewdirectory,letuslookatwhat’sinside.Todothis,wecanusethelscommand:ls
addusergroffpam-configs
applicationsgrubperl
apportgrub-gfxpayload-listsperl5
appshalpixmaps
apti18npkgconfig
aptitudeiconspolkit-1
apt-xapian-indexinfopopularity-contest
…
Asyoucansee,therearemanyitemsinthisdirectory.Wecanaddsomeoptionalflagstothecommandtomodifythedefaultbehavior.Forinstance,tolistallofthecontentsinanextendedform,wecanusethe-lflag(for“long”output):ls-l
total440
drwxr-xr-x2rootroot4096Apr172014adduser
drwxr-xr-x2rootroot4096Sep2419:11applications
drwxr-xr-x6rootroot4096Oct918:16apport
drwxr-xr-x3rootroot4096Apr172014apps
drwxr-xr-x2rootroot4096Oct918:15apt
drwxr-xr-x2rootroot4096Apr172014aptitude
drwxr-xr-x4rootroot4096Apr172014apt-xapian-index
drwxr-xr-x2rootroot4096Apr172014awk
…
Thisviewgivesusplentyofinformation,mostofwhichlooksratherunusual.Thefirstblockdescribesthefiletype(ifthefirstcolumnisa“d”theitemisadirectory,if it is a “-“, it is a normal file) and permissions. Each subsequent column,separatedbywhite space,describes thenumberofhard links, theowner, groupowner, item size, last modification time, and the name of the item. We willdescribesomeoftheseatanothertime,butfornow,justknowthatyoucanviewthisinformationwiththe-lflagofls.Togetalistingofallfiles,includinghiddenfilesanddirectories,youcanaddthe-aflag.Sincetherearenorealhiddenfilesinthe/usr/sharedirectory, let’sgobacktoourhomedirectoryandtrythatcommand.Youcangetbacktothehomedirectorybytypingcdwithnoarguments:cd
ls-a
....bash_logout.bashrc.profile
As you can see, there are three hidden files in this demonstration, alongwith .and ..,whicharespecial indicators.Youwill findthatoften,configurationfilesarestoredashiddenfiles.
Thedotanddoubledotentries,arebuilt-inmethodsofreferringtorelated
directories.Thesingledotindicatesthecurrentdirectory,andthedoubledot
indicatesthisdirectory’sparentdirectory.Thiswillcomeinhandyinthenext
section.
MovingAroundtheFilesystemwith“cd”
Wehavealreadychangeddirectoriestwicetodemonstratesomepropertiesofls.Let’stakeabetterlookatthecommandhere.
Beginbygoingbacktothe/usr/sharedirectorybytypingthis:
cd/usr/share
This isanexampleofchangingadirectorybygivinganabsolutepath.InLinux,
everyfileanddirectoryisunderthetop-mostdirectory,whichiscalledthe“root”
directory,butreferredtobyasingleleadingslash“/”.Anabsolutepathindicates
thelocationofadirectoryinrelationtothistop-leveldirectory.Thisletsusrefer
to directories in an unambiguous way from any place in the filesystem. Every
absolutepathmustbeginwithaslash.
The alternative is to use relative paths. Relative paths refer to directories in
relationtothecurrentdirectory.Fordirectoriesclosetothecurrentdirectory in
thehierarchy,thisisusuallyeasierandshorter.Anydirectorywithinthecurrent
directory canbe referencedbynamewithout a leading slash.We can change to
thelocaledirectorywithin/usr/sharefromourcurrentlocationbytyping:
cdlocale
Wecanlikewisechangemultipledirectorylevelswithrelativepathsbyprovidingtheportionof thepaththatcomesafter thecurrentdirectory’spath.Fromhere,wecangettotheLC_MESSAGESdirectorywithintheendirectorybytyping:
cden/LC_MESSAGES
Tomoveuponedirectorylevel,weusethespecialdoubledotindicatorwetalkedaboutearlier.Forinstance,wearenowinthe/usr/share/locale/en/LC_MESSAGESdirectory.Tomoveuponelevel,wecantype:cd..
Thistakesustothe/usr/share/locale/endirectory.Ashortcutthatyousawearlierthatwillalwaystakeyoubacktoyourhome
directoryistousecdwithoutprovidingadirectory:cd
pwd
/home/demo
ViewingFiles
Inthelastsection,welearnedhowtonavigatethefilesystem.Inthissection,wewilldiscussdifferentwaystoviewfiles.Incontrasttosomeoperatingsystems,Linux and other Unix-like operating systems rely on plain text files for vastportions of the system. The main way that we will view files is withthelesscommand.Thisiswhatwecalla“pager,”becauseitallowsustoscrollthroughpagesofafile.Whilethepreviouscommandsimmediatelyexecutedandreturnedyou to the command line,less is an application thatwill continue torunandoccupythescreenuntilyouexit.
Wewillopenthe/etc/servicesfile,whichisaconfigurationfilethatcontainsthesystemsservicesinformation:less/etc/services
Thefilewillbeopenedinless,allowingyoutoseetheportionofthedocumentthatfitsintheterminalwindow:#Networkservices,Internetstyle
#
#NotethatitispresentlythepolicyofIANAtoassignasinglewell-known
#portnumberforbothTCPandUDP;hence,officiallyportshavetwoentries
#eveniftheprotocoldoesn’tsupportUDPoperations.
#
#Updatedfromhttp://www.iana.org/assignments/port-numbersandother
#sourceslikehttp://www.freebsd.org/cgi/cvsweb.cgi/src/etc/services.
#Newportswillbeaddedonrequestiftheyhavebeenofficiallyassigned
#byIANAandusedinthereal-worldorareneededbyadebianpackage.
#Ifyouneedahugelistofusednumberspleaseinstallthenmappackage.
tcpmux1/tcp#TCPportservicemultiplexer
echo7/tcp
…
Toscroll,youcanusetheupanddownarrowkeysonyourkeyboard.Topagedownonewholescreens-worthofinformation,youcanuseeitherthespacebar,the“PageDown”buttononyourkeyboard,ortheCTRL-fshortcut.Toscrollbackup,youcanuseeitherthe“PageUp”button,ortheCTRL-bkeyboardshortcut.Tosearchforsometextinthedocument,youcantypeaforwardslash“/”followedbythesearchterm.Forinstance,tosearchfor“mail”,wewouldtype:/mailThiswillsearchforwardthroughthedocumentandstopatthefirstresult.Togettoanotherresult,youcantypethelower-casenkey:n
Tomovebackwardstothepreviousresult,useacapitalNinstead:N
Whenyouwishtoexitthelessprogram,youcantypeqtoquit:q
Whilewefocusedonthelesstool inthissection, therearemanyotherwaysofviewingafile.Thecatcommanddisplaysafile’scontentsandreturnsyoutothepromptimmediately.Theheadcommand,bydefault,showsthefirst10linesofafile. Likewise, the tail command shows the last 10 lines. These commandsdisplayfilecontentsinawaythatisusefulfor“piping”tootherprograms.Wewilldiscussthisconceptinafutureguide.
Feelfreetoseehowthesecommandsdisplaythe/etc/servicesfiledifferently.
FileandDirectoryManipulation
We learned in the last section how to view a file. In this section, we will
demonstratehowtocreateandmanipulatefilesanddirectories.
CreateaFilewith“touch”
Manycommandsandprogramscancreate files.Themostbasicmethodofcreatinga file iswiththetouchcommand.Thiswillcreateanempty fileusingthenameandlocationspecified.
First,weshouldmakesureweareinourhomedirectory,sincethis isa locationwherewehavepermissiontosavefiles.Then,wecancreateafilecalledfile1bytyping:cd
touchfile1
Now,ifweviewthefilesinourdirectory,wecanseeournewlycreatedfile:
ls
file1
Ifweusethiscommandonanexistingfile,thecommandsimplyupdatesthedata
ourfilesystemstoresonthetimewhenthefilewaslastaccessedandmodified.
Wecanalsocreatemultiplefilesatthesametime.Wecanuseabsolutepathsaswell.Forinstance,ifouruseraccountiscalleddemo,wecouldtype:touch/home/demo/file2/home/demo/file3
ls
file1file2file3
CreateaDirectorywith“mkdir”
Similartothetouchcommand,themkdircommandallowsustocreateemptydirectories.
For instance, to create a directory within our home directory called test,wecouldtype:cd
mkdirtest
Wecanmakeadirectorywithinthetestdirectorycalledexamplebytyping:mkdirtest/example
For the above command to work, the test directory must already exist. Totell mkdir that it should create any directories necessary to construct a givendirectory path, you can use the -p option. This allows you to create nesteddirectories in one step. We can create a directory structure that looks likesome/other/directoriesbytyping:mkdir-psome/other/directories
The command will make the some directory first, then it will createthe other directory in the some directory. Finally, it will createthedirectoriesdirectoryintheotherdirectory.
MovingandRenamingFilesandDirectorieswith“mv”
Wecanmoveafiletoanewlocationusingthemvcommand.Forinstance,wecanmovefile1intothetestdirectorybytyping:mvfile1test
For this command,we list all items thatwewant tomove,with the location to
move them to.We canmove that fileback to our home directory by using the
specialdotreferencetorefertoourcurrentdirectory.Weshouldmakesureweare
inourhomedirectory,andthenexecutethecommand:
cd
mvtest/file1.
This may seem unintuitive at first, but the mv command is also usedtorename files and directories. In essence,moving and renaming are both justadjustingthelocationandnameforanexistingitem.
Sotorenamethetestdirectorytotesting,wecouldtype:mvtesttesting
Note:ItisimportanttorealizethatyourLinuxsystemwillnotpreventyoufrom
certain destructive actions. If you are renaming a file and choose a name
that already exists, the previous file will be overwritten by the file you are
moving.Thereisnowaytorecoverthepreviousfileifyouaccidentallyoverwrite
it.
CopyingFilesandDirectorieswith“cp”
Withthemvcommand,wecouldmoveorrenameafileordirectory,butwecouldnotduplicateit.Thecpcommandcanmakeanewcopyofanexistingitem.
Forinstance,wecancopyfile3toanewfilecalledfile4:
cpfile3file4
Unlikethemvoperation,afterwhichfile3wouldno longerexist,wenowhavebothfile3andfile4.
Note:Aswith themvcommand, it ispossible tooverwrite a file if youarenotcareful about the filename you are using as the target of the operation. Forinstance, if file4 already existed in the above example, its contents would becompletelyreplacedbythecontentsoffile3.Inordertocopydirectories,youmustincludethe-roptioninthecommand.Thisstandsfor“recursive,”asitcopiesthedirectory,plusallofthedirectory’scontents.
This option is necessarywith directories, regardless of whether the directory isempty. For instance, to copy the some directory structure to a new structurecalledagain,wecouldtype:
cp-rsomeagain
Unlikewithfiles,withwhichanexistingdestinationwouldleadtoanoverwrite,if
thetargetisanexistingdirectory,thefileordirectoryiscopiedintothetarget:
cpfile1again
Thiswillcreateanewcopyoffile1andplaceitinsideoftheagaindirectory.
RemovingFilesandDirectorieswith“rm”and“rmdir”
Todeleteafile,youcanusethermcommand.
Note:Beextremelycarefulwhenusinganydestructivecommandlikerm.Thereisno “undo” command for these actions so it is possible to accidentally destroy
importantfilespermanently.
Toremovearegularfile,justpassittothermcommand:
cd
rmfile4
Likewise,toremoveemptydirectories,wecanusethermdircommand.Thiswillonly succeed if there is nothing in the directory in question. For instance, to
removetheexampledirectorywithinthetestingdirectory,wecantype:
rmdirtesting/example
If you wish to remove a non-empty directory, you will have to use
therm command again. This time, you will have to pass the-roption, whichremovesallofthedirectory’scontentsrecursively,plusthedirectoryitself.
For instance, to remove the again directory and everything within it, we cantype:
rm-ragain
Onceagain, it isworth reiterating that thesearepermanent actions.Be entirely
surethatthecommandyoutypedistheonethatyouwishtoexecute.
EditingFiles
Currently, we know how to manipulate files as objects, but we have not
learnedhowtoactuallyeditandaddcontenttothem.Thenanocommandisoneofthesimplestcommand-lineLinuxtexteditors,andisagreatstartingpointfor
beginners. It operates somewhat similarly to the less program we discussedearlier, in that it occupies the entire terminal for the duration of its use.
Thenanoeditorcanopenexistingfiles,orcreateafile.Ifyouwanttocreateanewfile,youcangiveitanamewhenyoucallthenanoeditor,orlateron,whenyouwishtosaveyourcontent.
Wecanopenthefile1fileforeditingbytyping:
cd
nanofile1
Thenanoapplicationwillopenthe file (which iscurrentlyblank).The interfacelookssomethinglikethis:
GNUnano2.2.6File:file1
[Read0lines]
^GGetHelp^OWriteOut^RReadFile^YPrevPage^KCutText^CCurPos
^XExit^JJustify^WWhereIs^VNextPage^UUnCutText^TToSpell
Alongthetop,wehavethenameoftheapplicationandthenameofthefileweare
editing.Inthemiddle,thecontentofthefile,currentlyblank,isdisplayed.Along
the bottom, we have a number of key combinations that indicate some basic
controlsfortheeditor.Foreachofthese,the^charactermeanstheCTRLkey.
Togethelpfromwithintheeditor,type:
CTRL-G
When you are finished browsing the help, type CTRL-X to get back to yourdocument.
Typeinormodifyanytextyouwouldlike.Forthisexample,wewilljusttypethese
twosentences:
Hellothere.
Hereissometext.
Tosavetype:
CTRL-O
Thisistheletter“o,”notazero.Itwillaskyoutoconfirmthenameofthefileyouwishtosaveto:
FileNametoWrite:file1
^GGetHelpM-DDOSFormatM-AAppendM-BBackupFile
^CCancelM-MMacFormatM-PPrepend
Asyoucansee,theoptionsatthebottomhavealsochanged.Thesearecontextual,
meaningtheywillchangedependingonwhatyouaretryingtodo.If file1 isstill
thefileyouwishtowriteto,hit“ENTER.”
Ifwemakeadditionalchangesandwishtosavethefileandexittheprogram,we
will see a similar prompt. Add a new line, and then try to exit the program by
typing:
CTRL-X
Ifyouhavenotsavedaftermakingyourmodification,youwillbeaskedwhetheryouwishtosavethemodificationsyoumade:
Savemodifiedbuffer(ANSWERING“No”WILLDESTROYCHANGES)?
YYes
NNo^CCancel
Youcan type “Y” to saveyourchanges, “N” todiscardyour changesandexit, or“CTRL-C”tocanceltheexitoperation.Ifyouchoosetosave,youwillbegiventhesamefileprompt thatyoureceivedbefore,confirmingthatyouwant tosave thechangestothesamefile.PressENTERtosavethefileandexittheeditor.
Youcanseethecontentsofthefileyoucreatedusingeitherthecatcommandtodisplay the contents, or theless command to open the file for viewing. Afterviewingwithless,rememberthatyoushouldhitqtogetbacktotheterminal.lessfile1
Hellothere.
Hereissometext.
Anotherline.
ChapterTen:UNIXSHELLSCRIPTING
Time isprecious. Itdoesnotmakesense towaste time typinga frequentlyused sequence of commands at a commandprompt,more especially if they areabnormally long or complex. Scripting is a way bywhich one can alleviate thisproblem by automating these command sequences in order tomake life at theshelleasierandmoreproductive.Scriptingisallaboutmakingthecomputer,thetool,dothework.BytheendofthistutorialyouwillhaveagoodunderstandingofthekindofscriptinglanguagesavailableforUnixandhowtoapplythemtoyourproblems. UNIX contains many wonderful and strange commands that can beveryusefulintheworldofscripting,themoretoolsyouknowandthebetteryouknowthem,themoreuseyouwillfindforthem.MostoftheUnixcommandsandmany of the built-in commands haveman pages;manpages contain the usageinstructionspertainingtotheparenttool.Theyarenotalwaysveryclearandmayrequirereadingseveraltimes.InordertoaccessamanpageinUnixthefollowingcommandsequenceisapplied:
mancommand
Ifamanpageexistsforthecommandspecifiedtheinternalviewerwillbeinvoked
andyouwillbeabletoreadaboutthevariousoptionsandusageinstructions.
ShellScriptingIntroduction
UNIXuses shells to accept commandsgivenby theuser; there arequite a
few different shells available. The most commonly used shells are SH (Bourne
SHell) CSH (C SHell) and KSH (Korn SHell), most of the other shells you
encounterwillbevariantsoftheseshellsandwillsharethesamesyntax,KSHis
basedonSHasisBASH(Bourneagainshell).TCSH(ExtendedCSHell)isbased
onCSH.
Thevariousshellsallhavebuiltinfunctionswhichallowforthecreationofshell
scripts, that is, the stringing together of shell commands and constructs to
automaterepetitivetasksinordertomakelifeeasierfortheuser.
With all these different shells available, what shell should we use? This is
debatable. For the purpose of this tutorial we will be using SH because it is
practicallyguaranteedtobeavailableonmostUnixsystemsandbesupportedby
theSHbasedshells.YourdefaultshellmaynotbeSH.Fortunatelywedonothave
tobeusingaspecificshell inordertoexploit itsfeaturesbecausewecanspecify
theshellwewanttointerpretourshellscriptwithinthescriptitselfbyincluding
thefollowinginthefirstline.
#!/path/to/shell
Usuallyanythingfollowing(#) is interpretedasacommentand ignoredbut if it
occurson the first linewitha (!) following it is treatedasbeing special and the
filename following the (!) is considered topoint to the locationof the shell that
shouldinterpretthescript.
Whena script is “executed”, it isbeing interpretedbyan invocationof the shell
that is running it.Hence, theshell issaid toberunningnon-interactively,when
theshellisused“normally”itissaidtoberunninginteractively.
Note: There aremany variations of the basic commands and extra information
which is too specific tobementioned in this short tutorial, you should read the
manpageforyourshelltogetamorecomprehensiveideaoftheoptionsavailable
to you. This tutorial will concentrate on highlighting the most often used and
usefulcommandsandconstructs.
CommandRedirectionandPipelines
By default a normal command accepts input from standard input, which we
abbreviatetostdin,standardinputisthecommandlineintheformofarguments
passed to the command. By default a normal command directs its output to
standard output, which we abbreviate to stdout, standard output is usually the
console display. For some commands thismay be the desired action but other
timeswemaywishtogetour input foracommandfromsomewhereother than
stdin and direct our output to somewhere other than stdout. This is done by
redirection:
We use> to redirect stdout to a file, for instance, if we wanted to redirect a
directorylistinggeneratedbythelswecoulddothefollowing:
ls>file
Weuse<tospecifythatwewantthecommandimmediatelybeforetheredirectionsymboltogetitsinputfromthesourcespecifiedimmediatelyafterthesymbol,forinstance,wecouldredirect the input togrep(whichsearches forstringswithinfiles)sothatitcomesfromafilelikethis:
grepsearchterm<file
Weuse>>toappendstdouttoafile,forexample,ifwewantedtoappendthedatetotheendofafilewewouldredirecttheoutputfromdatelikeso:
date>>file
One can redirect standard error (stderr) to a file by using2>, if we wanted to
redirectthestandarderrorfromcommandAtoafilewewoulduse:
commmandA2>
Pipes are another form of redirection that are used to chain commands so that
powerfulcompositecommandscanbeconstructed, thepipesymbol ’|’ takes the
stdoutfromthecommandprecedingitandredirectsittothecommandfollowing
it:
ls-l|grepsearchword|sort-r
The example above firsts requests a long (-l directory listing of the currentdirectory using the ls command, the output from this is then pipedto grepwhich filters out all the listings containing the searchword and thenfinally pipes this through to sortwhich then sorts the output in reverse (-r,sortthenpassestheoutputonnormallytostdout.
Variables
When a script starts, all environment variables are turned into shell variables.
Newvariablescanbeinstantiatedlikethis:
name=value
Youmustdoitexactlylikethat,withnospaces,thenamemustonlybemadeupof
alphabeticcharacters,numericcharactersandunderscores;itcannotbeginwitha
numericcharacter.Youshouldavoidusingkeywordslikefororanythinglikethat,
theinterpreterwillletyouusethembutdoingsocanleadtoobfuscatedcode;)
Variablesarereferencedlikethis:$name,hereisanexample:#!/bin/sh
msg1=Hello
msg2=There!
echo$msg1$msg2
This would echo “Hello There!” to the console display, if you want to assign a
stringtoavariableandthestringcontainsspacesyoushouldenclosethestringin
doublequotes(“),thedoublequotestelltheshelltotakethecontentsliterallyand
ignore keywords, however, a few keywords are still processed. You can still
use$withina(“)quotedstringtoincludevariables:
#!/bin/sh
msg1=“one”
msg2=”$msg1two”
msg3=”$msg2three”
echo$msg3
Wouldecho“onetwothree”tothescreen.Theescapecharactercanalsobeused
withinadoublequotedsectiontooutputspecialcharacters,theescapecharacter
is”",itoutputsthecharacterimmediatelyfollowingitliterallyso\wouldoutput\.
A special case is when the escape character is followed by a newline; the shell
ignoresthenewlinecharacter,whichallowsthespreadingoflongcommandsthat
mustbeexecutedonasingle line inrealityovermultiple lineswithinthescript.
Theescapecharactercanbeusedanywhere,exceptwithinsinglequotes.
Surroundinganythingwithsinglequotescausesittobetreatedasliteraltextthat
will be passed on exactly as intended, this can be useful for sending command
sequencestootherfilesinordertocreatenewscriptsbecausethetextbetweenthe
singlequoteswillremainuntouched.Forexample:
#!/bin/sh
echo‘msg=“HelloWorld!”’>hello
echo‘echo$msg’>>hello
chmod700hello
./hello
Thiswouldcause“msg=“HelloWorld!”tobeechoedandredirectedtothefilehello,“echo $msg”would then be echoed and redirected to the file hello but this timeappendedtotheend.Thechmodlinechangesthefilepermissionsofhellosothatwecanexecuteit.Thefinallineexecuteshellocausingitoutput“HelloWorld.”Ifwehadnotusedliteralquoteswewouldnothavehadtouseescapecharacterstoensurethat($)and(“)wereechoedtothefile,thismakesthecodealittleclearer.
Avariablemaybereferencedlikeso${VARIABLENAME},thisallowsonetoplace
characters immediately preceding the variable
like${VARIABLENAME}aaawithout the shell interpreting aaa asbeingpart of
thevariablename.
CommandLineArguments
Command line arguments are treated as special variables within the script, the
reason I am calling them variables is because they can be changed with
the shift command. The command line arguments are enumerated in the
followingmanner$0,$1,$2,$3,$4,$5,$6,$7,$8and$9.$0isspecialinthatit
corresponds to the name of the script itself.$1 is the first argument,$2 is the
second argument and so on. To reference after the ninth argument you must
enclosethenumberinbracketslikethis${nn}.Youcanusetheshiftcommandto
shiftthearguments1variabletotheleftsothat$2becomes$1,$1becomes$0and
soon,$0getsscrappedbecauseithasnowheretogo,thiscanbeusefultoprocess
alltheargumentsusingaloop,usingonevariabletoreferencethefirstargument
andshiftinguntilyouhaveexhaustedtheargumentslist.
Aswellasthecommandlineargumentstherearesomespecialbuilt-invariables:
$#representstheparametercount.Usefulforcontrollingloop
constructsthatneedtoprocesseachparameter.
passingalltheparameterstosomeotherfunctionorprogram.
$-expandstotheflags(options)theshellwasinvokedwith.Usefulfor
controllingprogramflowbasedontheflagsset.
$$expandstotheprocessidoftheshellinnovatedtorunthescript.
Usefulforcreatinguniquetemporaryfilenamesrelativetothis
instantiationofthescript.
Note:Thecommand lineargumentswillbe referred toasparameters fromnow
on, this is because SH also allows the definition of functions which can take
parametersandwhencalledthe$nfamilywillberedefined,hencethesevariables
arealwaysparameters,itsjustthatinthecaseoftheparentscripttheparameters
arepassedvia thecommand line.Oneexception is$0which isalwaysset to the
nameoftheparentscriptregardlessofwhetheritisinsideafunctionornot.
CommandSubstitution
In thewords of the SHmanual “Command substitution allows the output of a
commandtobesubstitutedinplaceofthecommandnameitself”.Therearetwo
waysthiscanbedone.Thefirstistoenclosethecommandlikethis:
$(command)
Thesecondistoenclosethecommandinbackquoteslikethis:
`command`
The command will be executed in a sub-shell environment and the standard
output of the shell will replace the command substitution when the command
finishes.
ArithmeticExpansion
Arithmeticexpansionisalsoallowedandcomesintheform:
$((expression))
Thevalueoftheexpressionwillreplacethesubstitution.Eg:
!#/bin/sh
echo$((1+3+4))
Willecho“8”tostdout
ControlConstructs
TheflowofcontrolwithinSHscriptsisdoneviafourmainconstructs;if…then…
elif…,else…,do…,while…,for…andcase….
If..Then..Elif..Else
Thisconstructtakesthefollowinggenericform;thepartsenclosedwithin([)and
(])areoptional:
iflist
thenlist
[eliflist
thenlist]…
[elselist]
fi
When aUnix command exits it exitswithwhat is known as an exit status, this
indicatestoanyonewhowantstoknowthedegreeofsuccessacommand,usually
whenacommandexecuteswithouterroritterminateswithanexitstatusofzero.
Anexitstatusofsomeothervaluewouldindicatesthatanerrorhasoccurred,the
details ofwhich are specific to the command.The commands’manpagesdetail
theexitstatusmessages.
AlistisdefinedintheSHas“asequenceofzeroormorecommandsseparatedbynewlines,semicolons,orampersands,andoptionallyterminatedbyoneofthesethree characters.” Hence in the generic definition of the if above the list willdeterminewhichof theexecutionpaths thescript takes.Forexample, there isacommandcalledtestonUNIX,whichevaluatesanexpressionandifitevaluatestrue will return zero and will return one otherwise, this is how we can testconditionsinthelistpart(s)oftheifconstructbecausetestisacommand.
Wedonotactuallyhavetotypethetestcommanddirectlyintothelisttouseit;it can be implied by encasing the test case within ([) and (]) characters, asillustratedbythefollowing(silly)example:
#!/bin/sh
if[“$1”=“1”]
then
echo“Thefirstchoiceisnice”
elif[“$1”=“2”]
then
echo“Thesecondchoiceisjustasnice”
elif[“$1”=“3”]
then
echo“Thethirdchoiceisexcellent”
else
echo“Iseeyouwerewiseenoughnottochoose”
echo“Youwin”
fi
Whatthisexampledoesiscomparethefirstparameter(commandlineargument
inthiscase)withthestrings“1”,“2”and“3”usingtests’(=)testwhichcompares
two strings for equality, if any of them match it prints out the corresponding
message.Ifnoneofthemmatchitprintsoutthefinalcase.OKtheexampleissilly
andactuallyflawed(theuserstillwinseveniftheytypein(4)orsomething)butit
illustrateshowtheifstatementworks.
Noticethattherearespacesbetween(if)and([),([)andthetestandthetestand
(]), these spacesmust be present otherwise the shellwill complain. Theremust
alsobespacesbetweentheoperatorandoperandsofthetestotherwiseitwillnot
workproperly.Noticehow it startswith (if)andendswith (fi),also,noticehow
(then)isonaseparatelinetothetestaboveitandthat(else)doesnotrequirea
(then)statement.Youmustconstructthisconstructexactlylikethisforittowork
properly.
It isalsopossibletointegratelogicalANDandORintothetesting,byusingtwo
testsseparatedbyeither“&&”or“||”respectively.Forexample,wecouldreplace
thethirdtestcaseintheexampleabovewith:
elif[“$1”=“3”]||[“$1”=“4”]
thenecho“Thethirdchoi…
Thescriptwouldprintout“Thethirdchoiceisexcellent”ifthefirstparameterwas
either“3”OR“4”.Toillustratetheuseof“&&”wecouldreplacethethirdtestcase
with:
elif[“$1”=“3”]||[“$2”=“4”]
thenecho“Thethirdchoi…
The scriptwouldprint out “The third choice is excellent” if andonly if the first
parameterwas“3”ANDthesecondparameterwas“4”.
“&&”and“||”arebothlazilyevaluatingwhichmeansthat inthecaseof“&&”, ifthefirsttestfailsitwon’tbotherevaluatingthesecondbecausethelistwillonlybetrueiftheyBOTHpassandsinceonehasalreadyfailedthereisnopointwastingtimeevaluatingthesecond.Inthecaseof“||”ifthefirsttestpassesitwon’tbotherevaluatingthesecondtestbecauseweonlyneedONEoftheteststopassforthewholelisttopass.Seethetestmanpageforthelistoftestspossible(otherthanthestringequalitytestmentionedhere).
Do…While
TheDo…Whiletakesthefollowinggenericform:
whilelist
dolist
done
Inthewordsof theSHmanual“Thetwolistsareexecutedrepeatedlywhilethe
exitstatusofthefirstlistiszero.”Thereisavariationonthisthatusesuntilinplaceofwhilewhichexecutesuntiltheexitstatusofthefirstlistiszero.Hereisanexampleuseofthewhilestatement:
#!/bin/sh
count=$1#Initialisecounttofirstparameter
while[$count-gt0]#whilecountisgreaterthan10do
do
echo$countsecondstillsuppertime!
count=$(expr$count-1)#decrementcountby1
sleep1#sleepforasecondusingtheUnixsleepcommand
done
echoSuppertime!!,YEAH!!#werefinished
Ifcalledfromthecommandlinewithanargumentof4thisscriptwilloutput
4secondstillsuppertime!
3secondstillsuppertime!
2secondstillsuppertime!
1secondstillsuppertime!
Suppertime!!,YEAH!!
Youcanseethatthistimewehaveusedthe-gtofthetestcommandimplicitlycalledby‘[‘and‘]’,whichstandsforgreaterthan.Paycarefulattentiontotheformattingandspacing.
For
Thesyntaxoftheforcommandis:
forvariableinword…
dolist
done
The SHmanual states “Thewords are expanded, and then the list is executed
repeatedlywiththevariablesettoeachwordinturn.”Awordisessentiallysome
othervariablethatcontainsalistofvaluesofsomesort,theforconstructassigns
eachofthevaluesinthewordtovariableandthenvariablecanbeusedwithinthe
bodyoftheconstruct,uponcompletionofthebodyvariablewillbeassignedthe
nextvalue inworduntil therearenomorevalues inword.Thisexampleshould
makethisclearer:
#!/bin/sh
fruitlist=“ApplePearTomatoPeachGrape”
forfruitin$fruitlist
do
if[“$fruit”=“Tomato”]||[“$fruit”=“Peach”]
then
echo“Ilike${fruit}es”
else
echo“Ilike${fruit}s”
fi
done
Inthisexample, fruitlist isword, fruit isvariableand thebodyof the statement
outputs how much this person loves various fruits but includes an if… then…
else statement to dealwith the correct addition of letters to describe the plural
version of the fruit, notice that the variable fruit was expressed
like ${fruit} because otherwise the shell would have interpreted the preceding
letter(s) as being part of the variable and echoed nothing because we have not
definedthevariablesfruitsandfruitesWhenexecutedthisscriptwilloutput:
IlikeApples
IlikePears
IlikeTomatoes
IlikePeachs
IlikeGrapes
Withintheforconstruct,doanddonemaybereplacedby’{‘and’}’.
Case
Thecaseconstructhasthefollowingsyntax:
casewordin
pattern)list;;
…
esac
Anexampleofthisshouldmakethingsclearer:
!#/bin/sh
case$1
in
1)echo‘FirstChoice’;;
2)echo‘SecondChoice’;;
*)echo‘OtherChoice’;;
esac
“1”,“2”and“*”arepatterns,wordiscomparedtoeachpatternandifamatchis
found the body of the corresponding pattern is executed, we have used “*” to
represent everything, since this is checked last we will still catch “1” and “2”
becausetheyarecheckedfirst. Inourexampleword is“$1”, the firstparameter,
hence if thescript is ranwith theargument “1” itwilloutput “FirstChoice”, “2”
“SecondChoice”andanythingelse“OtherChoice”.Inthisexamplewecompared
against numbers (essentially still a string comparison however) but the pattern
canbemorecomplex,seetheSHmanpageformoreinformation.
Functions
ThesyntaxofanSHfunctionisdefinedasfollows:
name()command
Itisusuallylaidoutlikethis:
name(){
commands
}
Afunctionwillreturnwithadefaultexitstatusofzero,onecanreturndifferent
exitstatusesbyusingthenotationreturnexitstatus.Variablescanbedefined
locallywithinafunctionusinglocalname=value.Theexamplebelowshowsthe
useofauserdefinedincrementfunction:
IncrementFunctionExample#!/bin/sh
inc(){ #Theincrementisdefinedfirstsowecanuseit
echo$(($1+$2))#Weechotheresultofthefirstparameterplusthesecondparameter
}
#Wechecktoseethatallthecommandlineargumentsarepresent
if[“$1”””]||[“$2”=””]||[“$3”=””]
then
echoUSAGE:
echo”counterstartvalueincrementvalueendvalue”
else
count=$1#Renamearevariableswithclearernames
value=$2
end=$3
while[$count-lt$end]#Loopwhilecountislessthanend
do
echo$count
count=$(inc$count$value) #Callincrementwithcountandvalueasparameters
done#sothatcountisincrementedbyvalue
fi
inc(){
echo$(($1+$2))
}
Thefunction isdefinedandopenedwith inc(){, the lineecho$(($1+$2))uses
the notation for arithmetic expression substitution which is $((expression)) to
enclose the expression, $1 + $2 which adds the first and second parameters
passedtothefunctiontogether,theechobitatthestartechoesthemtostandard
output,wecancatch thisvaluebyassigning the functioncall toavariable,as is
illustratedbythefunctioncall.
count=$(inc$count$value)
We use command substitution which substitutes the value of a command to
substitute the value of the function call whereupon it is assigned to
thecount variable.The commandwithin the command substitutionblock is inc
$count $value, the last two values being its parameters. Which are then
referenced from within the function using $1 and $2.We could have used the
othercommandsubstitutionnotationtocallthefunctionifwehadwanted:
count=`inc$count$value`
Hereisanotherexampleillustratingthescopeofvariables:
VariableScope,Example
#!/bin/sh
inc(){
localvalue=4
echo“valueis$valuewithinthefunction\n”
echo“\b\$1is$1withinthefunction”
}
value=5
echovalueis$valuebeforethefunction
echo“\$1is$1beforethefunction”
echo
echo-e$(inc$value)
echo
echovalueis$valueafterthefunction
echo“\$1is$1afterthefunction”
inc(){
localvalue=4
echo“valueis$valuewithinthefunction\n”
echo“\b\$1is$1withinthefunction”
}
Weassigna localvaluetothevariablevalueof4.Thenextthree linesconstruct
theoutput,rememberthatthisisbeingechoedtoabufferandwillbereplacethe
function call with all that was passed to stdout within the function when the
function exits. So, the calling code will be replaced with whatever we direct to
standardoutputwithinthefunction.Thefunctioniscalledlikethis:
echo-e$(inc$value)
Wehavepassedtheoption-etotheechocommandwhichcausesittoprocessC-style backslash escape characters, so we can process any backslash escapecharacterswhichthestringgeneratedbythefunctioncallcontains.Ifwejustechothe lines we want to be returned by the function it will not pass the newlinecharacterontothebufferevenifweexplicitlyincludeitwithanescapecharacterreference sowhatwe do is actually include the sequence of characters thatwill
produceanewlinewithinthestringsothatwhenitisechoedbythecallingcodewith the -e the escape characters will be processed and the newlines will beplacedwherewewantthem.echo“valueis$valuewithinthefunction\n”
Notice how the newline has been inserted with \n, the first two backslashesindicate that we want to echo a backslash because within double quotes abackslash indicates to process the next character literally, we have to do thisbecauseweareonlybetweendoublequotesandnottheliteral-textsinglequotes.Ifwehadusedsinglequoteswewouldhadhavetoechothebitwiththenewlineinseparately from the bit that contains $value otherwise $value would not beexpanded.echo“\b\$1is$1withinthefunction”
This is our second line, and is contained within double quotes so that thevariable $1 will be expanded, \b is included so that \b will be placed in theechoedlineandourcallingcodeprocessesthisasabackspacecharacter.Ifwedonotdo that the shellprefixesa space to the second line, thebackspace removesthisspace.
Theoutputfromthisscriptcalledwith2asthefirstargumentis:
valueis5beforethefunction
$1is2beforethefunction
valueis4withinthefunction
$1is5withinthefunction
valueis5afterthefunction
$1is2afterthefunction
Tip:Youcanuse“.DIRECTORY/common.sh”toimportfunctionsfromascriptcalled common.sh in DIRECTORY, a quick example is shown below, first istest.sh:#!/bin/sh
../common.sh
if[“$1”=””];then
echoUSAGE:
echo“shtest.shtype”
exit
fi
if`validtype$1`;then
echoValidtype
else
echoInvalidtype
fi
Hereiscommon.sh:#!/bin/sh
validtype(){
if[“$1”=“TYPEA”]||
[“$1”=“TYPEB”]||
[“$1”=“TYPEC”]||
[“$1”=“TYPED”]||
[“$1”=“TYPEE”];
then
exit0
else
exit1
fi
}
ChapterEleven:SHELLBASICOPERATOR
Each shell supports various operators. This chapter is based on default shell
(Bourne)sowearegoingtocoveralltheimportantBourneShelloperatorsinthis
tutorial.
Wewilldiscussthefollowingoperators−
ArithmeticOperators.
RelationalOperators.
BooleanOperators.
StringOperators.
FileTestOperators.
The Bourne shell did not originally have any mechanism to perform simplearithmeticbutitusesexternalprograms,eitherawkorthemustsimplerprogramexpr.
Hereissimpleexampletoaddtwonumbers–
#!/bin/sh
val=`expr2+2`
echo“Totalvalue:$val”
Thisproducesthefollowingresult−Totalvalue:4
Note:
Theremustbespacesbetweenoperatorsandexpressionsforexample
2+2isnotcorrect,itshouldbewrittenas2+2.
Completeexpressionsshouldbeenclosedbetween“,invertedcommas.
ArithmeticOperators
ThefollowingarearithmeticoperatorssupportedbytheBourneShell.
Assumevariable‘a’holds10andvariable‘b’holds20then−
Example,usingallthearithmeticoperators−
#!/bin/sh
a=10
b=20
val=`expr$a+$b`
echo“a+b:$val”
val=`expr$a-$b`
echo“a-b:$val”
val=`expr$a\*$b`
echo“a*b:$val”
val=`expr$b/$a`
echo“b/a:$val”
val=`expr$b%$a`
echo“b%a:$val”
if[$a==$b]
then
echo“aisequaltob”
fi
if[$a!=$b]
then
echo“aisnotequaltob”
fi
Thisproducesthefollowingresult−a+b:30
a-b:-10
a*b:200
b/a:2
b%a:0
aisnotequaltob
Note:
Theremustbespacesbetweenoperatorsandexpressionsforexample2+2isnotcorrect,whereasitshouldbewrittenas2+2.
Completeexpressionshouldbeenclosedbetween“,invertedcommas.
Youshoulduse\onthe*symbolformultiplication.
Theif…then…fistatementisadecision-makingstatement,whichwillbeexplainedinthenextchapter.
Operator Description Example
+ Addition-Addsvaluesoneithersideoftheoperator
`expr$a+$b`willgive30
- Subtraction-Subtractsrighthandoperandfromlefthandoperand
`expr$a-$b`willgive-10
* Multiplication-Multipliesvaluesoneithersideoftheoperator
`expr$a\*$b`willgive200
/ Division-Divideslefthandoperandbyrighthandoperand
`expr$b/$a`willgive2
% Modulus-Divideslefthandoperandbyrighthandoperandandreturnsremainder
`expr$b%$a`willgive0
= Assignment-Assignrightoperandinleftoperand
a=$bwouldassignvalueofbintoa
== Equality-Comparestwonumbers,ifbotharesamethenreturnstrue.
[$a==$b]wouldreturnfalse.
!= NotEquality-Comparestwonumbers,ifbotharedifferentthenreturnstrue.
[$a!=$b]wouldreturntrue.
It is very important to note that all the conditional expressions should be put
insidesquarebraceswithonespacesaroundthem, forexample [$a==$b] is
correctwhereas[$a==$b]isincorrect.
Allthearithmeticalcalculationsaredoneusinglongintegers.
RelationalOperators:
Bourne Shell supports the following relational operators, which are specific tonumericvalues.Theseoperatorswillnotworkforstringvaluesunlesstheirvalueisnumeric.Forexample,thefollowingoperatorscheckarelationbetween10and20aswell as inbetween “10” and “20”butnot inbetween “ten” and “twenty”.Assumevariableaholds10andvariablebholds20then−
Thisexampleusesalltherelationaloperators−#!/bin/sh
a=10
b=20
if[$a-eq$b]
then
echo“$a-eq$b:aisequaltob”
else
echo“$a-eq$b:aisnotequaltob”
fi
if[$a-ne$b]
then
echo“$a-ne$b:aisnotequaltob”
else
echo“$a-ne$b:aisequaltob”
fi
if[$a-gt$b]
then
echo“$a-gt$b:aisgreaterthanb”
else
echo“$a-gt$b:aisnotgreaterthanb”
fi
if[$a-lt$b]
then
echo“$a-lt$b:aislessthanb”
else
echo“$a-lt$b:aisnotlessthanb”
fi
if[$a-ge$b]
then
echo“$a-ge$b:aisgreaterorequaltob”
else
echo“$a-ge$b:aisnotgreaterorequaltob”
fi
if[$a-le$b]
then
echo“$a-le$b:aislessorequaltob”
else
echo“$a-le$b:aisnotlessorequaltob”
fi
Thisproducesthefollowingresult−10-eq20:aisnotequaltob
10-ne20:aisnotequaltob
10-gt20:aisnotgreaterthanb
10-lt20:aislessthanb
10-ge20:aisnotgreaterorequaltob
10-le20:aislessorequaltob
Note:
Theremustbespacesbetweenoperatorsandexpressionsforexample
2+2isnotcorrect,whereasitshouldbewrittenas2+2.
if…then…else…fistatementisadecisionmakingstatementwhich
willbeexplainedinthenextchapter.
Operator Description Example
-eq Checksifthevalueoftwooperandsareequal,ifyesthenconditionbecomestrue.
[$a-eq$b]isnottrue.
-ne Checksifthevalueoftwooperandsareequal,ifvaluesarenotequalthenconditionbecomestrue.
[$a-ne$b]istrue.
-gt Checksifthevalueofleftoperandisgreaterthanthevalueofrightoperand,ifyesthenconditionbecomestrue.
[$a-gt$b]isnottrue.
-lt Checksifthevalueofleftoperandislessthanthevalueofrightoperand,ifyesthenconditionbecomestrue.
[$a-lt$b]istrue.
-ge Checksifthevalueofleftoperandisgreaterthanorequaltothevalueofrightoperand,ifyesthenconditionbecomestrue.
[$a-ge$b]isnottrue.
-le Checksifthevalueofleftoperandislessthanorequaltothevalueofrightoperand,ifyesthenconditionbecomes
[$a-le$b]istrue.
true.
It is very important to note that all the conditional expressions should be put
insidesquarebraceswithonespacesaroundthem, forexample [$a<=$b] is
correctwhereas[$a<=$b]isincorrect.
BooleanOperators
TheBourneShellsupportsthefollowingbooleanoperators.
Assumevariable‘a’holds10andvariable‘b’holds20then−
Thisexampleusesallthebooleanoperators−#!/bin/sh
a=10
b=20
if[$a!=$b]
then
echo“$a!=$b:aisnotequaltob”
else
echo“$a!=$b:aisequaltob”
fi
if[$a-lt100-a$b-gt15]
then
echo“$a-lt100-a$b-gt15:returnstrue”
else
echo“$a-lt100-a$b-gt15:returnsfalse”
fi
if[$a-lt100-o$b-gt100]
then
echo“$a-lt100-o$b-gt100:returnstrue”
else
echo“$a-lt100-o$b-gt100:returnsfalse”
fi
if[$a-lt5-o$b-gt100]
then
echo“$a-lt100-o$b-gt100:returnstrue”
else
echo“$a-lt100-o$b-gt100:returnsfalse”
fi
Thisproducesthefollowingresult−10!=20:aisnotequaltob
10-lt100-a20-gt15:returnstrue
10-lt100-o20-gt100:returnstrue
10-lt5-o20-gt100:returnsfalse
Note:
Theremustbespacesbetweenoperatorsandexpressionsforexample
2+2isnotcorrect,whereasitshouldbewrittenas2+2.
Operator Description Example
! Thisisalogicalnegation.Thisinvertsatrueconditionintofalseandviceversa.
[!false]istrue.
-o ThisisalogicalOR.Ifoneoftheoperandsweretruethenconditionwouldbetrue.
[$a-lt20-o$b-gt100]istrue.
-a ThisisalogicalAND.Ifboththeoperandsweretruethenconditionwouldbetrueotherwise,itwouldbefalse.
[$a-lt20-a$b-gt100]isfalse.
StringOperators
TheBourneShellsupportsthefollowingstringoperators.
Assumevariable‘a’holds“abc”andvariable‘b’holds“efg”then−
Thisexampleusesallthestringoperators−#!/bin/sh
a=“abc”
b=“efg”
if[$a=$b]
then
echo“$a=$b:aisequaltob”
else
echo“$a=$b:aisnotequaltob”
fi
if[$a!=$b]
then
echo“$a!=$b:aisnotequaltob”
else
echo“$a!=$b:aisequaltob”
fi
if[-z$a]
then
echo“-z$a:stringlengthiszero”
else
echo“-z$a:stringlengthisnotzero”
fi
if[-n$a]
then
echo“-n$a:stringlengthisnotzero”
else
echo“-n$a:stringlengthiszero”
fi
if[$a]
then
echo“$a:stringisnotempty”
else
echo“$a:stringisempty”
fi
Thisproducesthefollowingresult−abc=efg:aisnotequaltob
abc!=efg:aisnotequaltob
-zabc:stringlengthisnotzero
-nabc:stringlengthisnotzero
abc:stringisnotempty
Note:
Theremustbespacesbetweenoperatorsandexpressionsforexample
2+2isnotcorrect,whereasitshouldbewrittenas2+2.
Operator Description Example
= Checksifthevalueoftwooperandsisequal,ifyesthenconditionbecomestrue.
[$a=$b]isnottrue.
!= Checksifthevalueoftwooperandsareequal,ifvaluesarenotequalthenconditionbecomestrue.
[$a!=$b]istrue.
-z Checksifthegivenstringoperandsizeiszero.Ifitiszerolengththenitreturnstrue.
[-z$a]isnottrue.
-n Checksifthegivenstringoperandsizeisnon-zero.Ifitisnon-zerolengththenitreturnstrue.
[-z$a]isnotfalse.
str Checkifstrisnottheemptystring.Ifitisemptythenitreturnsfalse.
[$a]isnotfalse.
FileTestOperators
ThefollowingoperatorstestvariouspropertiesassociatedwithaUnixfile.
Assumeavariablefileholdsanexistingfilename“test”whosesizeis100bytesandhasread,writeandexecutepermissions−
Thisexampleusesallthefiletestoperators−#!/bin/sh
file=“/var/www/tutorialspoint/unix/test.sh”
if[-r$file]
then
echo“Filehasreadaccess”
else
echo“Filedoesnothavereadaccess”
fi
if[-w$file]
then
echo“Filehaswritepermission”
else
echo“Filedoesnothavewritepermission”
fi
if[-x$file]
then
echo“Filehasexecutepermission”
else
echo“Filedoesnothaveexecutepermission”
fi
if[-f$file]
then
echo“Fileisanordinaryfile”
else
echo“Thisisspecialfile”
fi
if[-d$file]
then
echo“Fileisadirectory”
else
echo“Thisisnotadirectory”
fi
if[-s$file]
then
echo“Filesizeiszero”
else
echo“Filesizeisnotzero”
fi
if[-e$file]
then
echo“Fileexists”
else
echo“Filedoesnotexist”
fi
Thisproducesthefollowingresult−Filehasreadaccess
Filehaswritepermission
Operator Description Example
-bfile Checksiffileisablockspecialfileifyesthenconditionbecomestrue.
[-b$file]isfalse.
-cfile Checksiffileisacharacterspecialfileifyesthenconditionbecomestrue.
[-c$file]isfalse.
-dfile Checkiffileisadirectoryifyesthenconditionbecomestrue.
[-d$file]isnottrue.
-ffile Checkiffileisanordinaryfileasopposedtoadirectoryorspecialfileifyesthenconditionbecomestrue.
[-f$file]istrue.
-gfile ChecksiffilehasitssetgroupID(SGID)bitsetifyesthenconditionbecomestrue.
[-g$file]isfalse.
-kfile Checksiffilehasitsstickybitsetifyesthenconditionbecomestrue.
[-k$file]isfalse.
-pfile Checksiffileisanamedpipeifyesthenconditionbecomestrue.
[-p$file]isfalse.
-tfile Checksiffiledescriptorisopenandassociatedwithaterminalifyesthenconditionbecomestrue.
[-t$file]isfalse.
-ufile Checksiffilehasitssetuserid(SUID)bitsetifyesthenconditionbecomestrue.
[-u$file]isfalse.
-rfile Checksiffileisreadableifyesthenconditionbecomestrue.
[-r$file]istrue.
-wfile Checksiffileiswritableifyesthenconditionbecomestrue.
[-w$file]istrue.
-xfile Checksiffileisexecuteifyesthen [-x$file]is
Filehasexecutepermission
Fileisanordinaryfile
Thisisnotadirectory
Filesizeiszero
Fileexists
Note:
Theremustbespacesbetweenoperatorsandexpressionsforexample
2+2isnotcorrect,whereasitshouldbewrittenas2+2.
RunThe.ShFileShell
ScriptInLinux/Unix
After downloading
thesoftware,the.shfileis
nothing but the shell
script to install a given
application or to perform
other tasks under UNIX
like operating systems.
Theeasiestwaytorun.sh
shell script in Linux or
UNIX is to type either of
the following commands.
Open the terminal (your
shellprompt)andtypethe
command:
shfile.sh
ORbashfile.sh
.shAsRootUser
Sometimes you will need
to install an application
that requires root level
conditionbecomestrue. true.
-sfile Checksiffilehassizegreaterthan0ifyesthenconditionbecomestrue.
[-s$file]istrue.
-efile Checksiffileexists.Istrueeveniffileisadirectorybutexists.
[-e$file]istrue.
privileges.
Rootaccess isdisabledby
default on many Linux
and UNIX like systems.
Simply use sudo or su as
follows:
sudobashfilename.sh
Type your password. Another option is to use the su command as follows tobecomesuperuser:su-
Typerootuserpasswordandfinallyrunyourscript:bashfilename.sh
chmodCommand:RunShellScriptInLinux
Anotherrecommendedoptionistosetanexecutablepermissionusingthechmodcommandasfollows:chmod+xfile.sh
Nowyoucanrunthe.shfile../file.sh
ChapterTwelve:SHELLEMBEDDINGANDOPTIONS
Thischapterinvestigatestykeshells,implantedshellsandshellalternatives.
Shellinstalling
Shellscanbe installedtoworkonacommandline; thecommandlinecanbring
forthnewprocedurescontainingaforkofthepresentshell.Youcanusevariables
todemonstratethatthenewshellsaremade.Thevariable$var1justexistsinthe
(interim)subshell.
[paul@RHELv4u3gen]$reverberation$var1
[paul@RHELv4u3gen]$reverberation$(var1=5;echo$var1)
5
[paul@RHELv4u3gen]$reverberation$var1
[paul@RHELv4u3gen]$
You can install a shell into an inserted shell; this is called settled implantingof
shells.
paul@deb503:~$A=shell
paul@deb503:~$ reverberation $C$B$A $(B=sub;echo $C$B$A; reverberation$(C=sub;echo$C$B$A))
shellsubshellsubsubshell
Backticks
Single installation can change your present index and uses backticks instead of
dollarsectiontoimplant.
[paul@RHELv4u3~]$reverberation`cd/andsoforth;ls-d*|greppass`
passwd-passwd.OLD
[paul@RHELv4u3~]$
Thebackticks/()andthe$()documentationcanbeusedtoimplantashell.
Backticksorsinglequotes
Putting the installing between backticks utilizes one character not exactly the
dollarandbracketcombo.Becautiousontheotherhand,backticksarefrequently
mistakenforsinglequotes.
Thespecializedcontrastinthemiddleof”and`iscritical![paul@RHELv4u3gen]$reverberation`var1=5;echo$var1`
[paul@RHELv4u3gen]$reverberation‘var1=5;echo$var1’
var1=5;echo$var1
[paul@RHELv4u3gen]$
ShellAlternatives
Both set and unset are built-in shell commands, which are used to setalternativestothebashshell.Thefollowingcasewillclearupthis.Theshellwilltreatunsetvariablesasanunimportantvariable.Bysetting the-ualternative,theshellwilltreatanyreferencetounsetvariablesasanerror.[paul@RHEL4b~]$reverberation$var123
[paul@RHEL4b~]$set-u
[paul@RHEL4b~]$reverberation$var123
-bash:var123:unboundvariable
[paul@RHEL4b~]$set+u
[paul@RHEL4b~]$reverberation$var123
[paul@RHEL4b~]$
Torundownallthesetchoicesforyourshell,usereverberation$-.Thenoclobber
(or-C).
[paul@RHEL4b~]$reverberation$-
himBH
[paul@RHEL4b~]$set-C;set-u
[paul@RHEL4b~]$reverberation$-
himuBCH
[paul@RHEL4b~]$set+C;set+u
[paul@RHEL4b~]$reverberation$-
himBH
[paul@RHEL4b~]$
Whenwritingasetwithoutalternatives,yougetarundownofallvariableswithout
capacity,whentheshellisinposixmode,youcansetbashinposixmodewriting
set-oposix.
Practice:ShellInstalling
1. Discovertherundownofshellchoicesinthemanpageofbash.Whatis
thecontrastbetweensituated-uandset-onounset?
2. Actuatenounsetinyourshell.Testifitdemonstratesanerrormessage
whenusingnon-existingvariables.
3. Deactivatenounset.
4. Executedisc/varandlsinaninstalledshell.
Thereverberationcommand isexpected todemonstrate theaftereffectsofthe ls command. Precluding will cause the shell to execute the firstdocumentasacommand.
5. Addthevariableembvarintoaninsertedshellandreverberationit.Doesthevariableexistinyourpresentshellnow?
6. Clarifywhat“set-x”does.Canthisbeuseful?
(Optional)
7. Giventheaccompanyingscreenshot,addfourcharacterstothe
commandsothattheaggregateyieldisFirstMiddleLast.
[paul@RHEL4b~]$reverberateFirst;resoundMiddle;reverberate
Last.
8. Displayalonglisting(ls-l)ofthepasswdcommandutilizingthewhichcommandinsideaninstalledshell.
15.4.Arrangement:shellinserting
1. Discovertherundownofshellalternativesinthemanpageofbash.
Whatisthedistinctionbetweensituated
uandset-onounset?
Checkthemanofbashandlookfornounset-bothmeanthesamething.
2. Addnounsettoyourshell.Testthatitdemonstratesalapsemessage
whenutilizingnonexistingvariables.
set-u
Or
set-onounset
Bothlineshavethesameimpact.
3. Deactivatenounset.
set+u
Or
set+onounset
4. Executealbum/varandlsinanimplantedshell.
reverberation$(cd/var;ls)
Thereverberationcommandisusedtodemonstratetheconsequenceofthe
lscommand.Precludingwillmaketheshelltoexecutethefirstdocumentas
acommand.
5.Makethevariableembvarinanimplantedshellandreverberationit.Doesthevariableexistinyourpresentshell?
reverberation$(embvar=emb;echo$embvar) ;reverberation$embvar#the
lastresoundfizzles
$embvardoesnotexistinyourpresentshell
6. Clarifywhat“set-x”does.Isthisuseful?
Itshowsshelldevelopmentforinvestigatingyourcommand.
(Optional)
7. Giventheaccompanyingscreenshot,addfourcharacterstothe
commandlinesothattheaggregateyieldisFirstMiddleLast.
[paul@RHEL4b~]$reverberateFirst;resoundMiddle;reverberateLast
reverberation-nFirst;resound-nMiddle;resoundLast
8. Showalongposting(ls-l)ofthepasswdcommandusingthe“whichcommand”insideanimplantedshell.
ls-l$(whichpasswd
Almostall-modernshellallowsyoutosearchcommandhistoryifenabledbythe
user.Historycommandcandisplaythehistorylistwithlinenumbers,listedwitha
*havebeenmodifiedbyuser.
ChapterThirteen:SHELLHISTORYSEARCHCOMMAND
Typehistoryatashellprompt:$history
Output:6du-c
7du-ch
8ls[01-15]*-2008
9ls-ld[01-15]*-2008
10ls-ld[1-15]*-2008
11ls-ld[0]*-2008
12ls-ld[01]*-2008
13rm-vrf[01]*-2008
14du-ch
15ls
16cd
17umount/mnt
18df-H
19vnstat
20yumupdate
21vnstat-m
22vnstat-m-ieth0
….
…
996pingrouter.nixcraft.in
998alias
999~/scripts/clean.rss—fetch
1000vnstat
1001~/scripts/clean.rss—update
Tosearchforaparticularcommand,enter:$history|grepcommand-name
$history|egrep-i‘scp|ssh|ftp’
EmacsLine-EditModeCommandHistorySearching
Tofindthecommandcontainingstring,hit[CTRL]+[r]followedbysearchstring:(reverse-i-search):
Toshowthepreviouscommand,hit[CTRL]+[p].Youcanalsouseuparrowkey.CTRL-p
Toshowthenextcommand,hit[CTRL]+[n].Youcanalsousedownarrowkey.CTRL-n
fccommand
fcstandsforeitherthefindcommandorthefixcommand.Forexampletolistthelast10commands,enter:$fc-l10
tolistcommands130through150,enter:$fc-l130150
tolistallcommandssincethelastcommandbeginningwithssh,enter:$fc-lssh
Youcaneditcommands1through5usingvitexteditor,enter:$fc-evi15
Deletecommandhistory
The-coptioncausesthehistorylisttobeclearedbydeletingalloftheentries:$history-c
FILENAMEGLOBBINGWITH*,?,[]
Sometimesyouwanttomanipulateagroupoffiles,e.g.,deleteallofthem,
without having to perform the command on each file separately. For example,
supposewewanttodeleteallofthe.cfilesinadirectory.Awildcardisapattern,
whichmatchessomethingelse.Twocommonlyused*nixwildcardsare*(star)
and?(questionmark).
Star(*)meanszeroormorecharacters
QuestionMark(?)meansexactlyonecharacter
Brackets([])representasetofcharacters
Commandsinvolvingfilenamesspecifiedwithwildcardsareexpandedbytheshell
(this is calledglobbingafter thenameof a formerprogramcalledglobwhich
usedtodothisoutsidetheshell).
The‘*’
‘file*’willmatchanyfilenamewhichstartswiththecharacters“file”,andthenis
followedbyzeroormoreoccurrencesofanycharacter.
Examples
SupposeFred’shomedirectorycontainsthefiles,
file01.cpp
file02.cpp
file03.cpp
file1.cpp
file01.o
file02.o
file03.o
file1.o
Todeleteallofthe.cfiles,type,
$rm*.c
Todeletefile01.cppandfile01.o,$rmfile01.*
The‘?’
‘?’Representsasinglecharacter.
Examples
ConsideragainFred’shomedirectoryfromthepreviousexample.
Deletefile01.o,file02.oandfile03.o,butnotfile1.o,
$rmfile??.o
Deletefile01.o,butnotfile01.cpp,$rmfile01.?
The‘[]’Glob
A set of characters can be specified with brackets [ ]. ‘[ab]‘ meansthesinglecharactercanbe‘a‘OR‘b’.Rangescanalsobespecified(ex:‘[1-57-9]‘represents1-5OR7-9).
Examples
Deletefile02.cppandfile03.cppfromFred’sdirectory,
$rmfile0[23].cpp
ThiswilldeleteanyfilesthatstartwithforF(rememberlinuxiscasesensitive),$rm[fF]*
Todeleteallfilesthatstartwiththestring“file”followedbyasinglelettertype,$rmfile[a-zA-Z]
Thea-zandA-Zinthelastexamplemeansallthelettersintherangelowercasea-z
oruppercaseA-Z.
Thereismuchmoretowildcardmatching,butthisisenoughtogetyoustarted.
MoreExamples
Removeallfilesthatareexactly1character,
$rm?
Let’s say you have a directory named ‘9-15-2007-Backup-Really-Long-Name-blah…’Rather than typing thewhole name, you could just type a subset of thestringanduseitwiththecdcommand.
$cd9-15-2007*
Ifyouhavemultiplefoldersthatstartwith9-15-2007,yourdirectorywillbe
changedtothefirstonealphabetically.
Youcanusefilenameglobbingonmostcommandsthatacceptfilesasarguments.
Chapterfourteen:UNIX-SHELLINPUT/OUTPUTREDIRECTIONS
Timeisprecious.Itdoesnotmakesensetowastetimetypingafrequentlyusedsequenceofcommandsatacommandprompt,moreespeciallyiftheyareabnormallylongorcomplex.Scriptingisawaybywhichonecanalleviatethisproblembyautomatingthesecommandsequencesinordertomakelifeattheshelleasierandmoreproductive.Scriptingisallaboutmakingthecomputer,thetool,dothework.BytheendofthistutorialyouwillhaveagoodunderstandingofthekindofscriptinglanguagesavailableforUnixandhowtoapplythemtoyourproblems.UNIXcontainsmanywonderfulandstrangecommandsthatcanbeveryusefulintheworldofscripting,themoretoolsyouknowandthebetteryouknowthem,themoreuseyouwillfindforthem.MostoftheUnixcommandsandmanyofthebuilt-incommandshavemanpages;manpagescontaintheusageinstructionspertainingtotheparenttool.Theyarenotalwaysveryclearandmayrequirereadingseveraltimes.InordertoaccessamanpageinUnixthefollowingcommandsequenceisapplied
ChapterFifteen:UNIXSHELLFUNCTION
Functionsenableyoutobreakdowntheoverallfunctionalityofascriptintosmaller, logical subsections, which are then called upon to perform theirindividual taskswhenneeded.Using functions toperformrepetitive tasks isanexcellentway to create code reuse.Code reuse is an important part ofmodernobject-oriented programming principles. Shell functions are similar tosubroutines,procedures,andfunctionsinotherprogramminglanguages.
CreatingFunctions
Todeclareafunction,simplyusethefollowingsyntax−function_name(){
listofcommands
}
Thenameofyourfunctionisfunction_name,andthatiswhatyouwillusetocall
it from elsewhere in your scripts. The function name must be followed by
parentheses,whicharefollowedbyalistofcommandsenclosedwithinbraces.
Example
Exampleusingfunction−
#!/bin/sh
#Defineyourfunctionhere
Hello(){
echo“HelloWorld”
}
#Invokeyourfunction
Hello
Output$./test.sh
HelloWorld
$
PassParameterstoaFunction
You can define a function, which would accept parameters while calling
thosefunctions.Theseparameterswouldberepresentedby$1,$2andsoon.The
followingisanexamplewherewepasstwoparametersZaraandAliandthenwe
captureandprinttheseparametersinthefunction.
#!/bin/sh
#Defineyourfunctionhere
Hello(){
echo“HelloWorld$1$2”
}
#Invokeyourfunction
HelloZaraAli
Output$./test.sh
HelloWorldZaraAli
$
ReturningValuesfromFunctions
Ifyouexecuteanexitcommandfrominsideafunction,itseffectisnotonlytoterminateexecutionofthefunctionbutalsooftheshellprogramthatcalledthefunction. If you insteadwant to just terminate execution of the function, thenthereisawaytocomeoutofadefinedfunction.Basedonthesituationyoucanreturnanyvaluefromyourfunctionusingthereturncommandwhosesyntaxisasfollows−returncode
Here code can be anything you choose, but obviously you should choosesomethingthatismeaningfulorusefulinthecontextofyourscriptasawhole.
Example
Followingfunctionreturnsavalue1−#!/bin/sh
#Defineyourfunctionhere
Hello(){
echo“HelloWorld$1$2”
return10
}
#Invokeyourfunction
HelloZaraAli
#Capturevaluereturnedbylastcommand
ret=$?
echo“Returnvalueis$ret”
Output$./test.sh
HelloWorldZaraAli
Returnvalueis10
$
NestedFunctions
Oneofthemoreinterestingfeaturesoffunctionsisthattheycancallthemselves
aswellascallotherfunctions.Afunctionthatcallsitselfisknownasarecursive
function.
Thefollowingexampledemonstratesanestingoftwofunctions−
#!/bin/sh
#Callingonefunctionfromanother
number_one(){
echo“Thisisthefirstfunctionspeaking…”
number_two
}
number_two(){
echo“Thisisnowthesecondfunctionspeaking…”
}
#Callingfunctionone.
number_one
OutputThisisthefirstfunctionspeaking…
Thisisnowthesecondfunctionspeaking…
FunctionCallfromPrompt
Youcanputdefinitionsforcommonlyusedfunctionsinsideyour.profilesothey
willbeavailablewheneveryoulogin.Alternatively,youcangroupthedefinitions
inafile,saytest.sh,andthenexecutethefileinthecurrentshellbytyping−
$.test.sh
Thishas theeffectof causingany functionsdefined inside test.sh tobe read in
anddefinedtothecurrentshellasfollows−
$number_one
Option Description
-v Printalllinesthatdonotmatchpattern.
-n Printthematchedlineanditslinenumber.
-l Printonlythenamesoffileswithmatchinglines(letter“l”)
Thisisthefirstfunctionspeaking…
Thisisnowthesecondfunctionspeaking…
$
Toremovethedefinitionofafunctionfromtheshell,youusetheunsetcommand
withthe.foption.Thisisthesamecommandyouusetoremovethedefinitionof
avariabletotheshell.
$unset.ffunction_name
Unix-PipesandFilters
You can connect two commands together so that the output from oneprogram becomes the input of the next program. Two or more commandsconnectedinthiswayformapipe.Tomakeapipe,putaverticalbar(|)onthecommand line between two commands.When a program takes its input fromanotherprogram,performssomeoperationonthatinput,andwritestheresulttothestandardoutput,itisreferredtoasafilter.
ThegrepCommand
Thegrepprogramsearchesafileorfilesforlinesthathaveacertainpattern.Thesyntaxis−$greppatternfile(s)
The name “grep” derives from the ed (a UNIX line editor) commandg/re/p,whichmeans,“globallysearchforaregularexpressionandprintalllinescontaining it.” A regular expression is either plain text (a word, for example)and/orspecialcharactersusedforpatternmatching.Thesimplestuseofgrepistolookforapatternconsistingofasingleword.Itcanbeusedinapipesothatonly those lines of the input files containing a given string are sent to thestandardoutput.Ifyoudonotgivegrepafilenametoread,itreadsitsstandardinput;thatisthewayallfilterprogramswork−$ls-l|grep“Aug”
-rw-rw-rw-1johndoc11008Aug614:10ch02
-rw-rw-rw-1johndoc8515Aug615:30ch07
-rw-rw-r—1johndoc2488Aug1510:51intro
-rw-rw-r—1caroldoc1605Aug2307:35macros
$
There are various options,whichyoucanusealongwithgrepcommand–
Next, let’s use a regularexpression that tellsgrep tofind lines with “carol”,followed by zero or more
-c Printonlythecountofmatchinglines.
-i Matcheitherupper-orlowercase.
Option Description
-n Sortnumerically(example:10willsortafter2),ignoreblanksandtabs.
-r Reversetheorderofsort.
-f Sortupper-andlowercasetogether.
+x Ignorefirstxfieldswhensorting.
other characters abbreviatedin a regular expression as“.*”), then followedby“Aug”.Hereweareusing-ioptionto
havecaseinsensitivesearch−$ls-l|grep-i“carol.*aug”
-rw-rw-r—1caroldoc1605Aug2307:35macros
$
ThesortCommand
Thesort command arranges lines of text alphabetically or numerically. Theexamplebelowsortsthelinesinthefoodfile−$sortfood
AfghaniCuisine
BangkokWok
BigAppleDeli
IsleofJava
Mandalay
SushiandSashimi
SweetTooth
TioPepe‘sPeppers
$
Thesort command arrangeslines of text alphabetically bydefault. There are manyoptionsthatcontrolthesorting−
Morethantwocommandsmaybe linked by a pipe. Taking aprevious pipe exampleusinggrep,wecanfurthersortthefilesmodifiedinAugustby
orderofsize.
Thefollowingpipeconsistsofthecommandsls,grep,andsort−$ls-l|grep“Aug”|sort+4n
-rw-rw-r—1caroldoc1605Aug2307:35macros
-rw-rw-r—1johndoc2488Aug1510:51intro
-rw-rw-rw-1johndoc8515Aug615:30ch07
-rw-rw-rw-1johndoc11008Aug614:10ch02
$
ThispipesortsallfilesinyourdirectorymodifiedinAugustbyorderofsize,and
prints themtotheterminalscreen.Thesortoption+4nskips four fields(fields
areseparatedbyblanks)thensortthelinesinnumericorder.
ThepgandmoreCommands
A long output would normally zip by you on the screen, but if you run textthroughmoreorpgasafilter,thedisplaystopsaftereachscreenoftext.Letusassumethatyouhavealongdirectorylisting.Tomakeiteasiertoreadthesortedlisting,pipetheoutputthroughmoreasfollows−$ls-l|grep“Aug”|sort+4n|more
-rw-rw-r—1caroldoc1605Aug2307:35macros
-rw-rw-r—1johndoc2488Aug1510:51intro
-rw-rw-rw-1johndoc8515Aug615:30ch07
-rw-rw-r—1johndoc14827Aug912:40ch03
.
.
.
-rw-rw-rw-1johndoc16867Aug615:56ch05
—More—(74%)
Thescreenwillfillupwithonescreenoftextconsistingoflinessortedbyorderoffilesize.Atthebottomofthescreenis themorepromptwhereyoucantypeacommandtomovethroughthesortedtext.Whenyouaredonewiththisscreen,youcanuseanyofthecommandslistedinthediscussionofthemoreprogram.
ChapterSixteen:UNIXUSEFULCOMMAND
This quick guide lists commands, including a syntax andbrief description. Formoredetailedinformationcheckoutcommandsmanpage.$mancommand
FilesandDirectories
Thesecommandsallowyoutocreatedirectoriesandhandlefiles.
Command Description
cat DisplayFileContents
cd ChangesDirectorytodirname
chgrp Changefilegroup
chmod ChangingPermissions
cp Copysourcefileintodestination
file Determinefiletype
find Findfiles
grep Searchfilesforregularexpressions.
head Displayfirstfewlinesofafile
ln Createsoftlinkonoldname
ls Displayinformationaboutfiletype.
mkdir Createanewdirectorydirname
more Displaydatainpaginatedform.
mv Move(Rename)aoldnameto
newname.
pwd Printcurrentworkingdirectory.
rm Remove(Delete)filename
Command Description
awk Patternscanningandprocessinglanguage
cmp Comparethecontentsoftwofiles
comm. Comparesorteddata
cut Cutoutselectedfieldsofeachlineofafile
diff Differentialfilecomparator
expand Expandtabstospaces
join Joinfilesonsomecommonfield
Perl Datamanipulationlanguage
sed Streamtexteditor
sort Sortfiledata
split Splitfileintosmallerfiles
tr Translatecharacters
uniq Reportrepeatedlinesinafile
wc Countwords,lines,andcharacters
vi Opensvitexteditor
vim Opensvimtexteditor
fmt Simpletextformatter
spell Checktextforspellingerror
rmdir Deleteanexistingdirectoryprovided
itisempty.
tail Printslastfewlinesinafile.
touch Updateaccessandmodificationtime
ofafile.
Manipulatingdata
The contents of files can be
compared and altered with the
followingcommands.
CompressedFiles
Filesmaybecompressedtosavespace. Compressed files can becreatedandexamined−
ispell Checktextforspellingerror
Emacs GNUprojectEmacs
ex,edit Lineeditor
Emacs GNUprojectEmacs
Command Description
gompress Compressfiles
gunzip Uncompressgzippedfiles
gzip GNUalternativecompression
method
uncompress Uncompressfiles
unzip List,testandextractcompressed
filesinaZIParchive
zcat Catacompressedfile
zcmp Comparecompressedfiles
zdiff Comparecompressedfiles
zmore Fileperusalfilterforcrtviewingof
compressedtext
GettingInformation
VariousUnixmanualsanddocumentationareavailableon-line.ThefollowingShellcommandsgiveinformation−
Command Description
apropos Locatecommandsbykeywordlookup
info Displayscommandinformationpages
online
Command Description
ftp Filetransferprogram
rcp Remotefilecopy
rlogin RemotelogintoaUNIXhost
rsh Remoteshell
tftp Trivialfiletransferprogram
telnet Maketerminalconnectiontoanotherhost
ssh Secureshellterminalorcommandconnection
scp Secureshellremotefilecopy
sftp secureshellfiletransferprogram
man Displaysmanualpagesonline
whatis Searchthewhatisdatabasefor
completewords.
yelp GNOMEhelpviewer
NetworkCommunication
The following commands are used to send and receive files froma localUNIX
hoststotheremotehostsaroundtheworld.
Some of these commandsmay
berestrictedonyourcomputer
forsecurityreasons.
MessagesbetweenUsers
TheUNIXsystemsupportson-
screen messaging to other
usersandworldwideelectronic
mail−
Command Description
Evolution GUImailhandlingtoolonLinux
mail Simplesendorreadmailprogram
nesg Permitordenymessages
parcel Sendfilestoanotheruser
Pine Vdu-basedmailutility
Talk Talktoanotheruser
write Writemessagetoanotheruser
ProgrammingUtilities
The following programming tools and languages are available based on which
versionofUnixyouareusing
Command Description
dbx Sundebugger
gdb GNUdebugger
make Maintainprogramgroupsand
compileprograms.
nm Printprogram’snamelist
size Printprogram’ssizes
strip Removesymboltableandrelocation
bits
cb Cprogrambeautifier
cc ANSICcompilerforSunsSPARC
systems
Ctrace Cprogramdebugger
Gcc GNUANSICCompiler
Indent IndentandformatCprogramsource
Bc Interactivearithmeticlanguage
processor
Gcl GNUCommonLisp
Perl Generalpurposelanguage
Php Webpageembeddedlanguage
Command Description
chfn Changeyourfingerinformation
chgrp Changethegroupownershipofafile
chown Changeowner
date Printthedate
determin Automaticallyfindterminaltype
du Printamountofdiskusage
echo Echoargumentstothestandard
options
exit Quitthesystem
Py Pythonlanguageinterpreter
Asp Webpageembeddedlanguage
CC C++compilerforSunsSPARC
systems
g++ GNUC++Compiler
Javac JAVAcompiler
appletvieweir JAVAappletviewer
Netbeans Javaintegrateddevelopment
environmentonLinux
Sqlplus RuntheOracleSQLinterpreter
Sqlldr RuntheOracleSQLdataloader
MySql RunthemysqlSQLinterpreter
MiscCommands
Thesecommandslistoraltersysteminformation−
td>groups
finger Printinformationaboutlogged-in
users
groupadd Createausergroup
Showgroup
memberships
homequota Showquotaandfileusage
iostat ReportI/Ostatistics
kill Sendasignaltoaprocess
last Showlastloginsofusers
logout logoffUNIX
lun ListusernamesorloginID
netstat Shownetworkstatus
passwd Changeuserpassword
passwd Changeyourloginpassword
printenv Displayvalueofashellvariable
ps Displaythestatusofcurrentprocesses
ps Printprocessstatusstatistics
quota–v Displaydiskusageandlimits
reset Resetterminalmode
script Keepscriptofterminalsession
script Savetheoutputofacommandor
process
setenv Setenvironmentvariables
sty Setterminaloptions
time Timeacommand
top Displayallsystemprocesses
tset Setterminalmode
tty Printcurrentterminalname
umask Showthepermissionsthataregivento
viewfilesbydefault
uname Displaynameofthecurrentsystem
uptime Getthesystemuptime
useradd Createauseraccount
users Printnamesofloggedinusers
vmstat Reportvirtualmemorystatistics
w Showwhatloggedinusersaredoing
who Listloggedinusers
ChapterSeventeen:REGULAREXPRESSION
A regular expression is a string that can be used to describe severalsequencesofcharacters.RegularexpressionsareusedbyseveraldifferentUnixcommands, includinged,sed,awk,grep,and, to amore limited extent,vi.Heresedstandsforstreameditorastreamorientededitor,whichwascreatedexclusively for executing scripts. Thus, all the input you feed into it passesthroughandgoestoSTDOUTanditdoesnotchangetheinputfile.
Invokingsed
Beforewestart, letustakemakesureyouhavealocalcopyof/etc/passwdtextfiletoworkwithsed.Asmentionedpreviously,sedcanbe invokedbysendingdatathroughapipetoitasfollows−$cat/etc/passwd|sed
Usage:sed[OPTION]…{script-other-script}[input-file]…
-n,—quiet,—silent
suppressautomaticprintingofpatternspace
-escript,—expression=script
………………………….
Thecatcommanddumpsthecontentsof/etc/passwdtosed throughthepipeintosed’spatternspace.Thepatternspace is the internalworkbuffer thatsedusestodoitswork.
ThesedGeneralSyntax
Generalsyntaxforsed
/pattern/action
Here,patternisaregularexpression,andactionisoneofthecommands
giveninthefollowingtable.Ifpatternisomitted,actionisperformedforevery
lineaswehaveseenabove.
Theslashcharacters(/)thatsurroundthepatternarerequiredbecausetheyare
usedasdelimiters.
Range Description
P Printstheline
D Deletestheline
s/pattern1/pattern2/ Substitutesthefirstoccurrenceof
pattern1withpattern2.
DeletingAllLineswithsed
Invokesedagain,butthistimetellsedtousetheeditingcommanddeleteline,denotedbythesingleletterd−$cat/etc/passwd|sed‘d’
$
Insteadof invokingsedbysendinga file to it throughapipe,youcan instructsed to read the data from a file, as in the following example. The followingcommanddoesexactly thesamethingas thepreviousexample,without thecatcommand−$sed-e‘d’/etc/passwd
$
ThesedAddresses
sedalsounderstandssomethingcalledaddresses.Addressesareeitherparticularlocations in a file or a range where a particular editing command should beapplied.Whensedencountersnoaddresses,itperformsitsoperationsoneverylineinthefile.
Thefollowingcommandaddsabasicaddresstothesedcommandyouhavebeenusing–
$cat/etc/passwd|sed‘1d’|more
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
$
Noticethatthenumber1isaddedbeforethedeleteeditcommand.Thistellssedtoperformtheeditingcommandonthefirstlineofthefile.Inthisexample,sedwilldeletethefirstlineof/etc/passwordandprinttherestofthefile.
ThesedAddressRanges
So,whatifyouwanttoremovemorethanonelinefromafile?Youcanspecifyan
addressrangewithsedasfollows−$cat/etc/passwd|sed‘1,5d’|more
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
$
The above commandwill be applied on all the lines starting from 1 through 5.Therefore,itdeletesthefirstfivelines.
Tryoutthefollowingaddressranges–
Range Description
‘4,10d’ Linesstartingfrom4thtill10tharedeleted
‘10,4d’ Only10thlineisdeleted,becauseseddoesnotworkinreversedirection.
‘4,+5d’ Thiswillmatchline4inthefile,deletethatline,continuetodeletethenextfivelines,andthenceaseitsdeletionandprinttherest
‘2,5!d’ Thiswilldeletedeverythingexceptstartingfrom2ndtill5thline.
‘1~3d’ Thisdeletesthefirstline,stepsoverthenextthreelines,andthendeletesthefourthline.Sedcontinuesapplyingthispatternuntiltheendofthefile.
‘2~2d’ Thistellssedtodeletethesecondline,stepoverthenextline,deletethenextline,andrepeatuntiltheendofthefileisreached.
‘4,10p’ Linesstartingfrom4thtill10thareprinted
‘4,d’ Thiswouldgeneratesyntaxerror.
‘,10d’ Thiswouldalsogeneratesyntaxerror.
Note:Whileusingpaction,youshoulduse-noptiontoavoidrepetitionoflineprinting.Checkthedifferencebetweenthesetwocommands−$cat/etc/passwd|sed-n‘1,3p’
Checktheabovecommandwithout-nasfollows−
Flag Description
g Replaceallmatches,notjustthe
firstmatch.
NUMBER ReplaceonlyNUMBERthmatch.
p Ifsubstitutionwasmade,print
patternspace.
w
FILENAME
Ifsubstitutionwasmade,write
resulttoFILENAME.
$cat/etc/passwd|sed‘1,3p’
TheSubstitutionCommand
Thesubstitutioncommand,denotedbys,willsubstituteanystringthatyouspecify with any other string that you specify. To substitute one string withanother,youneedtohaveawayoftellingsedwhereyourfirststringendsandthesubstitutionstringbegins.This is traditionallydonebybookending the twostringswiththeforwardslash(/)character.Thefollowingcommandsubstitutesthefirstoccurrenceonalineofthestringrootwiththestringamrood.$cat/etc/passwd|sed‘s/root/amrood/’
amrood:x:0:0:rootuser:/root:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
……………………..
It is very important tonote thatsed substitutes only the first occurrence on aline.Ifthestringrootoccursmorethanonceonaline,onlythefirstmatchwillbereplaced.Totellsedtodoaglobalsubstitution,addthelettergtotheendofthecommandasfollows−$cat/etc/passwd|sed‘s/root/amrood/g’
amrood:x:0:0:amrooduser:/amrood:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
………………………
SubstitutionFlags
There are a number ofother useful flags that canbe passed in addition tothe g flag, and you canspecifymorethanoneatatime.
UsinganAlternative
StringSeparator
You may find yourselfhavingtodoasubstitutionon a string that includesthe forward slashcharacter.Inthiscase,youcan specify a differentseparatorbyproviding thedesignated character after
Iori Matchinacase-insensitive
manner.
Morm Inadditiontothenormalbehavior
ofthespecialregularexpression
characters^and$,thisflagcauses
^tomatchtheemptystringaftera
newlineand$tomatchtheempty
stringbeforeanewline.
thes.$cat/etc/passwd|sed
‘s:/root:/amrood:g’
amrood:x:0:0:amrood
user:/amrood:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
In theaboveexampleweused:asdelimiter insteadof slash/becausewewere
tryingtosearch/rootinsteadofsimpleroot.
ReplacingwithEmptySpace
Useanemptysubstitutionstringtodelete therootstring fromthe/etc/passwd
fileentirely−
$cat/etc/passwd|sed‘s/root//g’
:x:0:0::/:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
AddressSubstitution
Ifyouwanttosubstitutethestring‘sh’withthestring‘quiet’online10,youcan
specifyitasfollows−
$cat/etc/passwd|sed’10s/sh/quiet/g’
root:x:0:0:rootuser:/root:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/quiet
Similarly,todoanaddressrangesubstitution,youcoulddosomethinglikethis−$cat/etc/passwd|sed‘1,5s/sh/quiet/g’
root:x:0:0:rootuser:/root:/bin/quiet
daemon:x:1:1:daemon:/usr/sbin:/bin/quiet
bin:x:2:2:bin:/bin:/bin/quiet
sys:x:3:3:sys:/dev:/bin/quiet
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
Asyoucanseefromtheoutput,thefirstfivelineshadthestring‘sh’changedto‘quiet,’buttherestofthelineswereleftuntouched.
TheMatchingCommand
Youwouldusepoptionalongwith-noptiontoprintall thematching linesasfollows−$cattesting|sed-n‘/root/p’
root:x:0:0:rootuser:/root:/bin/sh
[root@ip-72-167-112-17amrood]#vitesting
root:x:0:0:rootuser:/root:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
UsingRegularExpression
Whilematchingpatterns,youcanuseregularexpressions,whichprovidegreaterflexibility. Check the following example, which matches all the lines startingwithdaemonandthendeletesthem−
$cattesting|sed‘/^daemon/d’
root:x:0:0:rootuser:/root:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
Expression Description
/a.c/ Matcheslinesthatcontainstringssuchas
a+c,a-c,abc,match,anda3c,whereasthe
pattern
/a*c/ Matchesthesamestringsalongwith
stringssuchasace,yacc,andarctic.
/[tT]he/ MatchesthestringTheandthe:
/^$/ MatchesBlanklines
/^.*$/ Matchesanentirelinewhateveritis.
/*/ Matchesoneormorespaces
/^$/ MatchesBlanklines
backup:x:34:34:backup:/var/backups:/bin/sh
Examplethatdeletesalllinesendingwithsh−$cattesting|sed‘/sh$/d’
sync:x:4:65534:sync:/bin:/bin/sync
The following table lists four special characters that are very useful in regularexpressions.
Character Description
^ Matchesthebeginningoflines.
$ Matchestheendoflines.
. Matchesanysinglecharacter.
* Matcheszeroormoreoccurrencesofthe
previouscharacter
[chars] Matchesanyoneofthecharactersgivenin
chars,wherecharsisasequenceofcharacters.
Youcanusethe-charactertoindicatearange
ofcharacters.
MatchingCharacters
Expressionstodemonstratethe
useofmetacharacters.
The following table showssome frequently used sets ofcharacters−
CharacterClassKeywords
Some special keywords arecommonlyavailabletoregexps,especially GNU utilities thatemployregexps.Theseareveryuseful for sed regularexpressions as they simplifythingsandenhancereadability.
For example, the characters a
Set Description
[a-z] Matchesasinglelowercaseletter
[A-Z] Matchesasingleuppercaseletter
[a-zA-Z] Matchesasingleletter
[0-9] Matchesasinglenumber
[a-zA-Z0-9]
Matchesasingleletterornumber
Character
Class
Description
[[:alnum:]] Alphanumeric[a-zA-Z0-9]
[[:alpha:]] Alphabetic[a-zA-Z]
[[:blank:]] Blankcharacters(spacesortabs)
[[:cntrl:]] Controlcharacters
[[:digit:]] Numbers[0-9]
[[:graph:]] Anyvisiblecharacters(excludes
whitespace)
[[:lower:]] Lowercaseletters[a-z]
[[:print:]] Printablecharacters(noncontrol
characters)
[[:punct:]] Punctuationcharacters
[[:space:]] Whitespace
[[:upper:]] Uppercaseletters[A-Z]
throughzaswellasthecharacters
A through Z constitute one such
class of characters that has the
keyword [[:alpha:]] Using the
alphabetcharacterclasskeyword,
this command prints only those
lines in the /etc/syslog.conf file
that start with a letter of the
alphabet−
$cat/etc/syslog.conf|sed-n‘/^[[:alpha:]]/p’
authpriv.*/var/log/secure
mail.*-/var/log/maillog
cron.*/var/log/cron
uucp,news.crit/var/log/spooler
local7.*/var/log/boot.log
C omplete list of availablecharacter class keywords inGNUsed.
AmpersandReferencing
The sed metacharacter &represents the contents of thepattern that wasmatched. Forinstance,youhavea file calledphone.txt full of phonenumbers−5555551212
5555551213
5555551214
6665551215
6665551216
7775551217
You want to make the area
code (the first three digits)
surroundedbyparentheses for
easier reading.Todo this, you
can use the ampersand
replacement character, like so
[[:xdigit:]] Hexdigits[0-9a-fA-F] −
$sed-e‘s/^[[:digit:]][[:digit:]][[:digit:]]/(&)/g’phone.txt
(555)5551212
(555)5551213
(555)5551214
(666)5551215
(666)5551216
(777)5551217
Hereyouarematchingthefirst3digitsandthenusing&replacingthose3digits
withsurroundingparentheses.
UsingMultiplesedCommands
Youcanusemultiplesedcommandsinasinglesedcommandasfollows−$sed-e‘command1’-e‘command2’…-e‘commandN’files
Here command1 through commandNaresed commands of the typediscussedpreviously.Thesecommandsareappliedtoeachlineinthelistoffilesgivenbyfiles.Usingthesamemechanism,wecanwriteabovephonenumberexampleasfollows–
$sed-e‘s/^[[:digit:]]\{3\}/(&)/g’\
-e‘s/)[[:digit:]]\{3\}/&-/g’phone.txt
(555)555-1212
(555)555-1213
(555)555-1214
(666)555-1215
(666)555-1216
(777)555-1217
Note− In theaboveexample, insteadof repeating thecharacterclasskeyword[[:digit:]] three times, you replaced it with \{3\}, which means to match thepreceding regular expression three times.Here I used \ to give line break youshouldremovethisbeforerunningthiscommand.
BackReferences
The ampersandmetacharacter is useful, but evenmore useful is the ability todefinespecificregionsinaregularexpressionsoyoucanreferencetheminyourreplacement strings.Bydefining specific parts of a regular expression, you canthen refer back to those parts with a special reference character. To do backreferences,youhavetofirstdefinearegionandthenreferbacktothatregion.Todefinearegionyouinsertbackslashparenthesesaroundeachregionofinterest.Thefirstregionthatyousurroundwithbackslashesisthenreferencedby\1,thesecondregionby\2,andsoon.
Assumingphone.txthasthefollowingtext−(555)555-1212
(555)555-1213
(555)555-1214
(666)555-1215
(666)555-1216
(777)555-1217
Nowtrythefollowingcommand−$catphone.txt|sed‘s/\(.*)\)\(.*-\)\(.*$\)/Area\
code:\1Second:\2Third:\3/’
Areacode:(555)Second:555-Third:1212
Areacode:(555)Second:555-Third:1213
Areacode:(555)Second:555-Third:1214
Areacode:(666)Second:555-Third:1215
Areacode:(666)Second:555-Third:1216
Areacode:(777)Second:555-Third:1217
Note:Intheaboveexampleeachregularexpressioninsidetheparenthesiswouldbebackreferencedby\1,\2andsoon.HereIused\togivelinebreakyoushouldremovethisbeforerunningthiscommand.
ChapterEighteen:FILESYSTEMBASICS
Afilesystemisalogicalcollectionoffilesonapartitionordisk.Apartitionisacontainerforinformationandcanspananentireharddriveifdesired.Yourharddrivecanhavevariouspartitionswhichusuallycontainonlyonefilesystem,suchasonefilesystemhousingthe/filesystemoranothercontainingthe/homefilesystem.Onefilesystemperpartitionallowsforthelogicalmaintenanceandmanagement of differing file systems.Everything inUnix is considered to be afile, includingphysicaldevicessuchasDVD-ROMs,USBdevices, floppydrives,andsoforth.
DirectoryStructure
Unix uses a hierarchical file system structure,much like an upside-down tree,withroot(/)atthebaseofthefilesystemandallotherdirectoriesspreadingfromthere. A UNIX file system is a collection of files and directories that have thefollowingproperties−
Ithasarootdirectory(/)thatcontainsotherfilesanddirectories.
Eachfileordirectoryisuniquelyidentifiedbyitsname,thedirectoryinwhichitresides,andauniqueidentifier,typicallycalledaninode.
Byconvention,therootdirectoryhasaninodenumberof2andthelost+founddirectoryhasaninodenumberof3.Inodenumbers0and1arenotused.Fileinodenumberscanbeseenbyspecifyingthe-ioptiontolscommand.
Itisself-contained.Therearenodependenciesbetweenonefilesystemandanyother.
The directories have specific purposes and generally hold the same types ofinformationforeasilylocatingfiles.ThefollowingarethedirectoriesthatexistonthemajorversionsofUnix−
Directory Description
/ Thisistherootdirectory,whichshould
containonlythedirectoriesneededatthe
toplevelofthefilestructure.
/bin Thisiswheretheexecutablefilesare
located.Theyareavailabletoallusers.
/dev Thesearedevicedrivers.
/etc Supervisordirectorycommands,
configurationfiles,diskconfiguration
files,validuserlists,groups,ethernet,
hosts,wheretosendcriticalmessages.
/lib Containssharedlibraryfilesand
sometimesotherkernel-relatedfiles.
/boot Containsfilesforbootingthesystem.
/home Containsthehomedirectoryforusersand
otheraccounts.
/mnt Usedtomountothertemporaryfile
systems,suchascdromandfloppyforthe
CD-ROMdriveandfloppydiskettedrive,
respectively.
/proc Containsallprocessesmarkedasafileby
processnumberorotherinformationthat
isdynamictothesystem.
/tmp Holdstemporaryfilesusedbetween
systemboots.
/usr Usedformiscellaneouspurposes,canbe
usedbymanyusers.Includes
administrativecommands,sharedfiles,
libraryfiles,andothers.
/var Typicallycontainsvariable-lengthfiles
suchaslogandprintfilesandotherfile
typesthatmaycontainavariableamount
ofdata.
/sbin Containsbinary(executable)files,usually
forsystemadministration.For
examplefdiskandifconfigutlities.
/kernel Containskernelfiles
NavigatingtheFileSystem
Nowthatyouunderstandthebasicsofthefilesystem,youcanbeginnavigating
to the files youneed.The followingare commandsyouwilluse tonavigate the
system−
Command Description
cat
filename
Displaysafilename.
cddirname Movesyoutothedirectoryidentified.
cpfile1file2 Copiesonefile/directorytospecified
location.
file
filename
Identifiesthefiletype(binary,text,
etc).
find
filename
dir
Findsafile/directory.
head
filename
Showsthebeginningofafile.
less
filename
Browsesthroughafilefromendor
beginning.
lsdirname Showsthecontentsofthedirectory
specified.
mkdir
dirname
Createsthespecifieddirectory.
more
filename
Browsesthroughafilefrombeginning
toend.
mvfile1
file2
Movesthelocationoforrenamesa
file/directory.
pwd Showsthecurrentdirectorytheuseris
in.
rm
filename
Removesafile.
rmdir
dirname
Removesadirectory.
tail
filename
Showstheendofafile.
touch
filename
Createsablankfileormodifiesan
existingfile.sattributes.
whereis
filename
Showsthelocationofafile.
which
filename
Showsthelocationofafileifitisin
yourPATH.
You can use Manpage Help to check complete syntax for each of these
commands.
ThedfCommand
Thefirstwaytomanageyourpartitionspaceiswiththedf(diskfree)command.Thecommanddf-k (disk free) displays the disk space usage in kilobytes, asshownbelow–
$df-k
Filesystem1K-blocksUsedAvailableUse%Mountedon
/dev/vzfs104857607836644264911675%/
/devices0000%/devices
$
Someofthedirectories,suchas/devices,shows0inthekbytes,used,andavail
columnsaswellas0%forcapacity.Thesearespecial(orvirtual)filesystems,and
althoughtheyresideonthediskunder/,bythemselvestheydonottakeupdisk
space.
The df -k output is generally the same on all Unix systems. Here’s what itusuallyincludes−
Column Description
Filesystem Thephysicalfilesystemname.
Kbytes Totalkilobytesofspaceavailable
onthestoragemedium.
Used Totalkilobytesofspaceused(by
files).
Avail Totalkilobytesavailableforuse.
Capacity Percentageoftotalspaceusedby
files.
Mountedon Whatthefilesystemismounted
on.
Youcanuse the-h (human readable) option to display the output in a formatthatshowsthesizeineasier-to-understandnotation.
TheduCommand
Thedu (disk usage) command enables you to specify directories to show diskspace usage on a particular directory. This command is helpful if you want todetermine how much space a particular directory is using. The followingcommandwilldisplaythenumberofblocksusedbyeachdirectory.Asingleblockmaytakeeither512Bytesor1KiloBytedependingonyoursystem.$du/etc
10/etc/cron.d
126/etc/default
6/etc/dfs
…
$
The-hoptionmakestheoutputeasiertocomprehend−$du-h/etc
5k/etc/cron.d
63k/etc/default
3k/etc/dfs
…
$
MountingtheFileSystem
Afilesystemmustbemountedinordertobeusablebythesystem.Toseewhatis
currentlymounted(availableforuse)onyoursystem,usethiscommand−
$mount
/dev/vzfson/typereiserfs(rw,usrquota,grpquota)
procon/proctypeproc(rw,nodiratime)
devptson/dev/ptstypedevpts(rw)
$
The /mnt directory, by Unix convention, is where temporarymounts (such as
CD-ROMdrives, remote network drives, and floppy drives) are located. If you
needtomountafilesystem,youcanusethemountcommandwiththefollowing
syntax−
mount-tfile_system_typedevice_to_mountdirectory_to_mount_to
Forexample,ifyouwanttomountaCD-ROMtothedirectory/mnt/cdrom,for
example,youcantype−
$mount-tiso9660/dev/cdrom/mnt/cdrom
ThisassumesthatyourCD-ROMdeviceiscalled/dev/cdromandthatyouwantto mount it to /mnt/cdrom. Refer to the mount man page for more specificinformationortypemount-hatthecommandlineforhelpinformation.Aftermounting, you can use the cd command to navigate the newly available filesystemthroughthemountpointyoujustcreated.
UnmountingtheFileSystem
To unmount (remove) the file system from your system, usetheumountcommandbyidentifyingthemountpointordevice.Forexample,tounmountcdrom,usethefollowingcommand−$umount/dev/cdrom
The mount command enables you to access your file systems, but on mostmodernUnixsystems,theautomountfunctionmakesthisprocessinvisibletotheuserandrequiresnointervention.
UserandGroupQuotas
User and groupquotasprovide themechanismsbywhich the amount of spaceusedbyasingleuseroralluserswithinaspecificgroupcanbelimitedtoavaluedefined by the administrator. Quotas operate around two limits that allow theusertotakesomeactioniftheamountofspace,ornumberofdiskblocksstarttoreachtheadministratordefinedlimits−
SoftLimit−Iftheuserexceedsthelimitdefined,thereisagraceperiodthatallowstheusertofreeupsomespace.
HardLimit−Whenthehardlimitisreached,regardlessofthegraceperiod,nofurtherfilesorblockscanbeallocated.
Thereareanumberofcommandstoadministerquotas−
Command Description
quota Displaysdiskusageandlimitsforauserofgroup.
edquota Thisisaquotaeditor.UsersorGroupsquotacanbeeditedusingthiscommand.
quotacheck Scanafilesystemfordiskusage,create,checkandrepairquotafiles
setquota Thisisalsoacommandlinequotaeditor.
quotaon Thisannouncestothesystemthatdiskquotasshouldbeenabledononeormorefilesystems.
quotaoff Thisannouncestothesystemthatdiskquotasshouldbedisabledoffoneormorefilesystems.
repquota Thisprintsasummaryofthediscusageandquotasforthespecifiedfilesystems
ChapterNineteen:UNIX-USERADMINISTRATION
TherearethreetypesofaccountsonUnixsystem−
Rootaccount−alsocalledsuperuserandhascompleteand
unfetteredcontrolofthesystem.Asuperusercanrunanycommands
withoutanyrestriction.Thisusershouldbeassumedasasystem
administrator.
Systemaccounts−Systemaccountsarethoseneededforthe
operationofsystem-specificcomponentsforexamplemailaccounts
andthesshdaccounts.Theseaccountsareusuallyneededforsome
specificfunctiononyoursystem,andanymodificationstothemcould
adverselyaffectthesystem.
Useraccounts−Useraccountsprovideinteractiveaccesstothe
systemforusersandgroupsofusers.Generalusersaretypically
assignedtotheseaccountsandusuallyhavelimitedaccesstocritical
systemfilesanddirectories.
UNIXsupportsaconceptofGroupAccount,whichlogicallygroupsanumberof
accounts.Everyaccountwouldbeapartofanygroupaccount.Unixgroupsplays
importantroleinhandlingfilepermissionsandprocessmanagement.
ManagingUsersandGroups
Therearethreemainuseradministrationfiles−
/etc/passwd:−Keepsuseraccountandpasswordinformation.This
fileholdsthemajorityofinformationaboutaccountsontheUnix
system.
/etc/shadow:−Holdstheencryptedpasswordofthecorresponding
account.Notallsystemssupportthisfile.
/etc/group:−Containsgroupinformationforeachaccount.
/etc/gshadow:−Containssecuregroupaccountinformation.
Checkalltheabovefilesusingcatcommand.
Commands available on the majority of Unix systems to create and manage
accountsandgroups–
Command Description
useradd Addsaccountstothesystem.
usermod Modifiesaccountattributes.
userdel Deletesaccountsfromthesystem.
groupadd Addsgroupstothesystem.
groupmod Modifiesgroupattributes.
groupdel Removesgroupsfromthesystem.
CreateaGroup
Youneedtocreategroupsbeforecreatinganyaccountotherwiseyouwouldhaveto use existing groups on your system. You have all the groups listedin /etc/groups file. All the default groups would be system account specificgroups and it is not recommended to use them for ordinary accounts.Use thefollowingsyntaxtocreateanewgroupaccount−groupadd[-ggid[-o]][-r][-f]groupname
Herearethedetailsoftheparameters:
Option Description
-gGID Thenumericalvalueofthegroup’sID.
-o Thisoptionpermitstoaddgroupwith
non-uniqueGID
-r Thisflaginstructsgroupaddtoadda
systemaccount
-f Thisoptioncausestojustexitwith
successstatusifthespecifiedgroup
alreadyexists.With-g,ifspecifiedGID
alreadyexists,other(unique)GIDis
chosen
groupname Actualgroupnametobecreated.
Ifyoudonotspecifyanyparametersthenthesystemwillusethedefaultvalues.
Thefollowingexamplewillcreateadevelopersgroupwithdefaultvalues,which
isacceptableformostadministrators.
$groupadddevelopers
ModifyaGroup
Tomodifyagroup,usethegroupmodsyntax−$groupmod-nnew_modified_group_nameold_group_name
Tochangethedevelopers_2groupnametodeveloper,type−$groupmod-ndeveloperdeveloper_2
HereishowyouwouldchangethefinancialGIDto545−$groupmod-g545developer
DeleteaGroup:
Todelete an existinggroup, all youneedare thegroupdel commandand thegroupname.Todeletethedevelopergroup,thecommandis−
$groupdeldeveloper
Thisremovesonly thegroup,notany filesassociatedwith thatgroup.The files
arestillaccessiblebytheirowners.
CreateanAccount
LetusseehowtocreateanewaccountonyourUnixsystem.Usethesyntaxto
createauser’saccount−
useradd-dhomedir-ggroupname-m-sshell-uuseridaccountname
AvailableParameters−
Option Description
-dhomedir Specifieshomedirectoryforthe
account.
-g
groupname
Specifiesagroupaccountforthis
account.
-m Createsthehomedirectoryifit
doesn’texist.
-sshell Specifiesthedefaultshellforthis
account.
-uuserid Youcanspecifyauseridforthis
account.
accountname Actualaccountnametobe
created
If youdonot specify a parameter then systemwill use the default values.Theuseraddcommandmodifiesthe/etc/passwd,/etc/shadow,and/etc/groupfilesandcreatesahomedirectory.Thefollowingisanexamplewhichwillcreatean accountmcmohd setting its home directory to /home/mcmohd and groupasdevelopers.ThisuserwouldhaveKornShellassignedtoit.$useradd-d/home/mcmohd-gdevelopers-s/bin/kshmcmohd
Before issuing the above command, make sure you already have
a developers group created using groupadd command. Once an account is
createdyoucansetitspasswordusingthepasswdcommandasfollows−
$passwdmcmohd20
Changingpasswordforusermcmohd20.
NewUNIXpassword:
RetypenewUNIXpassword:
passwd:allauthenticationtokensupdatedsuccessfully.
Whenyoutypepasswdaccountname,itgivesyouoptiontochangethepassword
provided you are superuser otherwise you would be able to change just your
passwordusingthesamecommandbutwithoutspecifyingyouraccountname.
ModifyanAccount
Theusermod command enables you to make changes to an existing account
fromthecommandline.Itusesthesameargumentsastheuseraddcommand,plus the -l argument, which allows you to change the account name. Forexample, to change the account namemcmohd tomcmohd20 and to changehomedirectoryaccordingly,youwouldneedtoissuefollowingcommand−$usermod-d/home/mcmohd20-m-lmcmohdmcmohd20
DeleteanAccount
Theuserdelcommand can be used to delete an existing user. This is a verydangerous command if not used with caution. There is only one argument oroptionavailableforthecommand:-r,forremovingtheaccount’shomedirectoryandmail file. For example, to remove accountmcmohd20, you would need toissuefollowingcommand−$userdel-rmcmohd20
Ifyouwanttokeepthehomedirectoryforbackuppurposes,omitthe-roption.Youcanremovethehomedirectoryasneededatalatertime.
ChapterTwenty:SYSTEMPERFORMANCE
Thepurposeofthistutorialistointroducetheperformanceanalysttosomeof
the free tools available tomonitor andmanageperformanceonUNIXsystems,
andtoprovideaguidelineonhowtodiagnoseandfixperformanceproblemsina
Unix environment. UNIX has following major resource types that need to be
monitoredandtuned−
CPU
Memory
Diskspace
Communicationslines
I/OTime
NetworkTime
Applicationsprograms
PerformanceComponents
Therearefivemajorcomponentswheretotalsystemtimegoes–
Component Description
UserstateCPU TheactualamountoftimetheCPUspendsrunningtheusersprogramintheuserstate.Itincludestimespentexecutinglibrarycalls,butdoesnotincludetimespentinthekernelonitsbehalf.
SystemstateCPU ThisistheamountoftimetheCPUspendsinthesystemstateonbehalfofthisprogram.AllI/Oroutinesrequirekernelservices.TheprogrammercanaffectthisvaluebytheuseofblockingforI/Otransfers.
I/OTimeandNetworkTime
ThesearetheamountoftimespentmovingdataandservicingI/Orequests
Command Description
nice/renice Runaprogramwithmodifiedscheduling
priority
netstat Printnetworkconnections,routing
tables,interfacestatistics,masquerade
connections,andmulticastmemberships
time Timeasimplecommandorgiveresource
usage
uptime SystemLoadAverage
ps Reportasnapshotofthecurrent
processes.
vmstat Reportvirtualmemorystatistics
gprof Displaycallgraphprofiledata
prof ProcessProfiling
top Displaysystemtasks
Term Description
VirtualMemoryPerformance
Thisincludescontextswitchingandswapping.
ApplicationProgram Timespentrunningotherprograms-whenthesystemisnotservicingthisapplicationbecauseanotherapplicationcurrentlyhastheCPU.
PerformanceTools
Unix provides the followingimportanttoolstomeasureandfine-tune Unix systemperformance−
Unix-SystemLogging
UNIXsystemshaveavery
flexible and powerful logging
system, which enables you to
record almost anything and
then manipulate the logs to
retrieve the information you
require. Many versions of
UNIX provide a general-
purpose logging facility
called syslog. Individual
programs that need to have
information logged send the
informationtosyslog.
Unix syslog is a host-configurable, uniform system logging facility. The
system uses a centralized system logging process that runs the
program/etc/syslogdor/etc/syslog.
Theoperationofthesystemloggerisquitestraightforward.Programssend
theirlogentriestosyslogd,whichconsultstheconfigurationfile/etc/syslogd.conf
or/etc/syslogand,whenamatchisfound,writesthelogmessagetothedesired
logfile.
There are four basic syslog
Facility Theidentifierusedtodescribetheapplicationor
processthatsubmittedthelogmessage.Examples
aremail,kernel,andftp.
Priority Anindicatoroftheimportanceofthemessage.
Levelsaredefinedwithinsyslogasguidelines,from
debugginginformationtocriticalevents.
Selector Acombinationofoneormorefacilitiesandlevels.
Whenanincomingeventmatchesaselector,an
actionisperformed.
Action Whathappenstoanincomingmessagethatmatches
aselector.Actionscanwritethemessagetoalog
file,echothemessagetoaconsoleorotherdevice,
writethemessagetoaloggedinuser,orsendthe
messagealongtoanothersyslogserver.
Facility Description
auth Activityrelatedtorequestingnameand
password(getty,su,login).
authpriv Sameasauthbutloggedtoafilethatcan
onlybereadbyselectedusers.
console Usedtocapturemessagesthatwould
generallybedirectedtothesystem
console.
cron Messagesfromthecronsystemscheduler.
daemon Systemdaemoncatchall.
ftp Messagesrelatingtotheftpdaemon.
kern Kernelmessages.
local0.local7 Localfacilitiesdefinedpersite.
lpr Messagesfromthelineprintingsystem.
mail Messagesrelatingtothemailsystem.
mark Pseudo-eventusedtogeneratetimestamps
terms that you should
understand−
SyslogFacilities
Here are the available
facilities for the selector.
Notallfacilitiesarepresent
onallversionsofUNIX.
SyslogPriorities
The syslog priorities aresummarized in thefollowingtable−
The combination of facilities
and levels enables you to be
discerning about what is
logged and where that
information goes. As each
program sends its messages
dutifullytothesystemlogger,
theloggermakesdecisionson
what to keep track of and
what to discardbased on the
levels defined in the selector.
Whenyouspecifya level, the
system will keep track of
everything at that level and
higher.
The/etc/syslog.conffile
The /etc/syslog.conf file
controls where messages are
logged. A typical syslog.conf
filemightlooklikethis−
inlogfiles.
news Messagesrelatingtonetworknews
protocol(nntp).
ntp Messagesrelatingtonetworktime
protocol.
user Regularuserprocesses.
uucp UUCPsubsystem.
Priority Description
emerg Emergencycondition,suchasanimminent
systemcrash,usuallybroadcasttoallusers.
alert Conditionthatshouldbecorrected
immediately,suchasacorruptedsystem
database.
crit Criticalcondition,suchasahardwareerror.
err Ordinaryerror.
warning Warning.
notice Conditionthatisnotanerror,butpossibly
shouldbehandledinaspecialway.
info Informationalmessage.
debug Messagesthatareusedwhendebugging
programs.
none Pseudolevelusedtospecifynottolog
messages.
*.err;kern.debug;auth.notice
/dev/console
daemon,auth.notice
/var/log/messages
lpr.info
/var/log/lpr.log
mail.*
/var/log/mail.log
ftp.*
/var/log/ftp.log
auth.*
@prep.ai.mit.edu
auth.*
root,amrood
netinfo.err
/var/log/netinfo.log
install.*
/var/log/install.log
*.emerg
*
*.alert
|program_name
mark.*
/dev/console
Eachlineofthefilecontainstwoparts−
Amessageselectorthatspecifieswhichkindofmessagestolog.Forexample,allerrormessagesoralldebuggingmessagesfromthekernel.
Anactionfieldthatsayswhatshouldbedonewiththemessage.Forexample,putitinafileorsendthemessagetoauser’sterminal.
Followingarethenotablepointsfortheaboveconfiguration−
Messageselectorshavetwoparts:afacilityandapriority.Forexample,kern.debugselectsalldebugmessages(thepriority)generatedbythekernel(thefacility).
Messageselectorkern.debugselectsallprioritiesthataregreaterthandebug.
Anasteriskinplaceofeitherthefacilityorthepriorityindicates“all.”Forexample,*.debugmeansalldebugmessages,whilekern.*meansallmessagesgeneratedbythekernel.
Youcanalsousecommastospecifymultiplefacilities.Twoormoreselectorscanbegroupedtogetherbyusingasemicolon.
LoggingActions
Theactionfieldspecifiesoneoffiveactions−
Logmessagestoafileoradevice.Forexample,/var/log/lpr.logor/dev/console.
Sendamessagetoauser.Youcanspecifymultipleusernamesbyseparatingthemwithcommas(e.g.,root,amrood).
Sendamessagetoallusers.Inthiscase,theactionfieldconsistsofanasterisk(e.g.,*).
Pipethemessagetoaprogram.Inthiscase,theprogramisspecifiedaftertheUNIXpipesymbol(|).
Sendthemessagetothesyslogonanotherhost.Inthiscase,theactionfieldconsistsofahostname,precededbyanatsign(e.g.,@tutorialspoint.com)
TheloggerCommand
UNIX provides the logger command, which is extremely useful for systemlogging.Theloggercommandsendsloggingmessagestothesyslogddaemon,and consequently provokes system logging. Thismeanswe can check from thecommandlineatanytimeusingthesyslogddaemonanditsconfiguration.Thelogger commandprovidesamethod foraddingone-lineentries to thesystemlogfilefromthecommandline.
Theformatofthecommandis−
logger[-i][-ffile][-ppriority][-ttag][message]…
Hereisthedetailoftheparameters–
Option Description
-f
filename
Usethecontentsoffilefilenameasthemessageto
log.
-i LogtheprocessIDoftheloggerprocesswitheach
Application Directory
httpd /var/log/httpd
samba /var/log/samba
cron /var/log/
mail /var/log/
mysql /var/log/
line.
-p
priority
Enterthemessagewiththespecifiedpriority
(specifiedselectorentry);themessageprioritycan
bespecifiednumerically,orasafacility.priority
pair.Thedefaultpriorityisuser.notice.
-ttag Markeachlineaddedtothelogwiththespecified
tag.
message Thestringargumentswhosecontentsare
concatenatedtogetherinthespecifiedorder,
separatedbythespace
YoucanuseManpageHelptocheckcompletesyntaxforthiscommand.
LogRotation
Logfileshavethepropensitytogrowveryfastandconsumelargeamountsofdiskspace. To enable log rotations, most distributions use tools suchas newsyslog or logrotate. These tools should be called at frequent intervalsusingthecrondaemon.Checkthemanpagesfornewsyslogorlogrotateformoredetails.
ImportantLogLocations
All the system applicationscreate their log filesin /var/log and its sub-directories. Here are fewimportant applications andtheir corresponding logdirectories−
ChapterTwenty-one:UNIX SIGNALS
ANDTRAPS
Signals are software interrupts sent to a program to indicate that an
importanteventhasoccurred.Theeventscanvary fromuserrequests to illegal
memoryaccesserrors.Somesignals,suchastheinterruptsignal,indicatethata
user has asked the program to do something that is not in the usual flow of
control.
The following are someof themore common signals youmight encounter and
wanttouseinyourprograms−
SignalName
SignalNumber
Description
SIGHUP 1 Hangupdetectedoncontrollingterminalordeathofcontrollingprocess
SIGINT 2 Issuediftheusersendsaninterruptsignal(Ctrl+C).
SIGQUIT 3 Issuediftheusersendsaquitsignal(Ctrl+D).
SIGFPE 8 Issuedifanillegalmathematicaloperationisattempted
SIGKILL 9 Ifaprocessgetsthissignalitmustquitimmediatelyandwillnotperformanycleanupoperations
SIGALRM 14 AlarmClocksignal(usedfortimers)
SIGTERM 15 Softwareterminationsignal(sentbykillbydefault).
LISTofSignals
There isaneasywayto listall thesignalssupportedbyyoursystem.Just issuethekill–lcommandanditwilldisplayallthesupportedsignals−$kill-l
1)SIGHUP2)SIGINT3)SIGQUIT4)SIGILL
5)SIGTRAP6)SIGABRT7)SIGBUS8)SIGFPE
9)SIGKILL10)SIGUSR111)SIGSEGV12)SIGUSR2
13)SIGPIPE14)SIGALRM15)SIGTERM16)SIGSTKFLT
17)SIGCHLD18)SIGCONT19)SIGSTOP20)SIGTSTP
21)SIGTTIN22)SIGTTOU23)SIGURG24)SIGXCPU
25)SIGXFSZ26)SIGVTALRM27)SIGPROF28)SIGWINCH
29)SIGIO30)SIGPWR31)SIGSYS34)SIGRTMIN
35)SIGRTMIN+136)SIGRTMIN+237)SIGRTMIN+338)SIGRTMIN+4
39)SIGRTMIN+540)SIGRTMIN+641)SIGRTMIN+742)SIGRTMIN+8
43)SIGRTMIN+944)SIGRTMIN+1045)SIGRTMIN+1146)SIGRTMIN+12
47)SIGRTMIN+1348)SIGRTMIN+1449)SIGRTMIN+1550)SIGRTMAX-14
51)SIGRTMAX-1352)SIGRTMAX-1253)SIGRTMAX-1154)SIGRTMAX-10
55)SIGRTMAX-956)SIGRTMAX-857)SIGRTMAX-758)SIGRTMAX-6
59)SIGRTMAX-560)SIGRTMAX-461)SIGRTMAX-362)SIGRTMAX-2
63)SIGRTMAX-164)SIGRTMAX
TheactuallistofsignalsvariesbetweenSolaris,HP-UX,andLinux.
DefaultActions
Everysignalhasadefaultactionassociatedwithit.Thedefaultactionforasignalistheactionthatascriptorprogramperformswhenitreceivesasignal.
Someofthepossibledefaultactionsare−
Terminatetheprocess.
Ignorethesignal.
Dumpcore.Thiscreatesafilecalledcorecontainingthememoryimageoftheprocesswhenitreceivedthesignal.
Stoptheprocess.
Continueastoppedprocess.
SendingSignals
Thereareseveralmethodsfordeliveringsignalstoaprogramorscript.OneofthemostcommonisforausertotypeCONTROL-CortheINTERRUPTkeywhileascriptisexecuting.
When you press theCtrl+C key a SIGINT is sent to the script and as per thedefineddefaultactionscriptterminates.
The other common method for delivering signals is to use the kill commandwhosesyntaxisasfollows−$kill-signalpid
HeresignaliseitherthenumberornameofthesignaltodeliverandpidistheprocessIDthatthesignalshouldbesentto.ForExample−$kill-11001
SendstheHUPorhang-upsignaltotheprogramthatisrunningwithprocessID
1001.Tosendakillsignaltothesameprocessusethefollowingcommand−
$kill-91001
ThiswouldkilltheprocessrunningwithprocessID1001.
TrappingSignals
WhenyoupresstheCtrl+CorBreakkeyatyourterminalduringexecutionofa
shell program, normally that program is immediately terminated, and your
commandpromptreturned.Thismaynotalwaysbedesirable.Forinstance,you
mayendupleavingabunchoftemporaryfilesthatwillnotgetcleanedup.
Trappingthesesignalsisquiteeasy,thetrapcommandhasthefollowingsyntax−
$trapcommandssignals
HerecommandcanbeanyvalidUnixcommand,orevenauser-definedfunction,
andsignalcanbealistofanynumberofsignalsyouwanttotrap.
Therearethreecommonusesfortrapinshellscripts−
Cleanuptemporaryfiles
Ignoresignals
CleaningUpTemporaryFiles
Asanexampleof the trapcommand, the following showshowyoucan remove
filesandthenexitifsomeonetriestoaborttheprogramfromtheterminal−
$trap“rm-f$WORKDIR/work1$$$WORKDIR/dataout$$;exit”2
From the point in the shell program that this trap is executed, the twofileswork1$$anddataout$$willbeautomaticallyremovedifsignalnumber2isreceived by the program. Therefore, if the user interrupts execution of theprogramafterthistrapisexecuted,youcanbeassuredthatthesetwofileswillbecleanedup.Theexitcommandthatfollowsthermisnecessarybecausewithoutitexecutionwouldcontinueintheprogramatthepointthatit leftoffwhenthesignalwasreceived.
Signalnumber1isgeneratedforhangup:Eithersomeoneintentionallyhangsup
thelineorthelinegetsaccidentallydisconnected.Youcanmodifythepreceding
traptoalsoremovethetwospecifiedfilesinthiscasebyaddingsignalnumber1
tothelistofsignals−$trap“rm$WORKDIR/work1$$$WORKDIR/dataout$$;
exit” 1 2. Now these files will be removed if the line gets hung up or if
theCtrl+Ckeygetspressed.Thecommandsspecifiedtotrapmustbeenclosedin
quotesiftheycontainmorethanonecommand.Also,notethattheshellscansthe
command lineat the time that the trapcommandgets executedandalsowhen
oneofthelistedsignalsisreceived.
Sointheprecedingexample,thevalueofWORKDIRand$$willbesubstitutedat
the time that the trap command is executed. If youwanted this substitution to
occuratthetimethateithersignal1or2wasreceivedyoucanputthecommands
insidesinglequotes−
$trap‘rm$WORKDIR/work1$$$WORKDIR/dataout$$;exit’12
IgnoringSignals
If thecommandlistedfor trap isnull, thespecifiedsignalwillbe ignoredwhenreceived.Forexample,thecommand−$trap”2
Specifies that the interruptsignalbe ignored.Youmightwant to ignorecertainsignalswhenperformingsomeoperationsthatyoudonotwantinterrupted.Youcanspecifymultiplesignalstobeignoredasfollows−$trap”12315
Notethatthefirstargumentmustbespecifiedforasignaltobeignoredandisnotequivalenttowritingthefollowing,whichhasaseparatemeaningofitsown−$trap2
Ifyouignoreasignal,allsubshellsalsoignorethatsignal.However,ifyouspecifyanactiontobetakenonreceiptofasignal,allsubshellswillstilltakethedefaultactiononreceiptofthatsignal.
ResettingTraps
Afteryouhavechangedthedefaultactiontobetakenonreceiptofasignal,youcanchangeitbackagainwithtrapifyousimplyomitthefirstargument;so.$trap12
Resetstheactiontobetakenonreceiptofsignals1or2backtothedefault.
DEDICATION
Thisbookisdedicatedto……MyWifeandSon
SPECIALTHANKSTomyWifeKatherineandmysonAdam