DevOps_UNIX / Linux_8_I/O_රිඩිරෙක්ෂන්_සහ_Piping.

ja2k17ja

Member
May 10, 2017
185
166
0
තිස්සමහාරාම
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 තුන තමා
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
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
දැක්ක නේද ls -l කමාන්ඩ් එක දුන්නම කලින් screen එකේ පෙන්නන ටික දැන් file එක ඇතුලෙ තියෙනවා.

අපිට 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:

ja2k17ja

Member
May 10, 2017
185
166
0
තිස්සමහාරාම
3) Standard Error Redirection
අපි මේ ගැන කතා කරන්න කලින් බලමු මොනවද මේ "Error" කියල කියන්නෙ. මට එක ඔයාට පොඩි උදාහරනයක් මගින් පෙන්නලා දෙන්න පුළුවන් එතකොට කටින් කියනවට වැඩ හොදට තේරුම් ගන්න පුළුවන්. :)

මුලින්ම අපේ හෝම් ඩිරෙක්ටරිය ඇතුලෙ my_errors කියල අලුතින් ඩිරෙක්ටරියක් හදා ගනිමු. මෙන්න මේ වගේ

ට්‍රයි mkdir ~/my_errors
nix@Dell ~/lsmore $ mkdir ~/my_errors
nix@Dell ~/lsmore $

දැන් cd ~/my_errors
nix@Dell ~/lsmore $ cd ~/my_errors
nix@Dell ~/my_errors $

දැන් අපි මෙන්න මේ කමාන්ඩ් එක දීල අපි හදපු "my_errors" ඩිරෙක්ටරි එක ඇතුලෙ "windows" කියල file එකක විස්තර ls -l මගින් ගන්න බලමු.

ට්‍රයි ls -l windows
nix@Dell ~/my_errors $ ls -l windows
ls: cannot access 'windows': No such file or directory
nix@Dell ~/my_errors $
හ්ම් .. අපිට් මෙන්න මෙහෙම දෙයක් "ls: cannot access 'windows': No such file or directory" ලැබුන. අපි මෙන්න මේකට කියන්නෙ "execution error" එකක් කියල. අපි දැන් බලමු කොහොමද මේ execution error එක අපි දකින්නේ කියල. මේ Error එක ඇතිවුනේ ls ඇතුලෙ මම කලින් කියපු විදියට standard error file (stderr) එක මගින් terminal screen එකට ඇවිල්ල තමයි අපිට display වෙන්නෙ. මම ඔයාලට කියල දුන්න UNIX / Linux වල stderr එක පෙරනිමි ලෙස (By default) terminal screen එකට සම්බන්ද වෙලා තියෙනව කියල. අන්න ඒ නිසා තමයි අපිට Error එක terminal screen එකේ පෙනුනෙ.

අපි "Standard Error Redirection" වලදී සිදු කරනේ ඉහලින් විස්තර කරපු Error එක terminal screen එක වෙතට යොමු නොකර file එකකට යොමු කිරීමයි. හොදින් මතක තියාගන්න අපිට් stderr එක file එකකට පමණක් නොවේ stdout වෙතටත් redirect කරන්න පුළුවන් බව. මම එය කරන විදියත් ඔයාලට පසුව කියල දෙන්නම්.

මුලින්ම file එකකට redirect කරන ආකාරය ඉගෙන ගනිමු අපි මේ සදහා යොදා ගනුයේ "2>".
අපිට Input Redirection සහ Output Redirection සදහා ක්‍රම දෙකක් තිබුනද "Standard Error Redirection" සදහා ඇත්තේ file descriptor එක බාවිතා කරන සිදු කරන මෙම ක්‍රමය "2>" පමණි.

අපි දැන් බලමු "ls: cannot access 'windows': No such file or directory" කියන error එක file එකකට redirect කරන ආකාරය.

ට්‍රයි ls -l windows 2> this_is_my_error.txt
nix@Dell ~/my_errors $ ls -l windows 2> this_is_my_error.txt
nix@Dell ~/my_errors $

දැක්ක නේද අපිට මේපාර "ls: cannot access 'windows': No such file or directory" කියල එකක් දැකගන්න ලැබුනේ නෑ. අපි දැන් බලමු "this_is_my_error.txt" file එක ඇතුලෙ මොනවද තියෙන්නෙ කියල .

ට්‍රයි cat < this_is_my_error.txt හෝ cat this_is_my_error.txt
nix@Dell ~/my_errors $ cat < this_is_my_error.txt
ls: cannot access 'windows': No such file or directory
nix@Dell ~/my_errors $

හරි දැන් ඔය කියපු error එක අපේ "this_is_my_error.txt" file එක ඇතුලෙ ලියවිලා තියෙන්නෙ.

මෙන්න මේකත් කරලා බලන්න.
nix@Dell ~/my_errors $ ls -l windows 2>> this_is_my_error.txt
nix@Dell ~/my_errors $ cat < this_is_my_error.txt
ls: cannot access 'windows': No such file or directory
ls: cannot access 'windows': No such file or directory
nix@Dell ~/my_errors $

දැන් ඔයාලට තේරුම් ගන්න පුළුවන් "2>>" එකෙන් වෙන වැඩෙත්.

මේ කරුණත් මතක තියාගන්න අපි "2>" කරන සැම විටම standard error එකක් තිබුනත් නැතත් අලුතින් file එකක් සැදෙනවා. error එකක් නැතිවිට හිස් file එකක් සැදෙනවා.

අපිට එය මෙසේ ටෙස්ට් කරල බලන්න පුළුවන්. අපි "windows" කියල file එකක් හදමු .
ට්‍රයි touch windows
nix@Dell ~/my_errors $ touch windows
nix@Dell ~/my_errors $

දැන් ls -l windows දීල බලමු
nix@Dell ~/my_errors $ ls -l windows
-rw-r--r-- 1 nix nix 0 Jun 23 16:39 windows

අපි දැන් "this_is_my_new_error.txt" කියන file එකට stderr එක redirect කරමු.

දැනට අපි සතුව "this_is_my_new_error.txt" කියල file එකක් නෑ . මේ බලන්න
nix@Dell ~/my_errors $ ls -l this_is_my_new_error.txt
ls: cannot access 'this_is_my_new_error.txt': No such file or directory
nix@Dell ~/my_errors $

දැන් අපි "ls -l windows 2> this_is_my_new_error.txt" කමාන්ඩ් එක දෙමු.

ට්‍රයි ls -l windows 2> this_is_my_new_error.txt
nix@Dell ~/my_errors $ ls -l windows 2> this_is_my_new_error.txt
-rw-r--r-- 1 nix nix 0 Jun 23 16:39 windows
nix@Dell ~/my_errors $

මෙහිදී windows නමින් file එකක් තිබුන නිසා error එකක් ඇතිවුනේ නෑ. දැන් බලමු "this_is_my_new_error.txt" කියල file එකක් හැදිල තියෙනවද කියල

ට්‍රයි ls -l this_is_my_new_error.txt
nix@Dell ~/my_errors $ ls -l this_is_my_new_error.txt
-rw-r--r-- 1 nix nix 0 Jun 23 16:44 this_is_my_new_error.txt
nix@Dell ~/my_errors $

මේ බලන්න මම කියපු විදියට "this_is_my_new_error.txt" කියල file එකක් හැදිල තියෙනව.

දැන් බලමු "this_is_my_new_error.txt" එක ඇතුලේ මොනවද තියෙන්නේ කියල .

ට්‍රයි cat < this_is_my_new_error.txt
nix@Dell ~/my_errors $ cat < this_is_my_new_error.txt
nix@Dell ~/my_errors $

නමුත් file එක ඇතුලෙ කිසිම දෙයක් නෑ. දැන් ඔයාලට මම කියපු දේ හොදට පැහැදිලියි.

දැන් මම ඔයාලට කියල දෙන්නම් stderr එක stdout එකට redirect කරන විදිය.

මුලින්ම මෙන්න මේ කමාන්ඩ් එක දිහා බලමු "ls -l lindows > this_is_my_out" මේකෙන් කියන්නෙ "lindows" කියල file එකක් අපේ current directory යෙ තිබුනොත් ඒකෙ විස්තර stdout එක redirect වීමෙන් this_is_my_out කියන file එකේ සටහන් වෙනව කියන එකයි.
අපි ට්‍රයි කරලා බලමු.
"lindows" කියල file එකක් හදමු
nix@Dell ~/my_errors $ touch lindows
nix@Dell ~/my_errors $

stdout එක "this_is_my_out" එකට redirect කරමු
nix@Dell ~/my_errors $ ls -l lindows > this_is_my_out
nix@Dell ~/my_errors $

"this_is_my_out" කියන file එකේ තියෙන දේ බලමු.
nix@Dell ~/my_errors $ cat < this_is_my_out
-rw-r--r-- 1 nix nix 0 Jun 23 16:58 lindows
nix@Dell ~/my_errors $

ඒ file එක (this_is_my_out) ඇතුලෙ මේ ටික තියෙනව "-rw-r--r-- 1 nix nix 0 Jun 23 16:58 lindows".

දැන් අපිට ඕන error එකක් ඇතිකරන්න. අපිට ලෙහෙසියෙන්ම එක කරන්න පුළුවන්. "lindows" කියන file එක delete කිරීමෙන්.
nix@Dell ~/my_errors $ rm lindows
nix@Dell ~/my_errors $
හරි දැන් බලමු කලින් දීපු ls -l lindows > this_is_my_out කමාන්ඩ් එක නැවත දීල .

ට්‍රයි ls -l lindows > this_is_my_out
nix@Dell ~/my_errors $ ls -l lindows > this_is_my_out
ls: cannot access 'lindows': No such file or directory
nix@Dell ~/my_errors $

අපි හිතපු විදියටම error එකක් ඇතිවුණා ඒ වගේම ඒක terminal screen එකේ පෙන්නුවා. අපි බලමු මොනවද දැන් "this_is_my_out" කියන file එකේ තියෙන්නෙ කියල.
ට්‍රයි cat < this_is_my_out
nix@Dell ~/my_errors $ cat < this_is_my_out
nix@Dell ~/my_errors $

"this_is_my_out" එක හිස් file එකක්.
ඔන්න දැන් මම කියල දෙන්නම් කොහොමද අපි "ls: cannot access 'lindows': No such file or directory" කියන error එක මෙන්න මේ "this_is_my_out" කියන file එකට stdout එක (>) මගින් යොමුකරන්නෙ කියල.

ට්‍රයි ls -l lindows > this_is_my_out 2>&1
nix@Dell ~/my_errors $ ls -l lindows > this_is_my_out 2>&1
nix@Dell ~/my_errors $

දැන් ට්‍රයි cat < this_is_my_out
nix@Dell ~/my_errors $ cat < this_is_my_out
ls: cannot access 'lindows': No such file or directory
nix@Dell ~/my_errors $

දැක්ක නේද වෙලා තියෙන දේ. දැන් ls: cannot access 'lindows': No such file or directory කියන error එක stdout redirection එක මගින් file එකකට redirect වෙලා තියෙන්නෙ .

command --> stderr --> stdout --> this_is_my_out

දැන් අපි නැවතත් "lindows" කියන file එක හදමු.
ට්‍රයි touch lindows
nix@Dell ~/my_errors $ touch lindows
nix@Dell ~/my_errors $

දැන් බලමු "lindows" කියන file එක ඇතිවිට මේ ls -l lindows > this_is_my_out 2>&1 කමාන්ඩ් එකෙන් වෙනදේ.
ට්‍රයි ls -l lindows > this_is_my_out 2>&1
nix@Dell ~/my_errors $ ls -l lindows > this_is_my_out 2>&1
nix@Dell ~/my_errors $

දැන් ට්‍රයි cat < this_is_my_out
nix@Dell ~/my_errors $ cat < this_is_my_out
-rw-r--r-- 1 nix nix 0 Jun 23 17:50 lindows
nix@Dell ~/my_errors $

අපිට මෙම ක්‍රමයෙන් stdout හා stderr කියන දෙකම එක file එකකට redirect කරන්න පුළුවන් කියල ඔයාලට වැටහෙනව. shell scripts වලදී අපිට මේවගේ දේවල් හුගක් උපකාරී වෙනව.


අපි බලමු මම අලුතින් එකතු කරපු මෙන්න මේ " 2>&1" කොටස ගැන.
ඔයාල දැන් දන්නවා "2>" කියන්නේ stderr (standard error file) කියල. "&1" මගින් file descriptor 1 නියෝජනය කරනව. අපි මෙතන කරල තියෙන්නෙ stderr (standard error file) එක file descriptor හි අගය 1 වන standard file ලයට කොපි කිරීමයි. ඒකියන්නෙ stdout කියන standard output file එකට කොපි කිරීමයි.

අපිට මෙන්න මේ කාරණාවත් හුගක් වැදගත් වෙනවා. අපි කිසියම් කමාන්ඩ් එකක් රන් කරනකොට එකෙන් ලැබෙන erros ටික terminal screen එකේ display වෙන එක නතර කරගැනීමට. අපිට පුළුවන් stderr එක /dev/null එකට redirect කරන්න.

ට්‍රයි ls -l unix_rocks
nix@Dell ~/my_errors $ ls -l unix_rocks
ls: cannot access 'unix_rocks': No such file or directory
nix@Dell ~/my_errors $

දැන් "2> /dev/null" සමග ට්‍රයි ls -l unix_rocks 2> /dev/null
nix@Dell ~/my_errors $ ls -l unix_rocks 2> /dev/null
nix@Dell ~/my_errors $
එවිට "ls: cannot access 'unix_rocks': No such file or directory" කියන error එක /dev/null වලට ගිහිල්ල නැතිවෙනව.
/dev/null ගැනත් ඉදිරියේදී කතා කරනව.

මම හිතනව දැනට ඔයාලට මේ කරුණු ටික ඉදිරි වැඩ කටයුතු ටික පටන් ගන්න ප්‍රමාණවත් වේවී කියල. ටිකක් සංකීර්ණ කරුණු කිහිපයක් ඉතිරිවෙලා තියෙනව. නමුත් එක කියල දෙන්න හොදම වෙලාව දැන් නොවේ:) අපි ඊළගට pipe ගැන කතාකරල අද පාඩම අවසන් කරමු.

4) pipe
අපි pipe කියල හදුන්වන්නෙ UNIX / Linux Kernel එක ඇතුලෙ තියෙන unnamed pipes. ( මම ඔයාලට Named pipe ගැන පොඩි හැදින් වීමක් DevOps_මුලික_UNIX / Linux_කමන්ඩ්ස් _5 - රිටර්න් ටු ls http://www.elakiri.com/forum/showthread.php?t=1803283 ලිපියෙන් කියල දුන්න මතකද? එහිදී මම unnamed pipes ගැනත් වචන කිහිපයක් කතා කළා. මතකද ?)

අපි දැන් බලමු මොනවද මේ pipe කියන්නෙ කියල. ඔයාලට මතකද මම මේ පාඩමේදී කිව්වා (Output Redirection). එක කමාන්ඩ් එකක stdout එක තවත් කමාන්ඩ් එකක stdin එකට දෙන්න පුළුවන් කියා. අපි අන්න ඒ විදියට කරන එකට කියන්නෙ pipe කියල.

සරලව කියනවා නම් පළමු කමාන්ඩ් එකේ output එක දෙවන කමාන්ඩ් එකේ input එක බවට යොදා ගැනීම. අපි මේ සදහා | කියන pipe operator එක බාවිතා කරනව.
මෙය සරල උදාහරනයක් මගින් තේරුම් ගන්න පුළුවන්.

ඔයාල දැනටමත් දන්නව මෙන්න මේ "cat < /etc/passwd" කමාන්ඩ් එකේ තේරුම මොකක්ද කියල. අපිට් එමගින් /etc/passwd file එකේ අන්තර්ගත දේවල් බලාගන්න පුළුවන්. ඒ මොකද ඒ කමාන්ඩ් එකේ stdout එක terminal screen එකට සම්බන්ද වෙලා තියෙන නිසා.

අපි දැන් මේ stdout එක වෙනත් කමාන්ඩ් එකක input එකක් විදියට යොදා ගනිමු. අපි ඒ සදහා මෙන්න මේ "| " pipe operator එක බාවිතා කරනව .

ට්‍රයි cat < /etc/passwd | wc -l
nix@Dell ~/my_errors $ cat < /etc/passwd | wc -l
46
nix@Dell ~/my_errors $

මෙතන අපි කරලා තියෙන්නෙ පළවන කමාන්ඩ් එකේ stdout එක terminal screen එකට් නොදී wc -l කියන කමාන්ඩ් එකට ඉන්පුට් එකක් විදියට pipe එක තුලින් යොමු කිරීමයි. අන්තිමට wc -l හි stdout එක terminal screen එක හා සම්බන්ද බැවින් අපිට් wc -l හි output එක දැක ගන්න පුළුවන්.

අපිට අපිට අවශ්ෂනම් wc -l හි stdout එකද, තවත් කමාන්ඩ් එකක input එකකට යොමුකරන්න පුළුවන්.

ට්‍රයි cat < /etc/passwd | wc -l | wc -l
nix@Dell ~/my_errors $ cat < /etc/passwd | wc -l | wc -l
1
nix@Dell ~/my_errors $

** wc -l කමාන්ඩ් එකෙන් අපිට file එකක තිබෙන new line ගණන (\n) ගණන් කර ගැනීමට පුළුවන්. මෙය බොහෝවිට file එකේ තිබෙන text lines ගන්නට සමානයි. ඇතැම් අවස්ථාවලදී අන්තිම line එකේ "\n" අඩංගු නොවුවිට එය ගණනය කිරීමට ඇතුලත් නොවේ. මෙය පැහැදිලි කිරීම පසුවට කල් තබමි :)

ඉදිරියේදී pipe ගැන තවත් උදාහරණ කිහිපක් අනෙක් පාඩම් තුලදී ඔයාලට ඉදිරිපත් කරන්නම්.

අවසාන වශයෙන් කියන්න තියෙනෙ UNIX / Linux pipe වලට ඉතා විශාල ප්‍රමාණයේ data සමග සෙල්ලම් කිරීමට පුළුවන.
මම මේ pipe උපයෝගී කරගෙන විදේශීය රුපවාහිනී අයතනයකට HD live streaming 1080p සිස්ටම් එකක් developed කරා. එහිදී SDI Video source එකෙන් එන 1.485 Gbit/s data ප්‍රමාණයක් live encoder එකට යොමු කලේ pipe එකකින්. දැන් ඔයාලට හිතාගන්න පුලුවන්නේද UNIX / Linux වල තියෙන පොඩි පොඩි දේවල් අපි හිතනවටවඩා හුගක් බලගතුයි කියල. මේ වගේ දේවල් කරන ආකාරයත් ඔයාලට ඉදිරියේදී කියල දෙන්න තමයි මගේ අදහස.

අද ටිකක් විතර මහන්සියි . මට මේක ඊයේ ඉවරකර ගන්න බැරිවුණා. ගෙදර පොඩි අයගෙ school වැඩ ටිකක් තිබුන.
අලුත් පාඩමකින් හමුවෙමු.

ස්තුතියි!

DevOps_UNIX / Linux_9_UNIX / Linux Presesses ගැන දැන ගනිමු පළමු කොටස
 
Last edited:
  • Like
Reactions: Schrodinger Cat

BadBlueGenius

Well-known member
  • Jun 11, 2014
    5,545
    6,530
    113
    DevOPsකියන වචනෙ විස්තර කරන්න පුලුවන්ද. මෙක ජොබ් ටඉටල් එකක්ඩ. නත්තම් it related ජොබ් කරන හමොම ඔය මෙ කියල ඩෙන දෙවල් ගැන දනගෙන ඉන්න ඔනද ?????
     

    ja2k17ja

    Member
    May 10, 2017
    185
    166
    0
    තිස්සමහාරාම
    DevOPsකියන වචනෙ විස්තර කරන්න පුලුවන්ද. මෙක ජොබ් ටඉටල් එකක්ඩ. නත්තම් it related ජොබ් කරන හමොම ඔය මෙ කියල ඩෙන දෙවල් ගැන දනගෙන ඉන්න ඔනද ?????
    මේ දේවල් IT කරන හැමෝම දැනගන්න ඕන නෑ. මම මේ දැනට කියල තියෙන දේවල් ටික Sys Admin ල අනිව දැනගත යුතුයි.

    අපිට DevOps ගැන සරලව මෙහෙම කියන්න පුළුවන්. අපි හිතමු සොෆ්ට්වෙය ඩිවෙලොප් කරන ආයතනයක
    DevOps කියන්නෙ සිස් ඇඩ්මින් ගෙන් පටන්ගෙන අන්තිමට සොෆ්ට්වෙය එක ප්‍රොඩක්ෂන් එකට යනකම්ම . Project Manager ට , developers ලට. උදව් කරන කෙනෙක්. DevOps ට ගොඩක් skills තියෙනව. නමුත් එයා හදන software එකේ coding වලට එකතු වෙන්නෑ . DevOps එය සමග වැඩකරන developers ලට ගැටළුවක් නොමැතිව වැඩ ටික කරගෙන යන්න වටපිටාව හදල දෙනව.

    මේක හරියට Sys Admin කෙනෙකුට Add-ons දාල ගත්ත වගේ. core එක වෙන්නෙ Sys Admin :)
     

    BadBlueGenius

    Well-known member
  • Jun 11, 2014
    5,545
    6,530
    113
    මේ දේවල් IT කරන හැමෝම දැනගන්න ඕන නෑ. මම මේ දැනට කියල තියෙන දේවල් ටික Sys Admin ල අනිව දැනගත යුතුයි.

    අපිට DevOps ගැන සරලව මෙහෙම කියන්න පුළුවන්. අපි හිතමු සොෆ්ට්වෙය ඩිවෙලොප් කරන ආයතනයක
    DevOps කියන්නෙ සිස් ඇඩ්මින් ගෙන් පටන්ගෙන අන්තිමට සොෆ්ට්වෙය එක ප්‍රොඩක්ෂන් එකට යනකම්ම . Project Manager ට , developers ලට. උදව් කරන කෙනෙක්. DevOps ට ගොඩක් skills තියෙනව. නමුත් එයා හදන software එකේ coding වලට එකතු වෙන්නෑ . DevOps එය සමග වැඩකරන developers ලට ගැටළුවක් නොමැතිව වැඩ ටික කරගෙන යන්න වටපිටාව හදල දෙනව.

    මේක හරියට Sys Admin කෙනෙකුට Add-ons දාල ගත්ත වගේ. core එක වෙන්නෙ Sys Admin :)

    dan pahadili. thanks reply ekata.
    kalin dipu nisa aye rep denna bahalu. :cool::cool:
     

    ruchira55

    Well-known member
  • Mar 23, 2012
    39,665
    40,246
    113
    උබ කරන්නෙ ගොඩක් හොඳ දෙයක්. සබ්ජෙක්ට් එකේ ඉන්න අයට ඉගෙන ගන්න ලේසි වෙන්නත් එක්ක හදපන්කො බන් පොඩි බ්ලොග් කෑල්ලක්. හදලා මේවා ඒකෙත් දාපන්.

    කලින් රෙප් දුන්න නිසා දැන් දෙන්න බෑ.
     

    ja2k17ja

    Member
    May 10, 2017
    185
    166
    0
    තිස්සමහාරාම
    ඔයාගේ අදහසට ස්තුතියි!

    උබ කරන්නෙ ගොඩක් හොඳ දෙයක්. සබ්ජෙක්ට් එකේ ඉන්න අයට ඉගෙන ගන්න ලේසි වෙන්නත් එක්ක හදපන්කො බන් පොඩි බ්ලොග් කෑල්ලක්. හදලා මේවා ඒකෙත් දාපන්.

    කලින් රෙප් දුන්න නිසා දැන් දෙන්න බෑ.
    ඔයාගේ අදහසට ස්තුතියි!

    මම හිතාගෙන ඉන්නේ මුලික UNIX / Linux කරුණු ටික ලියල අවසන් වුනාට පස්සෙ බ්ලොග් එකක දාන්න. දැනට මම ට්‍රයි කරන්නෙ Shell Script පාඩම් ටික පටන්ගන්න backgound එක හදාගන්න.
     

    _nimal

    Junior member
  • Mar 25, 2011
    94
    72
    18
    San José, Costa Rica
    දිගටම මෙ සත්කාරය කරන්න

    ඉදල හිටල මම මේ පැත්තෙ ආවට. ලොග් වෙලා යමක් නොකිය ගියොත් ටිකක් හිතට මදි වගේ දැනුණ....

    සහොදරය, ඔබ ජොබ් ගැන හොදට තෙරුම් ගන්න පුලුවන් විදියට කියල තියෙනව. වටිනා පොස්ට් එකක්. ඔබට OS ගැන හොද අවබොදයක් තියෙනව කියල මනාව පෙන්නුම් කරල තියෙනව. දිගටම මෙ සත්කාරය කරන්න ශක්තිය ලැබෙන්න කියල ප්‍රර්ථනා කරනව.

    edit:
    ජොබ් විතරක් නොවේ. සියල්ලම වගෙ කරන විදිය පවා උදාහරන සහිතව පෙන්නල තියෙනව. මෙ පොස්ට් එකත් එලකිරි වගේ! :P
     
    Last edited:
    May 7, 2016
    12,338
    1,004
    0
    තොප්පිගල
    11.gif
    11.gif