Sei sulla pagina 1di 7

Shell

Understandingcommandsyntax(Page109(62))
Options:
Mostcommandshaveoneormoreoptionsyoucanaddtochangethecommands
behavior.Optionstypicallyconsistofasingleletter,precededbyahyphen.However,
youcangroupsingleletteroptionstogetherorprecedeeachwithahyphen,touse
morethanoneoptionatatime.Forexample,thefollowingtwousesofoptionsforthe
lscommandarethesame:
$lslat
$lslat

Inbothcases,thelscommandisrunwiththel(longlisting),a(showhiddendot
files),andtoptions(listbytime).
Somecommandsincludeoptionsthatarerepresentedbyawholeword.Totella
commandtouseawholewordasanoption,youtypicallyprecedeitwithadouble
hyphen().Forexample,tousethehelpoptiononmanycommands,youenterhelp
onthecommandline.Withoutthedoublehyphen,thelettersh,e,l,andpwouldbe
interpretedasseparateoptions.(Therearesomecommandsthatdontfollowthe
doublehyphenconvention,usingasinglehyphenbeforeaword,butmostcommands
willusedoublehyphensforwordoptions.)
Arguments:

Manycommandsalsoacceptargumentsaftercertainoptionsareenteredorattheend
oftheentirecommandline.Anargumentisanextrapieceofinformation,suchasa
filename,directory,username,device,orotheritemthattellsthecommandwhatto
acton.Forexample,cat/etc/passwddisplaysthecontentsofthe/etc/passwdfileonyour
screen.Inthiscase,/etc/passwdistheargument.Usually,youcanhaveasmanyargu
mentsasyouwantonthecommandline,limitedonlybythetotalnumberof
charactersallowedonacommandline.

ConnectingandExpandingCommands
Metacharactersincludethepipecharacter(|),ampersand(&),semicolon(;),right
parenthesis()),leftparenthesis((),lessthansign(<)andgreaterthansign(>).

Pipingbetweencommands
Thepipe(|)metacharacterconnectstheoutputfromonecommandtotheinputof
anothercommand.Thisletsyouhaveonecommandworkonsomedataandthenhave
thenextcommanddealwiththeresults.Hereisanexampleofacommandlinethat

includespipes:
$cat/etc/passwd|sort|less

Thiscommandliststhecontentsofthe/etc/passwdfileandpipestheoutputtothesort
command.Thesortcommandtakestheusernamesthatbegineachlineofthe
/etc/passwdfile,sortsthemalphabetically,andpipestheoutputtothelesscommand(to
pagethroughtheoutput).

Sequentialcommands
Sometimesyoumaywantasequenceofcommandstorun,withonecommand
completingbeforethenextcommandbegins.Youcandothisbytypingseveral
commandsonthesamecommandlineandseparatingthemwithsemicolons(;):
$date;troffmeverylargedocument|lpr;date

Backgroundcommands
Somecommandscantakeawhiletocomplete.Sometimesyoumaynotwanttotieup
yourshellwaitingforacommandtofinish.Inthosecases,youcanhavethe
commandsruninthebackgroundbyusingtheampersand(&).
Thefollowingisanexampleofacommandbeingruninthebackground:
$troffmeverylargedocument|lpr&

Expandingcommands
Withcommandsubstitution,youcanhavetheoutputofacommandinterpretedbythe
shellinsteadofbythecommanditself.Inthisway,youcanhavethestandardoutput
ofacommandbecomeanargumentforanothercommand.Thetwoformsof
commandsubstitutionare$(command)and`command`(backticks,notsinglequotes).
Thecommandinthiscasecanincludeoptions,metacharacters,andarguments.The
followingisanexampleofusingcommandsubstitution:
$vi$(find/home|grepxyzzy)

Inthisexample,thecommandsubstitutionisdonebeforethevicommandisrun.

Expandingarithmeticexpressions
Theremaybetimeswhenyouwanttopassarithmeticresultstoacommand.Thereare
twoformsyoucanusetoexpandanarithmeticexpressionandpassittotheshell:$
[expression]or$(expression).Thefollowingisanexample:

$echo"Iam$[20121957]yearsold."Iam55yearsold.

Theshellinterpretsthearithmeticexpressionfirst(20121957)andthenpassesthat
informationtotheechocommand.

UsingMetacharactersandOperators
Usingfilematchingmetacharacters

*Matchesanynumberofcharacters.

?Matchesanyonecharacter.

[...]Matchesanyoneofthecharactersbetweenthebrackets,whichcanincludea

hyphenseparatedrangeoflettersornumbers.

Usingfileredirectionmetacharacters
Commandsreceivedatafromstandardinputandsendittostandardoutput.Using
pipes(describedearlier),youcandirectstandardoutputfromonecommandtothe
standardinputofanother.Withfiles,youcanuselessthan(<)andgreaterthan(>)
signstodirectdatatoandfromfiles.Herearethefileredirectioncharacters:
<Directsthecontentsofafiletothecommand.Inmostcases,thisisthedefault

actionexpectedbythecommandandtheuseofthecharacterisoptional;using less
bigfileisthesameasless<bigfile.

>Directsthestandardoutputofacommandtoafile.Ifthefileexists,thecontent

ofthatfileisoverwritten.

2>Directsstandarderror(errormessages)tothefile.

&>Directsbothstandardoutputandstandarderrortothefile.

>>Directstheoutputofacommandtoafile,addingtheoutputtotheendofthe

existingfile.

Usingbraceexpansioncharacters
Byusingcurlybraces({}),youcanexpandoutasetofcharactersacrossfilenames,
directorynames,orotherargumentsyougivecommands.Forexample,ifyouwantto
createasetoffilessuchasmemo1throughmemo5,youcandothatasfollows:
$touchmemo{1,2,3,4,5}
$ls

memo1memo2memo3memo4memo5

or
$touch{a..f}{1..5}
$ls
a1a3a5b2b4c1c3c5d2d4e1e3e5f2f4a2a4b1b3b5c2c4d1d3d5e2e4f1f3f5

GettingInformationAboutCommands

Usingthekoption,youcansearchthenameandsummarysectionsofallmanpages
installedonthesystem.

Linuxfilesystem

UnderstandingFilePermissionsandOwnership(Page
148(106))
Foraregularfile,adashappearsinfrontoftheninebitpermissionsindicator.Insteadofa
dash,youmightseead(foradirectory),l(forasymboliclink),b(forablockdevice),c(for
acharacterdevice),s(forasocket),orp(foranamedpipe).

Oftheninebitpermissions,thefirstthreebitsapplytotheownerspermission,the
nextthreeapplytothegroupassignedtothefile,andthelastthreeapplytoallothers.
Therstandsforread,thewstandsforwrite,andthexstandsforexecutepermissions.
Ifadashappearsinsteadoftheletter,itmeansthatpermissionisturnedoffforthat
associatedread,write,orexecutebit.
Becausefilesanddirectoriesaredifferenttypesofelements,read,write,andexecute
permissionsonfilesanddirectoriesmeandifferentthings.Table4.2explainswhat
youcandowitheachofthem.

Asnotedearlier,youcanseethepermissionforanyfileordirectorybytypingthe lsld
command.Thenamedfileordirectoryappearsasthoseshowninthisexample:
$lsldch3testrwrwr1joesales4983Jan1822:13ch3drwxrxrx2joesales1024Jan
2413:47test

Thefirstlineshowsthatthech3filehasreadandwritepermissionfortheownerand
thegroup.Allotherusershavereadpermission,whichmeanstheycanviewthefile
butcannotchangeitscontentsorremoveit.Thesecondlineshowsthetestdirectory
(indicatedbytheletterdbeforethepermissionbits).Theownerhasread,write,and
executepermissionswhilethegroupandotherusershaveonlyreadandexecute
permissions.Asaresult,theownercanadd,change,ordeletefilesinthatdirectory,
andeveryoneelsecanonlyreadthecontents,changetothatdirectory,andlistthe
contentsofthedirectory.(Ifyouhadnotusedthedoptionstols,youwouldhave
listedfilesinthetestdirectoryinsteadofpermissionsofthatdirectory.)

Changingpermissionswithchmod(numbers)
Ifyouownafile,youcanusethechmodcommandtochangethepermissiononitas
youplease.Inonemethodofdoingthis,eachpermission(read,write,andexecute)is
assignedanumberr=4,w=2,andx=1andyouuseeachsetstotalnumberto
establishthepermission.Forexample,tomakepermissionswideopenforyourselfas
owner,youwouldsetthefirstnumberto7(4+2+1),andthenyouwouldgivethe
groupandothersreadonlypermissionbysettingboththesecondandthirdnumbersto
4(4+0+0),sothatthefinalnumberis744.Anycombinationofpermissionscanresult
from0(nopermission)through7(fullpermission).

Changingpermissionswithchmod(letters)
Youcanalsoturnfilepermissionsonandoffusingplus(+)andminus()signs,
respectively,alongwithletterstoindicatewhatchangesandforwhom.Usingletters,
foreachfileyoucanchangepermissionfortheuser(u),group(g),other(o),andall
users(a).Whatyouwouldchangeincludestheread(r),write(w),andexecute(x)bits.
Forexample,startwithafilethathasallpermissionsopen(rwxrwxrwx).Runthe
followingchmodcommandsusingminussignoptions.Theresultingpermissionsare

showntotherightofeachcommand:
Thefollowingchmodcommandresultsinthispermission:rxrxrxchmodawfile
Thefollowingchmodcommandresultsinthispermission:rwxrwxrwchmodoxfile
Thefollowingchmodcommandresultsinthispermission:rwxchmodgorwxfile
Likewise,thefollowingexamplesstartwithallpermissionsclosed( ).Theplus
signisusedwithchmodtoturnpermissionson:
Thefollowingchmodcommandresultsinthispermission:rw$chmodu+rwfiles
Thefollowingchmodcommandresultsinthispermission:xxx$chmoda+xfiles
Thefollowingchmodcommandresultsinthispermission:rxrx$chmodug+rxfiles

Chapter5

Potrebbero piacerti anche