3.2 Searching and Extracting Data from Files (1): Redirecting Standard Input & Here Documents

Share this article to...

Redirecting Standard Input

            Keyboard မှ Input ပေးမယ့်အစား၊ Command တွေ ရိုက်မယ့်အစား၊ သက်ဆိုင်ရာဖိုင်တစ်ဖိုင်မှ Input ရယူဖို့ရာ အသုံးပြုတဲ့ Redirection ပုံစံတစ်မျိုးပါ။ “<” Operator ကို အသုံးပြုပြီး ဒီဥပမာမှာ ဖော်ပြထားပါတယ်။

Standard Input Redirection ကို ပုံမှန်အားဖြင့်တော့ File Arguments တွေလက်မခံတတ်တဲ့ Commands တွေနှင့် တွဲသုံးလေ့ရှိတာပါ။ “tr” Command ဟာ တစ်ခုအပါအဝင်ပါ။ ဖိုင်ထဲက Characters တွေကို ပြင်တာ၊ Character တစ်ခုခုကို ဖျက်တာတွေလုပ်ဖို့ ပြောင်းလဲဖို့ရာ သုံးနိုင်ပါတယ်။ အောက်ပါ ဥပမာမှာတော့ ‘l’ Character Deletion ကို ပြထားပါတယ်။

            အချက်အလက်တွေပိုမိုသိရှိဖို့အတွက် “tr” Command ရဲ့ Man Page မှာ ကြည့်နိုင်ပါတယ်။

Here Documents

            Output Redirections တွေနှင့်မတူဘဲ “<<” Operator ဟာ မတူတဲ့ အသွင်တမျိုးရှိပါတယ်။ အဲ့ဒီ Input Stream ကို Here Document လို့လည်း ခေါ်ပါတယ်။ Here Document ဆိုတာ Command (သို့) Interactive Program ဆီသို့ လွှဲပေးနိုင်တဲ့ Text (သို့) Code Block ပါပဲ။ bash, sh နှင့် csh တွေလို Scripting Languages တွေဟာ Text Files တွေမလိုဘဲ Command Line မှ Input ကို တိုက်ရိုက်ရယူနိုင်ကြပါတယ်။

အောက်ပါဥပမာမှာလို “<<” Operator ကို Command ထဲသို့ Data Input ပေးဖို့ (Word တစ်လုံးသာ သုံးပြီး File Name တွဲလျက်မဖော်ပြဘဲ) အသုံးပြုနိုင်တာပါ။ အဲ့ဒီ စကားလုံး Word လေးကို Input တွေရဲ့ အဆုံးသတ်စကားလုံး Delimiter အဖြစ် မှတ်ယူပြီး Content မဟုတ်တာမို့ “cat” Command ဖြင့် ပြန်ကြည့်တဲ့အခါ သူ့ကို ပြမှာမဟုတ်ပါဘူး။

cat” Command အကြောင်း ပိုမိုသိရှိနိုင်ဖို့ Man Page ခေါ်ကြည့်ပေးပါ။