3.2 Searching and Extracting Data from Files (1): Redirecting Standard Error

Share this article to...

Redirecting Standard Error

Error Messages တွေကိုပဲ လွှဲယူဖို့အတွက် 2> Operator ရဲ့နောက်မှာ Errors တွေရေးမယ့် သိမ်းထားမယ့်ဖိုင်ကို ထည့်ပေးရမှာပါ။ ဖိုင်မရှိလျင် အသစ်ဖန်တီးပေးမှာဖြစ်ပြီး ရှိပြီးသားဆိုပါက Overwrite လုပ်ပြီး ထပ်ရေးပေးသွားမှာပါ။

Standard Error တွေကို လွှဲပြောင်းဖို့အတွက် Channel 2 ကို အသုံးပြုပါတယ်။ လွှဲတဲ့အခါ Default ဖြစ်တဲ့ Standard Output Channel (Channel 1) နှင့် မမှားရအောင် Channel နံပါတ်ကို ထည့်သွင်းဖော်ပြရမှာပါ။ ဥပမာ – အောက်မှာဖော်ပြထားသလို find Command ဟာ ‘games’ ဆိုတဲ့ ဖိုင် (သို့) Directory ကို ရှာတဲ့အခါ ရှာတွေ့တဲ့ Output တွေကို Screen မှာ Standard Output အဖြစ်ပြမှာဖြစ်ပြီး Error တွေကိုတော့ ‘text-error’ ဆိုတဲ့ ဖိုင်ထဲမှာရေးသွားမှာမျိုးပါ။

Note: find Command အတွက် ပိုမိုသိရှိဖို့ Man Page ခေါ်ကြည့်နိုင်ပါတယ်။

ဥပမာ – အောက်ပါ sort Command မှာ Errors မရှိမှာမို့ ‘text-error’ ဖိုင်ထဲကို ဘာမှသွားရေးမှာ မဟုတ်ပါဘူး။

Standard Output မှာလိုပဲ Standard error တွေကိုလည်း 2>> Operator သုံးပြီး ထပ်ထည့်သွားနိုင်ပါတယ်။ Error အသစ်တွေကို ဖိုင်ရဲ့ အဆုံးမှာ ထပ်ဖြည့်သွားမှာမျိုးပါ။ ဖိုင်မရှိခဲ့လျင် အသစ်ဖန်တီးပေးမှာပါ။ ပထမဥပမာက ဖိုင်ထဲသို့ Information အသစ်တွေ ထပ်ဖြည့်တာဖြစ်ပြီး ဒုတိယဥပမာကတော့ နာမည်တူဖြင့် ရှိပြီးသားဖိုင်မတွေ့တဲ့အခါ ဖိုင်အသစ်ဖန်တီးသွားတာပါ။

အခုလို လွှဲလိုက်တဲ့အခါ Error Messages တွေကိုပဲ ဖိုင်ထဲသို့ လွှဲပြောင်းမှာဖြစ်ပြီး ပုံမှန် Output တွေကိုတော့ Screen မှာ ပြတာ၊ (သို့) Standard Output (သို့) stdout အဖြစ် ပြမှာပါ။

Input တွေကို လက်ခံပေမယ့် Process ဘာမှမလုပ်တဲ့ Output ပြန်မထုတ်ပေးမယ့် နည်းပညာအရ အထူးပြုလုပ်ထားတဲ့ Bit Bucket လို့ ခေါ်ကြတဲ့ ဖိုင်လေး (/dev/null) ရှိပါတယ်။ အောက်ပါ ဥပမာမှာ ဖော်ပြထားသလို သိမ်းထားဖို့မလိုအပ်တဲ့ Information တွေ၊ ပြဖို့ မလို၊ အရေးကြီးတဲ့ ဖိုင်တဖိုင်ထဲသို့ လွှဲဖို့ မလိုတဲ့ Information တွေကို အခုလိုပဲ လွှဲနိုင်တာပါ။