Tuesday, April 15, 2014

Batch Operators (Batch File Programming သင္ခန္းစာ-၄)



အျခား Programming language ေတြမွာလိုပဲ Batch Programming မွာလည္း operator ေတြရွိပါတယ္။
Operators
Description
()
Grouping
!  ~  -
Unary Operators
*  /  %  +  -
Arithmetic Operators
<<  >>  < >
Logical shift and re directional operators
&
Bitwise and
^
Bitwise exclusive or
=  *=  /=  %=  +=  -=  &=  ^=  |=  <<=  >>=
Assignment operators
,
Separator
&&
For using Multiple commands
||
For executing one from many commands
Operator ေတြကိုစမ္းသံုးၾကည့္ရေအာင္။
သခ်ာၤဆိုင္ရာလုပ္ငန္းေတြကိုေဆာင္ရြက္ရာမွာ arithmetic operator ေတြကိုသံုးပါမယ္။ ‘set’ ဆိုတဲ့ command ေနာက္မွာ ‘/a’ ဆိုတဲ့ switch ကိုတပ္ေပးလိုက္ရင္ ေပါင္း၊ႏုတ္၊ေျမာက္၊စား လုပ္လို႔ရပါၿပီ။
4 ႏွင့္ 5 ကိုေပါင္းၾကည့္ရေအာင္။
C:\>set /a 4+5
9
8 မွာ 2 ကုိႏုတ္ၾကည့္ရေအာင္။
C:\>set /a 8-2
6
8 ႏွင့္ 2 ကိုေျမာက္ၾကည့္ရေအာင္။ အေျမာက္အျဖစ္ ‘*’ (asterisk) ကိုသံုးပါတယ္။   
C:\>set /a 8*2
16
8 ကို 2 ႏွင့္စားၾကည့္ရေအာင္။ အစားအျဖစ္ ‘/’ (slash) ကိုသံုးပါတယ္။ စားလဒ္ကိုထုတ္ေပးပါတယ္။
C:\>set /a 8/2
4
7 ကို 3 ႏွင့္စားၿပီးအႂကြင္းထုတ္ရေအာင္။ စားၿပီးက်န္ေသာအႂကြင္းလိုခ်င္ရင္ ‘%’ (percentage) ကိုသံုးပါတယ္။
C:\>set /a 7%3
1

Operator precedence

Operator ေတြမွာဦးစားေပးအစီစဥ္ေတြရွိပါတယ္။ သခ်ာၤဆိုင္ရာ ဦးစားေပးလုပ္ေဆာင္မႈႏွင့္အတူတူပါပဲ။ အစီစဥ္ကေတာ့  *, /, %, +, -  တို႔ပါပဲ။ Grouping operator ျဖစ္တဲ့ ‘()’ ကေတာ့ ပထမဦးစားေပးအဆင့္မွာပါပါတယ္။ Group ဖြဲ႔ထားရင္ Group ထဲကကိန္းေတြကိုအရင္တြက္ရမွာျဖစ္ပါတယ္။
ဒီကဗ်ာေလးကိုက်က္ထားရင္ သခ်ာၤမွာေရာ၊ programming မွာပါအသံုးဝင္ပါလိမ့္မယ္။
“ကြင္း၏ေျမာက္စားေပါင္းႏုတ္”  “ () ၏ * / + - ”
စမ္းသပ္ၾကည့္ရေအာင္။
C:\>set /a (4+2)*3+4/2
20 
() ထဲကကိန္းကိုအရင္ရွင္းပါမယ္ (4+2 = 6) ။ ၿပီးရင္ အေျမာက္ကိုရွင္းမယ္ (6*3=18)။ ၿပီးရင္ အစား (4/2=2) ။ ေနာက္ဆံုးမွ အေပါင္းကိုရွင္း (18+2=20) ။
ယခု redirectional operators အေၾကာင္းရွင္းျပပါမယ္။ redirectional operators ျဖစ္တဲ့ ‘<’ ႏွင့္ ‘>’ က command prompt မွာ output ကိုမထုတ္ပဲ အျခား text file ေတြမွာ output ထုတ္ခ်င္ရင္သံုးပါတယ္။ ‘echo’ command ႏွင့္တြဲသံုးရပါတယ္။ စမ္းသပ္ၾကည့္ရေအာင္။
ယခု “hello notepad” စာသားကို  ‘hello.txt’ file ထဲမွာ output ထုတ္ပါမယ္။ ေနာက္ “hello wordpad” စာသားကို ‘hello.doc’ ထဲမွာ output ထုတ္ပါမယ္။
C:\>echo hello notepad > hello.txt
C:\>echo hello wordpad > hello.doc
အထက္ပါတိုင္း command prompt မွာရိုက္လိုက္ၿပီး Enter ေခါက္လိုက္ပါက C directory ေအာက္မွာ ‘hello.txt’  ႏွင့္ ‘hello.doc’ file ႏွစ္ခုကိုေတြ႔ရမွာပါ။ ထို file ေတြကိုဖြင့္ၾကည့္ပါက ‘hello.txt’ မွာ “hello notepad”  စာသားကိုေတြ႔ရမွာျဖစ္ၿပီး ‘hello.doc’ မွာ “hello wordpad” စာသားကိုေတြ႔ရမွာျဖစ္ပါတယ္။
Unary operator တစ္ခုျဖစ္တဲ့ tilde ‘~’ ကေတာ့ ရွည္လ်ားလွတဲ့ directory name ေတြကိုအတိုခ်ံဳ႕ေဖာ္ျပရာတြင္ သံုးပါတယ္။
C:\Program Files\Drive Space Indicator ဆိုတဲ့ directory ထဲဝင္ခ်င္တယ္ဆိုပါစို႔။ ‘cd’ command ေနာက္မွာ path လမ္းေၾကာင္းကိုရိုက္ၿပီးသြားလို႔ရပါတယ္။ ဒါေပမယ့္အတိုခ်ံဳ႕ခ်င္ရင္ေတာ့ directory name ေရွ႕ဆံုး စာလံုး ၆ လံုးအရင္ရိုက္၊ ၿပီးရင္ ‘~’ ႏွင့္ 1 ႏွင့္ရိုက္လိုက္ရင္ရပါၿပီ။ directory name က ၆ လံုးမျပည့္ခင္ space ျခားထားရင္ ကိုယ္ကကပ္ၿပီးေရးပါ။ directory name ေရွ႕ဆံုး စာလံုး ၆ လံုးလံုးတူတဲ့ directory ႏွစ္ခုရွိေနရင္ေတာ့ ပထမ directory ကိုသာအတည္ယူပါတယ္။
C:\>cd c:\progra~1\drives~1
C:\PROGRA~1\DRIVES~1>
Run box ထဲမွာ command လို႔ရိုက္ထည့္ၿပီး Enter ေခါက္ပါ။ ၄င္းထဲမွ directory path မ်ားသည္ tilde မ်ားျဖင့္သာအသံုးျပဳရပါတယ္။ ‘~’ ဆိုတဲ့ unary operator သံုးတိုင္း ‘1’ ကမပါမျဖစ္ထည့္ေပးရပါတယ္။
‘&&’ ကိုေတာ့ command အမ်ားႀကီးကိုတစ္ေၾကာင္းထဲမွာေပါင္းေရးခ်င္ရင္သံုးပါတယ္။ဥပမာ.. စာသားကို output အျဖစ္ထုတ္မယ္ စသျဖင့္ command ႏွစ္ခုကိုတစ္ေၾကာင္းတည္းေရးပါမယ္။ အဲဒီအတိုင္းပဲ ‘&&’ ႏွင့္ဆက္ၿပီး command ႏွစ္ခုထက္မကေရးလို႔ရပါတယ္။
C:\>echo Batch Program Addition && set /a 2+6
Batch Program Addition
8
Pipe Line operator (|)ကို ကိုယ္ output ထုတ္ခ်င္တဲ့အရာဟာ အျခား command ရဲ႕ input တစ္ခုအေနႏွင့္ သံုးခ်င္ရင္အသံုးျပဳပါတယ္။
C:/>echo Y|del *.txt

(မ်ိဳးသူထက္)

No comments:

Post a Comment