Академический Документы
Профессиональный Документы
Культура Документы
bcCommandExamplesinUnix/Linuxtutorials
Home
DataWarehouse
Informatica
InformaticaScenarios
Oracle
Unix
Hadoop
bcCommandExamplesinUnix/Linuxtutorials
Arithmeticoperationsarethemostcommoninanykindofprogramminglanguage.Unixor
linuxoperatingsystemprovidesthebccommandandexprcommandfordoingarithmetic
Search...
Search
PopularPosts
SedCommandinUnixandLinuxExamples
calculations.Youcanusethesecommandsinbashorshellscriptalsoforevaluating
arithmeticexpressions.
Herewewillseeonlyaboutthebccommand.Thebccommandevaluatesexpressionssimilar
tothecprogramminglanguage.Thebccommandsupportsthefollowingfeatures.
Arithmeticoperators
Incrementanddecrementoperators
Assignmentoperators
ComparisionorRelationalOperators
LogicalorBooleanoperators
MathFunctions
Conditionalstatements
Iterativestatements
Functions
CutCommandinUnix(Linux)Examples
TopExamplesofAwkCommandinUnix
FindCommandinUnixandLinuxExamples
InformaticaScenarioBasedInterviewQuestionswith
AnswersPart1
DateFunctionsinHive
GrepCommandinUnixandLinuxExamples
StringFunctionsinHive
SQLQueriesInterviewQuestionsOraclePart1
TypesofDimensionsindatawarehouse
ArithmeticoperatorExamples:
Thefollowingexampleshowshowtousevariousarithmeticoperators.Theexamplesare
HaveQuestions?FollowMe
prettystraightforward.So,Iwillprovideexplanationonlywhenrequired.Inmostofthe
examplestheechostatmentisusedtoprovidetheexpressionstothebccommand.
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html
1/11
3/4/2016
bcCommandExamplesinUnix/Linuxtutorials
vijaybhaskar
1.FindingSumofTwoexpressions
Addtocircles
>echo"2+5"|bc
7
2.DifferenceofTwonumbers
>echo"104"|bc
6
604havemeincircles
Viewall
3.Multiplyingtwonumbers
>echo"3*8"|bc
24
4.Dividingtwonumbers
Whenyoudividetwonumbers,thebccommandIgnoresthedecimalpartandreturnsonly
theintegralpartastheoutput.Seethebelowexamples
>echo"2/3"|bc
0
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html
2/11
3/4/2016
bcCommandExamplesinUnix/Linuxtutorials
>echo"5/4"|bc
1
Usethescalefunctiontospecifythenumberofdecimaldigitsthatthebccommandshould
return.
>echo"scale=2;2/3"|bc
.66
5.Findingtheremainderusingmodulusoperator
>echo"6%4"|bc
2
6.Usingexponentoperator
>echo"10^2"|bc
100
Heretheexpressionisevaluatedas10tothepowerof2.
AssignmentOperatorExamples:
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html
3/11
3/4/2016
bcCommandExamplesinUnix/Linuxtutorials
Assignmentoperatorsareusedtoassignavaluetothevariable.Thefollowingexample
showshowtousetheassignmentoperators:
Assigns10tothevariableandprintsthevalueontheterminal.
>echo"var=10;var"|bc
Incrementthevalueofthevariableby5
>echo"var=10;var+=5;var|bc
15
Thelistsofassignmentoperatorssupportedare:
var=value:Assignthevaluetothevariable
var+=value:similartovar=var+value
var=value:similartovar=varvalue
var*=value:similartovar=var*value
var/=value:similartovar=var/value
var^=value:similartovar=var^value
var%=value:similartovar=var%value
IncrementOperatorExamples:
Therearetwokindsofincrementoperators.Theyarepreincrementandpostincrement
operators.
++var:Preincrementoperator.Thevariableisincrementedfirstandthenthe
resultofthevariableisused.
var++:Postincrementoperator.Theresultofthevariableisusedfirstandthen
thevariableisincremented.
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html
4/11
3/4/2016
bcCommandExamplesinUnix/Linuxtutorials
>echo"var=5;++var"|bc
6
>echo"var=5;var++"|bc
5
Here,inthesecondexamplethevalueofvarisprintedfirstandthenitisincremented.See
thebelowexample,toseethecompleteincrementaleffect.
>echo"var=5;var++;var"|bc
5
6
DecrementOperatorExamples:
Similartotheincrementoperators,therearetwotypesofdecrementoperators.
var:Predecrementoperator.Thevariableisdecrementedfirstandthenthe
resultofthevariableisused.
var:Postdecrementoperator.Theresultofthevariableisusedfirstandthen
thevariableisdecremented.
>echo"var=5;var"|bc
4
>echo"var=5;var"|bc
5
RelationalOperatorsExamples:
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html
5/11
3/4/2016
bcCommandExamplesinUnix/Linuxtutorials
Relationaloperatorsareusedtocomparetwonumbers.Ifthecomparisonistrue,thenit
returns1.Otherwise(false),itreturns0.Therelationaloperatorsaremostlyusedin
conditionalstatementslikeif.Thelistofrelationaloperatorssupportedinbccommandare
shownbelow:
expr1<expr2:Resultis1ifexpr1isstrictlylessthanexpr2.
expr1<=expr2:Resultis1ifexpr1islessthanorequaltoexpr2.
expr1>expr2:Resultis1ifexpr1isstrictlygreaterthanexpr2.
expr1>=expr2:Resultis1ifexpr1isgreaterthanorequaltoexpr2.
expr1==expr2:Resultis1ifexpr1isequaltoexpr2.
expr1!=expr2:Resultis1ifexpr1isnotequaltoexpr2.
>echo"10>5"|bc
1
>echo"1==2"|bc
0
LogicalOperatorExamples:
Logicaloperatorsarealsomostlyusedinconditionalstatements.Theresultofthelogical
operatorsiseither1(True)or0(false)!expr:Resultis1ifexpris0.
expr&&expr:Resultis1ifbothexpressionsarenonzero.
expr||expr:Resultis1ifeitherexpressionisnonzero.
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html
6/11
3/4/2016
bcCommandExamplesinUnix/Linuxtutorials
>echo"4&&10"|bc
1
>echo"0||0"|bc
0
MathFunctions:
Thebuiltinmathfunctionssupportedare:
s(x):Thesineofx,xisinradians.
c(x):Thecosineofx,xisinradians.
a(x):Thearctangentofx,arctangentreturnsradians.
l(x):Thenaturallogarithmofx.
e(x):Theexponentialfunctionofraisingetothevaluex.
j(n,x):Thebesselfunctionofintegerordernofx.
sqrt(x):Squarerootofthenumberx.
Inadditiontothemathfunctions,thefollowingfunctionsarealsosupported.
length(x):returnsthenumberofdigitsinx
read():Readsthenumberfromthestandardinput.
ConditionalStatementExamples:
Conditionalstatementsareusedtotakedecisionsandexecutestatementsbasedonthese
decisions.Bccommandsupportstheifcondition.Thesyntaxofifstatementis
if(condition){statements}else{statements}
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html
7/11
3/4/2016
bcCommandExamplesinUnix/Linuxtutorials
Thefollowingexampleshowsshowtousetheifcondition
>echo'if(1==2)print"true"elseprint"false"'|bc
false
IterativeStatements:
Bccommandsupportstheforandwhileloopfordoingiterations.Thesyntaxofforandwhile
loopareshownbelow:
for(assignment;condition;increment){
statements
}
while(condition){
statements
}
Thefollowingexamplesprintsnumbersfrom1to10usingtheforandwhileloops
>echo"for(i=1;i<=10;i++){i;}"|bc
>echo"i=1;while(i<=10){i;i+=1}"|bc
Functions:
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html
8/11
3/4/2016
bcCommandExamplesinUnix/Linuxtutorials
Afunctionisacodeblockwhichexecuteslogicallyrelatedfunctionalityandreturnsavalue.
Thesyntaxofcreatingafunctionis
definefunctionname(commaseparatedparameterslist){
statements
returnstatement
}
Sofarwehaveprovidedthearithmeticexpressionstothebccommandbyusingtheecho
statement.Wecanwritethesearithmeticexpressionsinafileandthenexecutethose
statementsbyprovidingthefilenametothebccommand.Thisisshownbelow:
>catarth_expr.dat
2+5;
var=10*3
var
printvar
definesum(a,b){
returna+b
}
sum(3,5)
quit
Nowseehowtoexecutethesestatements:
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html
9/11
3/4/2016
bcCommandExamplesinUnix/Linuxtutorials
>bcarth_expr.dat
bc1.06
Copyright19911994,1997,1998,2000FreeSoftwareFoundation,Inc.
ThisisfreesoftwarewithABSOLUTELYNOWARRANTY.
Fordetailstype`warranty'.
7
30
30
8
Bedefaultthebccommandprintsthewelcomemessage(version,copyrightmessage.You
cansuppressthiswelcomemessagebyusingtheqoptionwithbccommand
>bcqarth_expr.dat
ImportantPoints:
Bccommandtreatsthesemicolon()ornewlineasthestatementseparator.
Togroupstatementsusethecurlybraces.Usewithfunctions,ifstatement,forand
whileloops.
Ifonlyanexpressionisspecifiedasastatement,thenbccommandevaluatesthe
expressionandprintstheresultonthestandardoutput.
Ifanassignmentoperatorisfound.Bccommandassignsthevaluetothevariable
anddonotprintthevalueontheterminal.
Afunctionshouldbedefinedbeforecallingit.Alwaysthefunctiondefinitionshould
appearfirstbeforethecallingstatements.
Ifastandalonevariableisfoundasastatement,bccommandprintsthevalueof
thevariable.YoucanalsoUsetheprintstatementfordisplayingthelistofvalues
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html
10/11
3/4/2016
bcCommandExamplesinUnix/Linuxtutorials
ontheterminal.
NewerPost
Home
pnrstatus
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html
OlderPost
privacypolicy
11/11