devexpress asp.net pdf viewer : Cutting pdf pages application Library tool html asp.net winforms online AppleShellScripting-20148-part691

Also,asnotedpreviously,filenamescontainingspacesarenothandledcorrectlyby
getopt
.Thisisnota
problemwiththescript.Itisafundamentallimitationofthe
getopt
toolandthewayitsoutputisparsed.
Cross-Platform Compatibility Note:  TheGNU(Linux)versionof
getopt
providesadditionalflags
thatcauseittooutputastringquotedforaparticularshelltoworkaroundthislimitation.Thatusage
isnotportable,however,andisnotcompatiblewiththeOSX
getopt
implementation.
Listing5-5
01_getopt.csh
#!/bin/csh
set OUTPUT_FILE=""
set DO_LONG=""
set argv=`getopt "hlo:" $*`
if ( $status != 0 ) then
echo "Usage: $0 [-l] [-o outputfile] [path ...]"
exit 1
endif
while ( "$1" != "--" )
echo "GOT FLAG $1"
switch($1)
case "-h":
echo "Usage: $0 [-l] [-o outputfile] [path ...]"
exit 1
case "-o":
set OUTPUT_FILE="$2"
shift
breaksw
case "-l":
set DO_LONG="-l"
breaksw
endsw
shift
ResultCodes,Chaining,andFlags
HandlingFlagsandArguments
2014-03-10   |   Copyright © 2003, 2014 Apple Inc. All Rights Reserved.
81
Cutting pdf pages - application Library tool:C# PDF Page Extract Library: copy, paste, cut PDF pages in C#.net, ASP.NET, MVC, Ajax, WinForms, WPF
Easy to Use C# Code to Extract PDF Pages, Copy Pages from One PDF File and Paste into Others
www.rasteredge.com
Cutting pdf pages - application Library tool:VB.NET PDF Page Extract Library: copy, paste, cut PDF pages in vb.net, ASP.NET, MVC, Ajax, WinForms, WPF
Detailed VB.NET Guide for Extracting Pages from Microsoft PDF Doc
www.rasteredge.com
end
shift # remove trailing --
# echo "ARGS: $*"
if ( "$OUTPUT_FILE" == "" ) then
ls $DO_LONG $*
else
ls $DO_LONG $* > $OUTPUT_FILE
endif
exit $status
Note:  The
$status
variableisexplainedfurtherinWorkingwithResultCodes (page 70).
ResultCodes,Chaining,andFlags
HandlingFlagsandArguments
2014-03-10   |   Copyright © 2003, 2014 Apple Inc. All Rights Reserved.
82
application Library tool:C# PDF copy, paste image Library: copy, paste, cut PDF images in
NET Demo Codes. Best .NET framework PDF editor SDK control for image copying, pasting and cutting from adobe PDF file in Visual C#.
www.rasteredge.com
application Library tool:VB.NET PDF copy, paste image library: copy, paste, cut PDF images
NET framework PDF editor SDK control for image copying, pasting and cutting from adobe PDF file in VB.NET. PDF image cutting is similar to image deleting.
www.rasteredge.com
Noproceduralprogramminglanguagewouldbecompletewithoutsomenotionofsubroutines,functions,or
othersuchconstructs.TheBourneshellisnoexception.
IntheBourneshell,therearetwobasicwaystoapproachsubroutines.Thefirstisthroughexecutingoutside
tools(whichmayincludeascriptexecutingitselfrecursively).ThiswasdescribedbrieflyinBasicControl
Statements (page 46).However,thereareothertechniquesforobtainingresultcodeinformationfromexternal
scripts.ThesearedescribedinWorkingwithResultCodes (page 70).Youcanalsomakeexecutionofone
commandbeconditionalupontheresultcodereturnedbyanothercommandasdescribedinChaining
Execution (page 71).
Thesecondwaytoapproachsubroutines(andonewhichgenerallyresultsinbetterperformance)isthrough
theuseofactualsubroutines.ThesearedescribedinSubroutineBasics (page 83).Youcanalsowriteshort,
simplesubroutinesinlineasdescribedinAnonymousSubroutines (page 84).
Thescopingrulesforshellsubroutinesdifferfromthescopingrulesformostotherprogramminglanguages.
ShellscriptvariablescopingisexplainedinVariableScoping (page 86).
Youmayfinditusefultoincludeoneentireshellscriptinsideanother.ThissubjectiscoveredinIncludingOne
ShellScriptInsideAnother(Sourcing) (page 89).
Finally,youmayfinditusefultoexecuteoutsidescriptsinthebackgroundandchecktheirstatusatalater
time.YoucanlearnaboutthisinBackgroundJobsandJobControl (page 197).
SubroutineBasics
SubroutinesintheBourneshelllookverymuchlikeCfunctionswithouttheargumentlist.Youcallthese
subroutinesjustlikeyourunaprogram,andsubroutinescanbeusedanywherethatyoucanuseanexecutable.
Hereisasimpleexamplethatprints"Arg1:Thisisanarg"usingashellsubroutine:
#!/bin/sh
mysub()
{
2014-03-10   |   Copyright © 2003, 2014 Apple Inc. All Rights Reserved.
83
Subroutines, Scoping, and Sourcing
application Library tool:VB.NET TIFF: How to Convert TIFF to Word Using VB.NET Sample Codes
Word document, without the trouble of cutting and pasting a single page or multiple pages into MS powerful & profession imaging controls, PDF document, tiff
www.rasteredge.com
echo "Arg 1: $1"
}
mysub "This is an arg"
Justasshellscriptargumentsarestoredinshellvariablesnamed$1,$2,andsoon,sotooarethearguments
toshellsubroutines.Infact,inmostways,shellsubroutinesbehaveexactlylikeexecutinganexternalscript.
Oneplacewheretheybehavedifferentlyisinvariablescoping.SeeVariableScoping (page 86)formore
information.
Ingeneral,asubroutinecandoanythingthatashellscriptcando.Itcanevenreturnanexitstatustothecalling
partoftheshellscript.Forexample:
#!/bin/sh
mysub()
{
return 3
}
mysub "This is an arg"
echo "Subroutine returned $?"
Note:  Becarefulnottouse
exit
inthesubroutine.Ifyoudo,theentirescriptwillexit,notjustthe
subroutine.Thisisonewayinwhichsubroutinesbehavedifferentlythanseparatescriptsbehave.
C Shell Note:  TheCshelldoesnotsupportsubroutines.Youcan,however,useadditionalexternal
scriptstosimulatethem.Forverysimplesubroutines,youcanalsoapproximatethefunctionality
withaliasesasdescribedinThealiasBuiltin (page 16).
AnonymousSubroutines
TheBourneshellallowsyoutogroupmorethanonecommandtogetherandtreatthembothasaseparate
command.Ineffect,youarecreatingananonymoussubroutineinline.
Subroutines,Scoping,andSourcing
AnonymousSubroutines
2014-03-10   |   Copyright © 2003, 2014 Apple Inc. All Rights Reserved.
84
Forexample,ifyouwanttocopyalargenumberoffilesfromoneplacetoanother,youcoulduse
cp
,butthis
maynotbesemanticallyidealforanynumberofreasons.Anotheroptionistouse
tar
tocreateanarchive
onstandardoutput,thenpipethattoasecondinstanceof
tar
thatextractsthearchive.
Thebasiccommandsneededareshowbelow.Thefirstcommandinthisexamplearchivesthelistedfilesand
printsthearchivecontentstostandardoutput.Thesecondcommandtakesanarchiveformstandardoutput
andextractsthefiles.
tar -cf - file1 file2 file3 ...
tar -xf -
Thus,tocopyfilesfromoneplacetoanother,youcouldpipethefirst
tar
commandtothesecondone.
However,there’saproblemwiththat:becausethesecond
tar
isrunninginthesamedirectory,youare
extractingthefilesontopofthemselves.Ifyou’relucky,nothinghappensatall.Intheworstcasescenario,
youcouldlosefilesthisway.
Thus,youneedruntwocommandsontherightsideofthepipe:a
cd
commandtochangedirectoriesbefore
extractingthearchiveandthe
tar
commanditself.Youcandothiswithananonymoussubroutine.
Hereisasimpleexample:
tar -cf - file1 file2 file3 | \
{ cd "/destination" ; tar -xf - ; }
Noticethesemicolonbeforetheclosecurlybrace.Thissemicolonisrequired.Alsonoticethespaceafterthe
openingcurlybrace.Thisspaceisalsorequired.Forgettingeitheroftheseresultsinasyntaxerror.
Ofcourse,aswritten,thereisstillsomeriskinvolvedinusingthiscode.Ifthedestinationdirectorydoesnot
exist,the
cd
commandfails,andthe
tar
commandexecutesinthewrongdirectory.Tosolvethisproblem,
youshouldchecktheexitstatusofthefirstcommandbeforerunningthesecondone.
Forexample:
tar -cf - file1 file2 file3 | \
{ if cd "/destination" ; then tar -xf - ; fi; }
Thisversionwillexecutethe
cd
command,thenexecutethesecond
tar
commandonlyifthe
cd
command
wassuccessful.
Subroutines,Scoping,andSourcing
AnonymousSubroutines
2014-03-10   |   Copyright © 2003, 2014 Apple Inc. All Rights Reserved.
85
C Shell Note:  TheCshelldoesnotsupportanonymoussubroutines.Youcan,however,useadditional
externalscriptstosimulatethem.Youcanalsoroughlyapproximatethisfunctionalitythroughcareful
useofchainingasdescribedinChainingExecution (page 71).Forexample:
( cd / && ls ) | more
Unfortunately,ifyouneedthesecondcommandtoexecuteevenifthefirstcommandfails,youcan
quicklyendupwithveryunreadablecode.
((ls /boguslocation || true) && (ls || true)) | more
VariableScoping
Subroutinesexecutewithinthesameshellinstanceasthemainshellscript.Asaresult,allshellvariablesare,
bydefault,sharedbetweenthesubroutinesandthemainprogrambody.Thiscreatesabitofaproblemwhen
writingrecursivecode.
Fortunately,variablesdonothavetoremainglobal.
DeclaringaLocalVariable
Todeclareavariablelocaltoagivensubroutine,usethe
local
statement.
#!/bin/sh
mysub()
{
local MYVAR
MYVAR=3
echo "SUBROUTINE: MYVAR IS $MYVAR";
}
MYVAR=4
echo "MYVAR INITIALLY $MYVAR"
mysub "This is an arg"
Subroutines,Scoping,andSourcing
VariableScoping
2014-03-10   |   Copyright © 2003, 2014 Apple Inc. All Rights Reserved.
86
echo "MYVAR STILL $MYVAR"
Thisscriptwilltellyouthattheinitialvalueis4,thevaluewaschangedto3inthesubroutine,andremains4
whenthesubroutinereturns.Wereitnotfora
local
declarationof
MYVAR
inthesubroutine,thesubsequent
changeto
MYVAR
wouldhavepropagatedbacktothemainbodyofthescript.
Muchlikethe
export
statement,the
local
statementcanbeusedatthebeginningofanassignmentstatement
aswell.Forexample,theprevioussubroutinecouldhavecontainedthefollowinglineinstead:
local MYVAR=3
Ineithercase,anysubsequentchangestothevariable
MYVAR
remainlocaltothissubroutine.
Ifthissubroutinecallsitselfrecursively,anewcopyof
MYVAR
iscreatedforeachcalltothissubroutine,resulting
inacallstackmuchlikelocalvariablesinCorotherlanguages.
Unlikemostotherlanguages,however,ifthissubroutinecallsothersubroutines,thelocalcopyof
MYVAR
is
alsousedbythoseothersubroutines(unlesstheyalsodeclarealocalcopyof
MYVAR
).Ineffect,itisasthough
theglobalvariable
MYVAR
werereplacedwithanewglobalvariablethatgetsdestroyedandreplacedwiththe
originalwhenthesubroutinereturns.
Important:  Changestothisvariableinsubroutinesthatdonothavea
local
declarationof
MYVAR
will
stillresultinmodificationstotheglobalcopyof
MYVAR
exceptwhenthosesubroutinesarecalledfromthis
one.
UsingGlobalVariablesinSubroutines
Ingeneral,youcanfreelyreadandmodifyglobalvariableswithinanysubroutine.However,therearetwo
situationsinwhichthisisnotthecase:
Changestovariablespreviouslydeclaredas
local
inthecurrentcallstack.Thisisdescribedfurtherin
DeclaringaLocalVariable (page 86).
Changesmadeinsubroutinescalledthroughinlineexecution.
Ifyoucallasubroutineusinginlineexecution,thatsubroutinegetsalocalcopyofallshellvariables.Changes
madetothosevariablesarenotpropagatedbackintothemainscriptcontextbecausethesubroutinegets
executedinaseparateshell.
Thefollowingscriptdemonstratestheseconcepts:
Subroutines,Scoping,andSourcing
VariableScoping
2014-03-10   |   Copyright © 2003, 2014 Apple Inc. All Rights Reserved.
87
#!/bin/sh
# Demonstrates scoping rules.
changevalue()
{
NAME="$1"
eval "$NAME=\"\$(expr \"\$$NAME\" \"+\" \"1\")\""
eval echo "\$$NAME"
}
localchange()
{
local X=17
printf "Local variable X: $X + 1 is: "
changevalue X
echo "which is also $X"
}
A=3
printf "$A + 1 is "
changevalue A
echo "which is also $A"
B=3
printf "$B + 1 is "
RESULT="$(changevalue B)"
echo $RESULT
echo "which is NOT $B"
localchange
echo "X in a global context is \"$X\""
Subroutines,Scoping,andSourcing
VariableScoping
2014-03-10   |   Copyright © 2003, 2014 Apple Inc. All Rights Reserved.
88
Note:  Theuseof
eval
isexplainedinUsingtheevalBuiltinforDataStructures,Arrays,and
Indirection (page 168).
Noticethatwhen
changevalue
iscalleddirectly,thechangesitmakestoglobalvariablesarepropagated
backtothemainscriptbody.Whenitiscalledusinginlineexecution,thechangesarelost.
Thiscancauseproblemsforanysubroutinethatreturnsastringandalsohassideeffects.Therearetwo
straightforwarddesignpatternsthatcanbeusedtosolvethis:
Thesubroutinecouldstoreitsoutputstringinavariableinsteadofprintingit.Thecallerwouldthenuse
thatvariableinsteadofusinginlineexecutiontocapturethesubroutine’soutputinavariable.
Ifdesired,oneargumenttothesubroutinecouldbethenameofthevariabletouse.Bydesigningitin
thisway,thecallercanspecifyavariablethatislocaltothecallingsubroutine,thusavoidingglobal
namespacepollution.
Thecallercanredirectthesubroutine’soutputtoafileandsubsequentlyuseinlineexecutionwiththe
cat
commandtocopythesubroutine’soutputintoavariable.
Bothmethodsarefunctionallyequivalent.
IncludingOneShellScriptInsideAnother(Sourcing)
Aswithanyprogramminglanguagethatincludessubroutines,itisoftenusefultobuildupalibraryofcommon
subroutinesthatyourscriptscanuse.Toavoidduplicatingthiscontent,theBourneshellscriptinglanguage
providesamechanismtoincludeoneshellscriptinsideanotherbyreference.Thisprocessiscommonlyreferred
toassourcing.
Tosourceonescriptfromanother,youusethe
.
builtin.
Forexample,createafilecontainingthesubroutine
mysub
fromVariableScoping (page 86).Callit
mysub.sh
.
Tousethissubroutineinanotherscript,youcandothefollowing:
#!/bin/sh
MYVAR=4
# The next line sources the external script.
. /path/to/mysub.sh
echo "MYVAR INITIALLY $MYVAR"
Subroutines,Scoping,andSourcing
IncludingOneShellScriptInsideAnother(Sourcing)
2014-03-10   |   Copyright © 2003, 2014 Apple Inc. All Rights Reserved.
89
mysub "This is an arg"
echo "MYVAR STILL $MYVAR"
Thisscriptdoesexactlythesamethingasthescriptintheprevioussection.Theonlydifferenceisthatthe
subroutineusedisinadifferentfile.
Inadditiontousingtheperiod(
.
)character,manyshellsprovidea
source
builtinthatdoesthesamething.
Forexample:
# This form is less compatible.
source /path/to/mysub.sh
The
source
builtinismorepopularamongformerCshellprogrammers,whiletheperiod(
.
)versionismore
popularamongBourneshellpurists.Theperiodversionisconsideredportable.
Compatibility Note:  The
source
builtinisaBASHextensionthatisalsosupportedbyZSH.Other
Bourneshellvariantsdonotsupportthisbuiltin.Formaximumportability,youshouldalwaysuse
theperiod(
.
)builtininstead.
Theseexamplesarenotasstraightforwardastheyseem,however.Whilethisworksverywellforincluding
subroutines,youcannotalwaysusethisinplaceofexecutinganoutsidescript,asexecutionandsourcing
behaveverydifferentlywithrespecttovariables.Thefollowingexampledemonstratesthis:
#!/bin/sh
# Save as sourcetest1.sh
MYVAR=3
. sourcetest2.sh
echo "MYVAR IS $MYVAR"
#!/bin/sh
# Save as sourcetest2.sh
MYVAR=4
Subroutines,Scoping,andSourcing
IncludingOneShellScriptInsideAnother(Sourcing)
2014-03-10   |   Copyright © 2003, 2014 Apple Inc. All Rights Reserved.
90
Documents you may be interested
Documents you may be interested