DevOps_UNIX / Linux_8_I/O_රිඩිරෙක්ෂන්_සහ_Piping.
DevOps_UNIX / Linux_8_I/O_රිඩිරෙක්ෂන්_සහ_Piping.
අපි අද කතාකරන්නේ I/O redirection සහ Piping පිලිබදවයි.
මුලින්ම අපි Standard Input, Standard Output සහ Standard Error ගැන හොද අවබෝදයක් ලබාගනිමු. මම ඔයාලට මේ දේවල් ඉතාම සරල විදියට තේරුම් කරලා දෙන්නම්. ඔයාලට තියෙන්නෙ හොදට අවබෝද කරගෙන මම කියපු කරුණු මතකයේ තබාගෙන නිතර බාවිතා කරන එක.
මෙන්න මෙහෙමයි අපේ කතාන්දරය පටන් ගන්නේ. UNIX / Linux කමාන්ඩ් එකක් execute කලට පසු, kernel එක ස්වයන්ක්රියව files 3ක් open කරනව. අපි ඒ files වලට කියනව standard files කියල. ඒ file තුන තමා
පෙරනිමි ලෙස (By default) සියලුම UNIX / Linux කමාන්ඩ්ස් ඒවයෙ input ලබාගන්නෙ stdin (standard input) මගිනි, කමාන්ඩ්ස් වල result එක stdout (standard output) වෙත යොමුකරනව. ඒ වගේම යම්කිසි දෝෂයක් ඇතිවූ විට එය stderr (standard error) වෙත යොමුකරනව.
හොදින් මතක තියාගන්න අපි කමාන්ඩ්ස් රන් කරන terminal එක සමග මේ standard files තුන බද්දවෙලා තියෙනව කියල. පොඩ්ඩක් ඉන්න මම ඒ කතාව ටිකක් විස්තර කරන්නම්.
අපි UNIX / Linux shell එකේ කමාන්ඩ් එකක් රන් කරනකොට එකට input එක දෙන්නෙ අපේ terminal එකට සම්බන්ද කරල තියෙන Keyboard එකෙන් සහ කමාන්ඩ් එකේ result එක හෝ දෝෂය (Error එක) terminal screen (Display) එකට යොමුවේ. මෙහිදී Keyboard එක stdin file එක සමගද terminal screen එක stdout හා stderr කියන file දෙක සමගද සම්බන්ද වී ඇත.
තවද UNIX / Linux file system එක තුල ඕපන්කරන, සියලුම files සදහා file descriptor එකක් ලැබෙනවා. ඒ වගේම standard files සදහාද ඒවාට අවේනික වූ file descriptors තුනක් තියෙනව. මෙහිදී
stdin හි file descriptor එක 0 ද,
stdout හි file descriptor එක 1 ද,
stderr හි file descriptor එක 2 ද වේ.
අපි දැන් බලමු මොදක්කද මේ file descriptors කියන්නේ කියල. අපි file descriptor එකක් කියල කියන්නෙ 0 පටන් ගෙන ආරෝහණ ක්රමයට වැඩිවෙන අංකයකට. මෙයින් 0, 1 සහ 3 කියන අංක ටික අනිවාර්යයෙන්ම standard files ෆයිල් තුනට හිමිවෙනව. මේ විදියට file descriptor එකක් දීමෙන් UNIX /Linux කර්නල් (kernel) එකට, එක් එක් file එක හදුනා ගැනීමට පුළුවන් වෙනව. සරලව කියනවනම් kernel එක තුල file එක සහ ඊට අදාල file descriptor සහිත table එකක් තියාගන්නව. දැන් හරිනේ ද ?
මම මුලින් කිවුවනේ පෙරනිමි ලෙස stdin, stdout, stderr කියන standard files තුන පිලිවෙලින් terminal එකේ Keyboard, screen සහ නැවත screen එක සමග සම්බද වෙලා තියෙනවා කියල.
නමුත් මේවා ස්ථිර ලෙසම සම්බන්ද වී නොමැත. අපිට stdin එකට input එකක් දීමට Keyboard එක වෙනුවට සාමාන්ය (ordinary) file එකක් පවා යොදා ගැනීමට පුළුවන්. ඒවගේම stdout සහ stderr එකේ output එක terminal screen එකට යොමු නොකර වෙනත් file එකකට හෝ නැතිනම් තවත් UNIX / Linux කමාන්ඩ් එකක input එකක් විදියට බාවිතා කරන්න (redirect) පුළුවන්.
අපි මෙන්න මේ ක්රියාවලිට සිංහලෙන් කියනවා අයි ඕ රිඩිරේක්ෂන් කියල. English වලින් නම් I/O Redirection. අපිට මේවිදියට I/O Redirection කරන්න standard files තුනේ file descriptors ඉතා වැදගත් වෙනව.
අපි දැන් I/O Redirection කරන ආකාරය උදාහරණ සමගින් බලමු.
1) Input Redirection
මෙහි දී අප කරනුයේ UNIX / Linux කමාන්ඩ් එකක input එක terminal keyboard එකෙන් නොදී file එකකින් ලබාදීමයි. Input Redirection එක ආකාර දෙකකට සිදුකල හැකිය.
අප පළමු ක්රමයේදී "<" සංකේතය යොදා ගැනීමෙන් එය සිදුකරන අතර දෙවන ක්රමයේදී "<" සමග stdin හි file descriptor එක වන 0 යොදා ගැනීමත් (0<) සිදු කරයි.
ට්රයි cat < /etc/issue
ට්රයි grep "processor" < /proc/cpuinfo
ට්රයි cat 0< /etc/issue
ට්රයි grep "processor" 0< /proc/cpuinfo
ඉහත උදාහරණ කිහිපයේ අපි input එක විදියට බාවිතා කළේ files කිහිපයකි. දැන් අපි දන්නව "<" හා "0<" මගින් Input Redirection සිදුකල හැකිබව.
2) Output Redirection
සරලව පැහැදිලි කරනවනම් අප මෙහිදී සිදුකරනුයේ කමාන්ඩ් එකක output එක terminal screen එකකට යොමු නොකර file එකකට හෝ වෙනත් කමාන්ඩ් එකකට යොමු කිරීමයි. මෙහිදී හොදින් මතක තියාගන්න. කමාන්ඩ් එකක output එක වෙනත් කමාන්ඩ් එකක input එකක් විදියට බාවිතා කිරීම අපි විශේෂ නමකින් හදුන් වනවා(pipe). අපි ඒ ගැන පසුව කතා කරමු. මෙහිදී output එක file එකකට යොමු කිරීම පමණක් Output Redirection එක යටතේ මම විස්තර කරනව.
අපිට මෙයත් ආකාර දෙකකට සිදුකල හැක.
අප පළමු ක්රමයේදී ">" සංකේතය යොදා ගැනීමෙන් එය සිදුකරන අතර දෙවන ක්රමයේදී ">" සමග stdout හි file descriptor එක වන 1 යොදා ගැනීමත් (1>) සිදු කරයි.
අපි මුලින්ම පසුගිය පාඩමකදී සාදාගත් "lsmore" කියන ඩිරෙක්ටරි එකට මාරුවෙලා උදාහරණ ටික කරල බලමු .
ට්රයි cd ~/lsmore
දැන් අපි Output Redirection උදාහරන ටික පටන්ගනිමු.
ට්රයි ls -l > ~/ls_more_file_1.txt
මෙහිදී අප සිදු කරලා තියෙන්නේ ls -l කියන කමාන්ඩ් එකේ output එක terminal screen එකට යොමු නොකර කෙලින්ම එය "ls_more_file_1.txt" කියන file එකට යොමු කීරීමයි. හොදින් බලන්න ls_more_file_1.txt පිහිටලා තියෙන්නේ අපේ හෝම් (~) directory එක ඇතුලෙ. දැන් අපි ls_more_file_1.txt එකේ තියෙන්නේ මොනවද කියල බලමු. අපිට් ඒ සදහා
cat < ~/ls_more_file_1.txt කියන කමාන්ඩ් එක යොදා හඳුනා පුළුවන්.
ට්රයි cat < ~/ls_more_file_1.txt
අපිට 1> යොදා ගනිමින් ද මෙය සිදු කල හැකිය.
ට්රයි ls -l 1> ~/ls_more_file_2.txt
දැන් cat පාරක් දීල චෙක් කරලා බලමු.
ට්රයි cat < ~/ls_more_file_2.txt
අපි තව දුරටත් උදාහරණ කිහිපයක් කරමු.
අපි මුලින්නම cd ~/lsmore කරලා lsmore ඩිරෙක්ටරි එක ඇතුලට යමු. ඉන් අනතුරුව cat > stdout_example1 (හෝ cat 1> stdout_example1) කියල ටයිප් කරලා Enter කි එක press කරන්න.
දැන් අපිට මේ ටික terminal එකේ ටයිප් කරන්න පුළුවන්. අන්තිම line එකත් ටයිප් කරල Enter කි එකත් ඔබල අන්තිමට Ctrl කි එක සමග d කි එක press කරන්න (Ctrl d). මෙන්න මේ විදියට
ට්රයි cat > stdout_example1
දැන් අපිට cat ම යොදාගෙන (Input Redirection මගින්) stdout_example1 ඇතුලේ තියෙන දේවල් බලාගන්න පුළුවන්.
ට්රයි cat < stdout_example1 හෝ cat 0< stdout_example1
මේකත් ට්රයි කරලා බලන්න
අපි දැන් බලමු "Input Redirection" හා "Output Redirection" දෙකම එක විට යොදාගන්නා ආකාරය.
ට්රයි cat < stdout_example1 > stdout_example2 හෝ cat 0< stdout_example1 1> stdout_example2
අපි දැන් බලමු stdout එක විදියට දුන්න stdout_example2 එක ඇතුලෙ මොනවද තියෙන්නෙ කියල . cat පාරක් දීල බලමු.
හ්ම් දැන් අපිට තියෙනවා stdout_example1 වගේම stdout_example2 කියල අලුත් ෆයිල් එකක්.
ඉහත උදාහරණයේදී මම කරලා තියෙන දේ පොඩ්ඩක් පැහැදිලි කරන්නම්. අපි මුලින්ම මෙන්න මේ කොටස ගනිමු. "< stdout_example1" අපි මෙහිදී කරලා තියෙන්නෙ stdin එක redirect කරලා තියෙනව stdout_example1 කියන ෆයිල් එකට අපි එය කලින් හදපු file එකක්. එවිට cat කමාන්ඩ් එක input එක විදියට ගන්නෙ මෙන්න මේ stdout_example1 එක. දැන් බලමු මේ කොටස "> stdout_example2" මෙහිදී අපි කරලා තියෙන්නෙ cat හි stdout එක redirect කරල තියෙනව "stdout_example2" කියන file එකට. අන්තිමට අපිට ලැබුන stdout_example1 එකට සමාන stdout_example2 කියල file එකක්.
හොදින් මතක තියාගන්න. මෙවගේ Input සහ Output redirection දෙකම එකවිට ඇතිවිට "<" (Input Redirection) එක මුලින්ම කරන්න ඕන කියල දෙයක් නැ. අපිට ඉහත කරපු දේ මේ විදියටත් කරන්න පුළුවන්.
ට්රයි cat > stdout_example3 < stdout_example1
cat දීල චෙක් කරගමු.
ට්රයි cat < stdout_example3
දැන් අපි බලමු Appending Output Redirection කියන එක ගැන . අපි මේ සදහා >> හෝ 1>> යොදාගන්නව.
මම දෙන උදාහරණය බලල ඔයාලම තේරුම් ගන්න උත්සහ කරන්න.
ට්රයි cat < stdout_example1 > stdout_example4
ට්රයි cat < stdout_example4
නැවත වරක්
ට්රයි cat < stdout_example1 > stdout_example4
නැවත වරක් cat පාරක් දල බලමු
ට්රයි cat < stdout_example4
ඉහත අවස්ථා දෙකම එක වගේ ප්රතිපල තියෙන්නෙ.
දැන් අපි බලමු Appending Output Redirection (>>) එක බාවිතා කරල
ට්රයි cat < stdout_example1 >> stdout_example4
දැන් cat පාරක් දාල ඇතුලේ තියෙන්නෙ මොනවද කියල බලමු.
ට්රයි cat < stdout_example4
එවර අපිට ලොකු වෙනසක් පේනව.
අපි තවත් වරක් "cat < stdout_example1 >> stdout_example4" කමාන්ඩ් එක දීල බලමු.
ට්රයි cat < stdout_example1 >> stdout_example4 හෝ cat < stdout_example1 1>> stdout_example4
නැවත ට්රයි cat < stdout_example4
දැන් මෙතන වෙන දේ ඔයාලට හොදට තේරුණා නේද. >>" එක යොදනවිට file name එකට සමාන file එකක් ඩිරෙක්ටරිය ඇතුලෙ තිබුනොත් එම file එක overwrite නොකර එහි අගට අලුත් දේවල් එකතු කරනව කියල. ">" යෙදුවිට තිබුන file එක overwrite වෙනව. ">" හා ">>" යන දෙකටම අපි දෙන file name එක සහිත file එකක් නොමතිනම්. එම නමින් අලුතින් file එකක් හදාගන්න පුළුවන්.
DevOps_UNIX / Linux_8_I/O_රිඩිරෙක්ෂන්_සහ_Piping.
අපි අද කතාකරන්නේ I/O redirection සහ Piping පිලිබදවයි.
මුලින්ම අපි Standard Input, Standard Output සහ Standard Error ගැන හොද අවබෝදයක් ලබාගනිමු. මම ඔයාලට මේ දේවල් ඉතාම සරල විදියට තේරුම් කරලා දෙන්නම්. ඔයාලට තියෙන්නෙ හොදට අවබෝද කරගෙන මම කියපු කරුණු මතකයේ තබාගෙන නිතර බාවිතා කරන එක.
මෙන්න මෙහෙමයි අපේ කතාන්දරය පටන් ගන්නේ. UNIX / Linux කමාන්ඩ් එකක් execute කලට පසු, kernel එක ස්වයන්ක්රියව files 3ක් open කරනව. අපි ඒ files වලට කියනව standard files කියල. ඒ file තුන තමා
1) stdin - standard input file
2) stdout - standard output file
3) stderr - standard error file
පෙරනිමි ලෙස (By default) සියලුම UNIX / Linux කමාන්ඩ්ස් ඒවයෙ input ලබාගන්නෙ stdin (standard input) මගිනි, කමාන්ඩ්ස් වල result එක stdout (standard output) වෙත යොමුකරනව. ඒ වගේම යම්කිසි දෝෂයක් ඇතිවූ විට එය stderr (standard error) වෙත යොමුකරනව.
හොදින් මතක තියාගන්න අපි කමාන්ඩ්ස් රන් කරන terminal එක සමග මේ standard files තුන බද්දවෙලා තියෙනව කියල. පොඩ්ඩක් ඉන්න මම ඒ කතාව ටිකක් විස්තර කරන්නම්.
අපි UNIX / Linux shell එකේ කමාන්ඩ් එකක් රන් කරනකොට එකට input එක දෙන්නෙ අපේ terminal එකට සම්බන්ද කරල තියෙන Keyboard එකෙන් සහ කමාන්ඩ් එකේ result එක හෝ දෝෂය (Error එක) terminal screen (Display) එකට යොමුවේ. මෙහිදී Keyboard එක stdin file එක සමගද terminal screen එක stdout හා stderr කියන file දෙක සමගද සම්බන්ද වී ඇත.
stdin --> Keyboard
stdout --> terminal screen
stderr --> terminal screen
තවද UNIX / Linux file system එක තුල ඕපන්කරන, සියලුම files සදහා file descriptor එකක් ලැබෙනවා. ඒ වගේම standard files සදහාද ඒවාට අවේනික වූ file descriptors තුනක් තියෙනව. මෙහිදී
stdin හි file descriptor එක 0 ද,
stdout හි file descriptor එක 1 ද,
stderr හි file descriptor එක 2 ද වේ.
අපි දැන් බලමු මොදක්කද මේ file descriptors කියන්නේ කියල. අපි file descriptor එකක් කියල කියන්නෙ 0 පටන් ගෙන ආරෝහණ ක්රමයට වැඩිවෙන අංකයකට. මෙයින් 0, 1 සහ 3 කියන අංක ටික අනිවාර්යයෙන්ම standard files ෆයිල් තුනට හිමිවෙනව. මේ විදියට file descriptor එකක් දීමෙන් UNIX /Linux කර්නල් (kernel) එකට, එක් එක් file එක හදුනා ගැනීමට පුළුවන් වෙනව. සරලව කියනවනම් kernel එක තුල file එක සහ ඊට අදාල file descriptor සහිත table එකක් තියාගන්නව. දැන් හරිනේ ද ?
මම මුලින් කිවුවනේ පෙරනිමි ලෙස stdin, stdout, stderr කියන standard files තුන පිලිවෙලින් terminal එකේ Keyboard, screen සහ නැවත screen එක සමග සම්බද වෙලා තියෙනවා කියල.
නමුත් මේවා ස්ථිර ලෙසම සම්බන්ද වී නොමැත. අපිට stdin එකට input එකක් දීමට Keyboard එක වෙනුවට සාමාන්ය (ordinary) file එකක් පවා යොදා ගැනීමට පුළුවන්. ඒවගේම stdout සහ stderr එකේ output එක terminal screen එකට යොමු නොකර වෙනත් file එකකට හෝ නැතිනම් තවත් UNIX / Linux කමාන්ඩ් එකක input එකක් විදියට බාවිතා කරන්න (redirect) පුළුවන්.
අපි මෙන්න මේ ක්රියාවලිට සිංහලෙන් කියනවා අයි ඕ රිඩිරේක්ෂන් කියල. English වලින් නම් I/O Redirection. අපිට මේවිදියට I/O Redirection කරන්න standard files තුනේ file descriptors ඉතා වැදගත් වෙනව.
stdin හි file descriptor එක 0
stdout හි file descriptor එක 1
stderr හි file descriptor එක 2
අපි දැන් I/O Redirection කරන ආකාරය උදාහරණ සමගින් බලමු.
1) Input Redirection
මෙහි දී අප කරනුයේ UNIX / Linux කමාන්ඩ් එකක input එක terminal keyboard එකෙන් නොදී file එකකින් ලබාදීමයි. Input Redirection එක ආකාර දෙකකට සිදුකල හැකිය.
අප පළමු ක්රමයේදී "<" සංකේතය යොදා ගැනීමෙන් එය සිදුකරන අතර දෙවන ක්රමයේදී "<" සමග stdin හි file descriptor එක වන 0 යොදා ගැනීමත් (0<) සිදු කරයි.
ට්රයි cat < /etc/issue
nix@Dell ~ $ cat < /etc/issue
Linux Mint 18.1 Serena \n \l
ට්රයි grep "processor" < /proc/cpuinfo
nix@Dell ~ $ grep "processor" < /proc/cpuinfo
processor : 0
processor : 1
processor : 2
processor : 3
ට්රයි cat 0< /etc/issue
nix@Dell ~ $ cat 0< /etc/issue
Linux Mint 18.1 Serena \n \l
ට්රයි grep "processor" 0< /proc/cpuinfo
nix@Dell ~ $ grep "processor" 0< /proc/cpuinfo
processor : 0
processor : 1
processor : 2
processor : 3
ඉහත උදාහරණ කිහිපයේ අපි input එක විදියට බාවිතා කළේ files කිහිපයකි. දැන් අපි දන්නව "<" හා "0<" මගින් Input Redirection සිදුකල හැකිබව.
2) Output Redirection
සරලව පැහැදිලි කරනවනම් අප මෙහිදී සිදුකරනුයේ කමාන්ඩ් එකක output එක terminal screen එකකට යොමු නොකර file එකකට හෝ වෙනත් කමාන්ඩ් එකකට යොමු කිරීමයි. මෙහිදී හොදින් මතක තියාගන්න. කමාන්ඩ් එකක output එක වෙනත් කමාන්ඩ් එකක input එකක් විදියට බාවිතා කිරීම අපි විශේෂ නමකින් හදුන් වනවා(pipe). අපි ඒ ගැන පසුව කතා කරමු. මෙහිදී output එක file එකකට යොමු කිරීම පමණක් Output Redirection එක යටතේ මම විස්තර කරනව.
අපිට මෙයත් ආකාර දෙකකට සිදුකල හැක.
අප පළමු ක්රමයේදී ">" සංකේතය යොදා ගැනීමෙන් එය සිදුකරන අතර දෙවන ක්රමයේදී ">" සමග stdout හි file descriptor එක වන 1 යොදා ගැනීමත් (1>) සිදු කරයි.
අපි මුලින්ම පසුගිය පාඩමකදී සාදාගත් "lsmore" කියන ඩිරෙක්ටරි එකට මාරුවෙලා උදාහරණ ටික කරල බලමු .
ට්රයි cd ~/lsmore
nix@Dell ~ $ cd ~/lsmore
nix@Dell ~/lsmore $
දැන් අපි Output Redirection උදාහරන ටික පටන්ගනිමු.
ට්රයි ls -l > ~/ls_more_file_1.txt
nix@Dell ~/lsmore $ ls -l > ~/ls_more_file_1.txt
nix@Dell ~/lsmore $
මෙහිදී අප සිදු කරලා තියෙන්නේ ls -l කියන කමාන්ඩ් එකේ output එක terminal screen එකට යොමු නොකර කෙලින්ම එය "ls_more_file_1.txt" කියන file එකට යොමු කීරීමයි. හොදින් බලන්න ls_more_file_1.txt පිහිටලා තියෙන්නේ අපේ හෝම් (~) directory එක ඇතුලෙ. දැන් අපි ls_more_file_1.txt එකේ තියෙන්නේ මොනවද කියල බලමු. අපිට් ඒ සදහා
cat < ~/ls_more_file_1.txt කියන කමාන්ඩ් එක යොදා හඳුනා පුළුවන්.
ට්රයි cat < ~/ls_more_file_1.txt
දැක්ක නේද ls -l කමාන්ඩ් එක දුන්නම කලින් screen එකේ පෙන්නන ටික දැන් file එක ඇතුලෙ තියෙනවා.nix@Dell ~/lsmore $ cat < ~/ls_more_file_1.txt
total 56
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 0
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 1
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 1dir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 2
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 2dir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 3
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 3dir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 4
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 5
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 6
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 7
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 8
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 9
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 a
-rwxr----- 1 nix nix 0 Jun 19 09:15 about
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 adir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 b
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 bdir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 c
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 cdir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 computer
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 d
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 ddir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 did
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 e
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 edir
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 fdir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 file
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 how
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 is
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 iso
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 mp3
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 my
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 names
-rw-r--r-- 1 nix nix 0 Jun 20 12:17 new007
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 see
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 songs
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 that
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 these
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 videos
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 where
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 windows
-rwxr-xr-x 1 nix nix 0 Jun 19 09:15 you
අපිට 1> යොදා ගනිමින් ද මෙය සිදු කල හැකිය.
ට්රයි ls -l 1> ~/ls_more_file_2.txt
nix@Dell ~/lsmore $ ls -l 1> ~/ls_more_file_2.txt
nix@Dell ~/lsmore $
දැන් cat පාරක් දීල චෙක් කරලා බලමු.
ට්රයි cat < ~/ls_more_file_2.txt
දැන් අපිට වැඩේ පැහැදිලියි.nix@Dell ~/lsmore $ cat < ~/ls_more_file_2.txt
total 56
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 0
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 1
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 1dir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 2
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 2dir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 3
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 3dir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 4
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 5
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 6
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 7
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 8
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 9
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 a
-rwxr----- 1 nix nix 0 Jun 19 09:15 about
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 adir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 b
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 bdir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 c
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 cdir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 computer
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 d
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 ddir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 did
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 e
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 edir
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 fdir
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 file
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 how
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 is
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 iso
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 mp3
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 my
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 names
-rw-r--r-- 1 nix nix 0 Jun 20 12:17 new007
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 see
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 songs
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 that
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 these
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 videos
-rw-r--r-- 1 nix nix 0 Jun 19 09:15 where
drwxr-xr-x 2 nix nix 4096 Jun 19 09:16 windows
-rwxr-xr-x 1 nix nix 0 Jun 19 09:15 you
අපි තව දුරටත් උදාහරණ කිහිපයක් කරමු.
අපි මුලින්නම cd ~/lsmore කරලා lsmore ඩිරෙක්ටරි එක ඇතුලට යමු. ඉන් අනතුරුව cat > stdout_example1 (හෝ cat 1> stdout_example1) කියල ටයිප් කරලා Enter කි එක press කරන්න.
දැන් අපිට මේ ටික terminal එකේ ටයිප් කරන්න පුළුවන්. අන්තිම line එකත් ටයිප් කරල Enter කි එකත් ඔබල අන්තිමට Ctrl කි එක සමග d කි එක press කරන්න (Ctrl d). මෙන්න මේ විදියට
ට්රයි cat > stdout_example1
nix@Dell ~/lsmore $ cat > stdout_example1
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
දැන් අපිට cat ම යොදාගෙන (Input Redirection මගින්) stdout_example1 ඇතුලේ තියෙන දේවල් බලාගන්න පුළුවන්.
ට්රයි cat < stdout_example1 හෝ cat 0< stdout_example1
nix@Dell ~/lsmore $ cat < stdout_example1
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
nix@Dell ~/lsmore $ cat 0< stdout_example1
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
nix@Dell ~/lsmore $
මේකත් ට්රයි කරලා බලන්න
nix@Dell ~/lsmore $ echo "Ayubowan"
Ayubowan
nix@Dell ~/lsmore $ echo "Ayubowan" 1> ayubowan.txt
nix@Dell ~/lsmore $ cat < ayubowan.txt
Ayubowan
nix@Dell ~/lsmore $
අපි දැන් බලමු "Input Redirection" හා "Output Redirection" දෙකම එක විට යොදාගන්නා ආකාරය.
ට්රයි cat < stdout_example1 > stdout_example2 හෝ cat 0< stdout_example1 1> stdout_example2
nix@Dell ~/lsmore $ cat < stdout_example1 > stdout_example2
nix@Dell ~/lsmore $
අපි දැන් බලමු stdout එක විදියට දුන්න stdout_example2 එක ඇතුලෙ මොනවද තියෙන්නෙ කියල . cat පාරක් දීල බලමු.
nix@Dell ~/lsmore $ cat < stdout_example2
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
හ්ම් දැන් අපිට තියෙනවා stdout_example1 වගේම stdout_example2 කියල අලුත් ෆයිල් එකක්.
ඉහත උදාහරණයේදී මම කරලා තියෙන දේ පොඩ්ඩක් පැහැදිලි කරන්නම්. අපි මුලින්ම මෙන්න මේ කොටස ගනිමු. "< stdout_example1" අපි මෙහිදී කරලා තියෙන්නෙ stdin එක redirect කරලා තියෙනව stdout_example1 කියන ෆයිල් එකට අපි එය කලින් හදපු file එකක්. එවිට cat කමාන්ඩ් එක input එක විදියට ගන්නෙ මෙන්න මේ stdout_example1 එක. දැන් බලමු මේ කොටස "> stdout_example2" මෙහිදී අපි කරලා තියෙන්නෙ cat හි stdout එක redirect කරල තියෙනව "stdout_example2" කියන file එකට. අන්තිමට අපිට ලැබුන stdout_example1 එකට සමාන stdout_example2 කියල file එකක්.
හොදින් මතක තියාගන්න. මෙවගේ Input සහ Output redirection දෙකම එකවිට ඇතිවිට "<" (Input Redirection) එක මුලින්ම කරන්න ඕන කියල දෙයක් නැ. අපිට ඉහත කරපු දේ මේ විදියටත් කරන්න පුළුවන්.
ට්රයි cat > stdout_example3 < stdout_example1
nix@Dell ~/lsmore $ cat > stdout_example3 < stdout_example1
nix@Dell ~/lsmore $
cat දීල චෙක් කරගමු.
ට්රයි cat < stdout_example3
nix@Dell ~/lsmore $ cat < stdout_example3
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
දැන් අපි බලමු Appending Output Redirection කියන එක ගැන . අපි මේ සදහා >> හෝ 1>> යොදාගන්නව.
මම දෙන උදාහරණය බලල ඔයාලම තේරුම් ගන්න උත්සහ කරන්න.
ට්රයි cat < stdout_example1 > stdout_example4
nix@Dell ~/lsmore $ cat < stdout_example1 > stdout_example4
nix@Dell ~/lsmore $
ට්රයි cat < stdout_example4
nix@Dell ~/lsmore $ cat < stdout_example4
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
nix@Dell ~/lsmore $
නැවත වරක්
ට්රයි cat < stdout_example1 > stdout_example4
nix@Dell ~/lsmore $ cat < stdout_example1 > stdout_example4
nix@Dell ~/lsmore $
නැවත වරක් cat පාරක් දල බලමු
ට්රයි cat < stdout_example4
nix@Dell ~/lsmore $ cat < stdout_example4
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
nix@Dell ~/lsmore $
ඉහත අවස්ථා දෙකම එක වගේ ප්රතිපල තියෙන්නෙ.
දැන් අපි බලමු Appending Output Redirection (>>) එක බාවිතා කරල
ට්රයි cat < stdout_example1 >> stdout_example4
nix@Dell ~/lsmore $ cat < stdout_example1 >> stdout_example4
nix@Dell ~/lsmore $
දැන් cat පාරක් දාල ඇතුලේ තියෙන්නෙ මොනවද කියල බලමු.
ට්රයි cat < stdout_example4
nix@Dell ~/lsmore $ cat < stdout_example4
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
nix@Dell ~/lsmore $
එවර අපිට ලොකු වෙනසක් පේනව.
අපි තවත් වරක් "cat < stdout_example1 >> stdout_example4" කමාන්ඩ් එක දීල බලමු.
ට්රයි cat < stdout_example1 >> stdout_example4 හෝ cat < stdout_example1 1>> stdout_example4
nix@Dell ~/lsmore $ cat < stdout_example1 >> stdout_example4
nix@Dell ~/lsmore $
නැවත ට්රයි cat < stdout_example4
nix@Dell ~/lsmore $ cat < stdout_example4
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
nix@Dell ~/lsmore $
දැන් මෙතන වෙන දේ ඔයාලට හොදට තේරුණා නේද. >>" එක යොදනවිට file name එකට සමාන file එකක් ඩිරෙක්ටරිය ඇතුලෙ තිබුනොත් එම file එක overwrite නොකර එහි අගට අලුත් දේවල් එකතු කරනව කියල. ">" යෙදුවිට තිබුන file එක overwrite වෙනව. ">" හා ">>" යන දෙකටම අපි දෙන file name එක සහිත file එකක් නොමතිනම්. එම නමින් අලුතින් file එකක් හදාගන්න පුළුවන්.
Last edited:




