Download Comparison Between Python and Korn Shell - Lecture Slides | ECE 36400 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!
ECE 3 6 4 : S o f t w a re To o lsECE 3 6 4 : S o f t w a re To o ls
La b o ra t o ry La b o ra t o ry
Le c t u re 7 Le c t u re 7
Fe b ru a ry 2 3 , 2 0 0 8 Fe b ru a ry 2 3 , 2 0 0 8
Re m in d e rsRe m in d e rs
Pra c t ic a l Exa m 1 is in t wo we e ksPra c t ic a l Exa m 1 is in t wo we e ks
It will c o ve r a ll m a t e ria l u p t o a n d in c lu d in gIt will c o ve r a ll m a t e ria l u p t o a n d in c lu d in g
t h is le c t u re (Le c t u re 7 )t h is le c t u re (Le c t u re 7 )
No e xt e rn a l m a t e ria lsNo e xt e rn a l m a t e ria ls
Yo u will b e p ro vid e d wit h b o o ks a n d le c t u reYo u will b e p ro vid e d wit h b o o ks a n d le c t u re
n o t e sn o t e s
Op t io n a l He lp Se s s io n n e xt we e kOp t io n a l He lp Se s s io n n e xt we e k
Re g ra d e re q u e s t sRe g ra d e re q u e s t s
Ch e a t in gCh e a t in g
Le c t u re 0 7Le c t u re 0 7
1.1. Co m p a ris o n b e t we e n Pyt h o n a n dCo m p a ris o n b e t we e n Pyt h o n a n d
Ko rn Sh e ll Ko rn Sh e ll
S a m p le p ro g ra m sS a m p le p ro g ra m s
Co p ie s o f t h e p ro g ra m s c o ve re d in t h isCo p ie s o f t h e p ro g ra m s c o ve re d in t h is
c la s s c a n b e fo u n d in t h e fo llo win gc la s s c a n b e fo u n d in t h e fo llo win g
d ire c t o ry:d ire c t o ry:
/home/elwomnt/a/ee364/LEC-XX/home/elwomnt/a/ee364/LEC-XX
(o r,(o r,^ ~ee364/LEC-XX~ee364/LEC-XX))
wh e rewh e re XXXX is t h e le c t u re n u m b e r.is t h e le c t u re n u m b e r.
~ee364/LEC-07~ee364/LEC-
fo r in s t a n c efo r in s t a n c e
To d a y 's le c t u reTo d a y 's le c t u re
So m e t im e s KSH is e a s ie r.So m e t im e s KSH is e a s ie r.
So m e t im e s Pyt h o n is e a s ie r.So m e t im e s Pyt h o n is e a s ie r.
Ch o o s in g t h e rig h t t o o l c a n b e t h eCh o o s in g t h e rig h t t o o l c a n b e t h e
m o s t im p o rt a n t t h in g y o u d o .m o s t im p o rt a n t t h in g y o u d o.
Th e p ra c t ic a l h a s b o t h KSH a n d Pyt h o n .Th e p ra c t ic a l h a s b o t h KSH a n d Pyt h o n.
Th is will h e lp yo u p re p a re .Th is will h e lp yo u p re p a re.
Co m m a n d lin e p a ra m e t e rs -Co m m a n d lin e p a ra m e t e rs -
Ko rn S h e ll Ko rn S h e ll
print "Number of parameters: $#"print "Number of parameters: $#"
print "Executable name is $0"print "Executable name is $0"
I=0I=
while (( $# > 0 ))while (( $# > 0 ))
dodo
(( ++I ))(( ++I ))
print "Parameter ${I}: $1"print "Parameter ${I}: $1"
shiftshift
donedone
Co m m a n d lin e p a ra m e t e rs -Co m m a n d lin e p a ra m e t e rs -
P y t h o n P y t h o n
import sysimport sys
print "Number of parameters: ", \print "Number of parameters: ", \
len(sys.argv)-1len(sys.argv)-
print "Executable name is ", sys.argv[0]print "Executable name is ", sys.argv[0]
I = 0I = 0
for I in range(1, len(sys.argv)):for I in range(1, len(sys.argv)):
print "Parameter %d: %s" % \print "Parameter %d: %s" % \
(I, sys.argv[I]) (I, sys.argv[I])
Ex a m p le sEx a m p le s
Le t 's t a ke a lo o k a t t h e p ro g ra m sLe t 's t a ke a lo o k a t t h e p ro g ra m s
1_Parameters.ksh1_Parameters.ksh
1_Parameters.py1_Parameters.py
P e rm is s io n s - Ko rn S h e llP e rm is s io n s - Ko rn S h e ll
Filename="Some_File_Name"Filename="Some_File_Name"
if [[! -r ${Filename} ]]; thenif [[! -r ${Filename} ]]; then
print -n "File: ${Filename}"print -n "File: ${Filename}"
print " is not readable"print " is not readable"
fifi
P e rm is s io n s - P y t h o nP e rm is s io n s - P y t h o n
import osimport os
Filename = "Some_File_Name"Filename = "Some_File_Name"
if os.access(Filename, os.R_OK) != 1:if os.access(Filename, os.R_OK) != 1:
print "File: %s is not readable" \print "File: %s is not readable" \
% (Filename) % (Filename)
Ex a m p le sEx a m p le s
Le t 's t a ke a lo o k a t t h e p ro g ra m sLe t 's t a ke a lo o k a t t h e p ro g ra m s
2_Read_Stdin.ksh2_Read_Stdin.ksh
2_Read_Stdin.py2_Read_Stdin.py
Th e s e p ro g ra m s re a d fro m s t a n d a rdTh e s e p ro g ra m s re a d fro m s t a n d a rd
in p u t a n d p rin t t o s t a n d a rd o u t p u t , b u tin p u t a n d p rin t t o s t a n d a rd o u t p u t , b u t
d o n 't fo rg e t a b o u t re d ire c t io n
d o n 't fo rg e t a b o u t re d ire c t io n
2_Read_Stdin.ksh < Some_File2_Read_Stdin.ksh < Some_File
2_Read_Stdin.py < Some_File2_Read_Stdin.py < Some_File
File I/O - Ko rn S h e llFile I/O - Ko rn S h e ll
while read -A Line; dowhile read -A Line; do
I=0I=
while (( I < ${#Line[]} )); dowhile (( I < ${#Line[]} )); do
print "I: $I $Line[I]" print "I: $I $Line[I]"
(( ++I )) (( ++I ))
donedone
done < Some_Filedone < Some_File
Wh e re 's t h e b u g ?Wh e re 's t h e b u g?
Ve lo c ira p t o rsVe lo c ira p t o rs
"It 's a UNIX s ys t e m!
I kn o w t h is !""It 's a UNIX s ys t e m! I kn o w t h is !"
P ro g ra m t e s t in g - Ko rn S h e llP ro g ra m t e s t in g - Ko rn S h e ll
Wh a t if we wa n t t o ru n a p ro g ra m wit h m u lt ip leWh a t if we wa n t t o ru n a p ro g ra m wit h m u lt ip le
in p u t file s ?in p u t file s?
Xec="C_Xec"Xec="C_Xec" forfor DataData inin Input_;Input_; dodo printprint "Running"Running ${Xec}${Xec} withwith ${Data}"${Data}" rmrm -f-f corecore ${Xec}${Xec} << ${Data}${Data} 2>&12>& ifif (((( $?$? !=!= 00 )))) |||| [[[[ -f-f corecore ]];]]; thenthen printprint "File:"File: ${Xec}${Xec} failedfailed withwith ${Data}"${Data}" fifi donedone 2>&12>&
P ro g ra m t e s t in g - Ko rn S h e llP ro g ra m t e s t in g - Ko rn S h e ll
Xec="C_Xec"Xec="C_Xec" forfor DataData inin Input_;Input_; dodo printprint "Running"Running ${Xec}${Xec} withwith ${Data}"${Data}" rmrm -f-f corecore Output=$(${Xec}Output=$(${Xec} << ${Data}${Data} 2>&1)2>&1) ifif (((( $?$? !=!= 00 )))) |||| [[[[ -f-f corecore ]];]]; thenthen printprint "File:"File: ${Xec}${Xec} failedfailed withwith ${Data}"${Data}" fifi printprint^ ${Output}${Output} donedone 2>&12>&
P ro g ra m t e s t in g - P y t h o nP ro g ra m t e s t in g - P y t h o n
import stringimport string import commandsimport commands import osimport os Xec = "Some_C_a.out"Xec = "Some_C_a.out" Input_List = commands.getoutput("ls Input_").split()Input_List = commands.getoutput("ls Input_").split() for Data in Input_List:for Data in Input_List: print "Running", Xec, "with", Dataprint "Running", Xec, "with", Data os.system("rm -f core")os.system("rm -f core") [Status, Output] = commands.getstatusoutput([Status, Output] = commands.getstatusoutput( Xec + " < " + Data)Xec + " < " + Data) if (Status != 0) or (os.access("core", 0)):if (Status != 0) or (os.access("core", 0)): print "File:", Xec, "failed with", Dataprint "File:", Xec, "failed with", Data print Outputprint Output
Ex a m p le sEx a m p le s
Le t 's t a ke a lo o k a t t h e p ro g ra m sLe t 's t a ke a lo o k a t t h e p ro g ra m s
5_Test_Program.ksh5_Test_Program.ksh
5_Test_Program.py5_Test_Program.py
6_Test_Program.ksh6_Test_Program.ksh
7_Check.ksh7_Check.ksh
7_Check.py7_Check.py
8_Check.ksh8_Check.ksh
8_Check.py8_Check.py
Me n u s - Ko rn S h e llMe n u s - Ko rn S h e ll
#! /bin/ksh#! /bin/ksh PS3="Select a food item: "PS3="Select a food item: " select Item in Burger Fries Shake Quit; doselect Item in Burger Fries Shake Quit; do printprint case ${REPLY} incase ${REPLY} in
- print "That will be $500.00"1) print "That will be $500.00" REPLY="";;REPLY="";;
- print "That will be $10.00"2) print "That will be $10.00" REPLY="";;REPLY="";;
- print "That will be $80.00"3) print "That will be $80.00" REPLY="";;REPLY="";;
- print "You made the right choice!"4) print "You made the right choice!" break;;break;; ) print "Invalid entry!") print "Invalid entry!" REPLY=""REPLY="" esacesac donedone printprint exit 0exit 0
Me n u s - P y t h o nMe n u s - P y t h o n
#! /usr/bin/env python#! /usr/bin/env python import sysimport sys Items=("Burger", "Fries", "Shake", "Quit")Items=("Burger", "Fries", "Shake", "Quit") Reply = NoneReply = None while Reply == None:while Reply == None: printprint for I in range(1, len(Items)+1):for I in range(1, len(Items)+1): print "%d) %s" % (I, Items[I-1])print "%d) %s" % (I, Items[I-1]) print "Select a food item: ",print "Select a food item: ", Reply = sys.stdin.readline().strip()Reply = sys.stdin.readline().strip() try:try: Reply = int(Reply)Reply = int(Reply) except ValueError:except ValueError: printprint print "Invalid entry!"print "Invalid entry!" Reply = NoneReply = None else:else: printprint if (Reply < 0) orif (Reply < 0) or (Reply > len(Items)):(Reply > len(Items)): print "Invalid entry!"print "Invalid entry!" Reply = NoneReply = None elif Reply == 1: elif Reply == 1: print "That will be $500.00"print "That will be $500.00" Reply = NoneReply = None elif Reply == 2: elif Reply == 2: print "That will be $10.00"print "That will be $10.00" Reply = NoneReply = None elif Reply == 3: elif Reply == 3: print "That will be $80.00"print "That will be $80.00" Reply = NoneReply = None else: else: print "You made theprint "You made the right choice!"right choice!" passpass sys.exit(0) sys.exit(0)
Ex a m p le sEx a m p le s
Le t 's t a ke a lo o k a t t h e p ro g ra m sLe t 's t a ke a lo o k a t t h e p ro g ra m s
9_Menu.ksh9_Menu.ksh
9_Menu.py9_Menu.py
Ex a m p le sEx a m p le s
On e la s t lit t le fu n s c rip t
On e la s t lit t le fu n s c rip t
10_Trap.ksh10_Trap.ksh
As s ig n m e n t f o r La b 8As s ig n m e n t f o r La b 8
Do t h e p re la b , it will b e wo rt hDo t h e p re la b , it will b e wo rt h 5 p o in t s5 p o in t s
o u t o f yo u r fin a l la b s c o reo u t o f yo u r fin a l la b s c o re
Brin g u pBrin g u p idleidle a n d wo rk t h ro u g h a ll o f t h ea n d wo rk t h ro u g h a ll o f t h e
e xa m p le s c o ve re d d u rin g le c t u ree xa m p le s c o ve re d d u rin g le c t u re
Ch e c k o u t t h e c o m m a n d s m o d u le inCh e c k o u t t h e c o m m a n d s m o d u le in
Be a zle y (if yo u n e e d t o )Be a zle y (if yo u n e e d t o )
Re a d Ro s e n b e rg (if yo u n e e d t o )Re a d Ro s e n b e rg (if yo u n e e d t o )