Thuta Learning
ရှာဖွေရန်
IntermediateDevOps & Toolsbeginner

Shell Scripting အခြေခံ

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Shell Scripting အခြေခံ ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • Terminal ထဲမှာ ကိုယ်တိုင် command ကို စမ်းသုံးတတ်အောင် လုပ်မယ်
  • Real server/project မှာ ဒီ command ဘယ်လို အသုံးဝင်သလဲ ချိတ်မြင်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

Shell script ဆိုတာ command များစုစည်းထားတဲ့ text file တစ်ခုပါ — .sh extension ကို convention အရ သုံးလေ့ရှိပေမယ့် Linux မှာ extension က execute ဖြစ်/မဖြစ် သက်ရောက်မှု မရှိပါဘူး (permission (x) ကသာ အဓိကကျ)။ ဖိုင်ရဲ့ ပထမလိုင်းမှာ #!/bin/bash ဆိုတဲ့ 'shebang' line ရေးထားရပါတယ် — ဒါက 'ဒီ script ကို bash နဲ့ run ပါ' ဆိုတဲ့ ညွှန်ကြားချက်ပါ။ Variable ကို NAME="value" ပုံစံနဲ့ ဖန်တီးပြီး $NAME နဲ့ ခေါ်သုံးပါတယ် (space မထားရ — NAME = value လို space ထားရင် error ဖြစ်ပါတယ်)။ Comment ရေးချင်ရင် # ကြိုတပ်ပါ။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Server backup လုပ်တဲ့အခါ, project setup လုပ်တဲ့အခါ command ၁၀ ကြောင်းကို လက်ခြင်းရိုက်နေမယ့်အစား backup.sh script တစ်ခု ရေးထားရင် ./backup.sh command တစ်ခုတည်းနဲ့ လုပ်ငန်းစဉ်တစ်ခုလုံး ပြီးသွားပါတယ် — အမှားလုပ်ခွင့်လည်း လျော့သွားပါတယ် (ကိုယ်ရေးထားတဲ့ command အတိုင်း အမြဲတမ်း အတိအကျ run မှာမို့)။ Script ရေးပြီးရင် chmod +x (ယခင်အခန်းကနေ) ကို ထပ်ခါထပ်ခါ လိုအပ်ပါလိမ့်မယ်။

အတူတူ terminal ထဲ စမ်းမယ်

bash
#!/bin/bash
# Simple greeting script
NAME="Linux Learner"
echo "Hello, $NAME!"
echo "Today is $(date)"
You should see
./greet.sh run ရင် 'Hello, Linux Learner!' နှင့် today's date ကို terminal ပေါ်တွင် print ထုတ်လာမည်။

၅ မိနစ် စမ်းကြည့်

greet.sh ဆိုတဲ့ script ဖန်တီးပါ — shebang line, variable တစ်ခု, echo နှစ်ကြောင်း ပါစေပါ။ chmod +x လုပ်ပြီး run ကြည့်ပါ။

သတိလေးတစ်ချက်

Script ထဲမှာ dangerous command (rm, sudo) ပါရင် run ခင် script ကို ကိုယ်တိုင်တစ်ကြိမ် အသေးစိတ် ဖတ်ပါ — script ကို run လိုက်ရင် line တိုင်း အလိုအလျောက် run ဖြစ်သွားပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • NAME = "value" လို variable assignment မှာ space ထားပြီး 'command not found' error ရခြင်း
  • Shebang line (#!/bin/bash) ကို ဖိုင်ရဲ့ ပထမလိုင်းမှာ မထားဘဲ ကျန်တဲ့ comment ကြားမှာ ထားခြင်း

အခု ကိုယ်တိုင် စမ်းကြည့်

greet.sh ဆိုတဲ့ script ဖန်တီးပါ — shebang line, variable တစ်ခု, echo နှစ်ကြောင်း ပါစေပါ။ chmod +x လုပ်ပြီး run ကြည့်ပါ။

You'll know it worked when: ./greet.sh run ရင် 'Hello, Linux Learner!' နှင့် today's date ကို terminal ပေါ်တွင် print ထုတ်လာမည်။

Shell Scripting အခြေခံ | Thuta Learning