redirection etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
redirection etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

30 Ekim 2021 Cumartesi

bash yönlendirme - redirection - Standard Output, Standard Error Birleştirme

Standard Output,  Standard Error Birleştirme Nedir
Açıklaması şöyle. Bu komut aslında bayağı karışık bir hal alabiliyor.
When you redirect something to &number, you are not opening a new file at all; you're reusing an already open file along with whatever mode it was opened.

The numbers refer to "open file" handles (file descriptors). So there is no technical difference between how >& and >>& (and indeed <&) would work – they all just mean "clone the existing file descriptor using dup()".

That is, 2>&1 indicates that file descriptor #1 (which you previously opened for appending using >>logfile) is cloned into number #2. And yes, 2<&1 works identically.
Bu yönlendirme POSIX uyumlu. Açıklaması şöyle.
So >out.txt 2>&1 is a POSIX-compliant way to redirect both standard output and standard error to out.txt.
Örnek
&>
veya
>&
şeklinde kullanılabilir. İlk kullanım tercih edilmeli. Şu kullanım ile aynıdır
>word 2>&1
1 - stdout
2 - stderr
akımlarıdır.

Bash'e özel
Bash'e özel şöyle yaparız
my_command_here arg1 arg2 |& less
Açıklaması şöyle
Note that |& is a Bashism. It does not work with /bin/sh (normally). If you want this in a portable shell script, use 2>&1 and a normal pipe instead.


Kullanım Örneleri
Klasik kullanımı daha kolay anlamak için birleştirme işlemlerini soldan başlayarak okumak lazım

Örnek
Klasik kullanım için şöyle yaparız. out.txt yeniden yaratılır ve hem stderr hem de stdout out.txt dosyasına yönlendirilir. Burada stderr çıktısı stdout'a yönlendiriliyor
my command > out.txt 2>&1  
Örnek - dev/null
Klasik kullanım için şöyle yaparız. Örnekte ise hem stdout hem de stderr /dev/null'a gönderiliyor. Böylece tüm çıktı /dev/null'a gönderilir. 
my command > /dev/null 2>&1  
Windows'ta /dev/null yerine sadece nul kullanılır.
your_dos_command 2> nul
Örnek - Biraz Karışık
Bu karışık kullanım C veya C++ dillerindeki pointer işlemlerine benziyor.  Eğer şöyle yaparsak, soldan okumaya başlarsak stderr önce stdout'a yönlendirilir, daha sonra stdout /dev/null'a gönderilir. Ancak stderr'i değiştirmedik. Yani halen stderr çıktısını ekranda görürüz.
command 2>&1 1>/dev/null
Benzer bir örnek şöyle
$ ( echo "this is stdout"; echo "this is stderr" >&2 ) 1>foo 2>&1 1>bar
$ cat foo
this is stderr
$ cat bar
this is stdout
Açıklaması şöyle
We can see that 2>&1 sends stderr to the "foo" file that stdout was redirected to, but when we redirect stdout to "bar" we don't alter stderr's destination. 

Örnek
Yönlendirmeyi kaldırmak için şöyle yaparız
exec 3>&-
Örnek
Şöyle yaparız. Burada stdout açıkça out.txt dosyasına yönlendiriliyor. stderr ise stdout'a yönlendiriliyor. Yani her şey out.txt dosyasına yazılıyor
my command 1 > out.txt 2>&1  
Örnek
Aynı şeyi şöyle yaparız.
my command &> out.txt
Örnek
Aynı şeyi şöyle yaparız.
my command 1>>out.txt 2>>out.txt
Örnek
Sadece bazı komutların çıktısını stdout'a diğerlerini /dev/null'a yönlendirmek için şöyle yaparız. exec bir bash built-in komutu
exec 3>&1 &>/dev/null
some_command
another_command
command_you_want_to_see >&3
command3
Açıklaması şöyle
You can use the exec command to redirect everything for the rest of the script.

You can use 3>&1 to save the old stdout stream on FD 3, so you can redirect output to that if you want to see the output.

10 Eylül 2020 Perşembe

bash yönlendirme - redirection

Giriş
bash redirection kelimesini bash yönlendirme olarak tercüme ettim. Yönlendirmese sayı ve operatör arasında boşluk olmamalı. Boşluk olursa ne olacağını gösteren bir örnek şöyle
[root@divinity test]# echo 0 > file.txt
[root@divinity test]# cat file.txt
0
[root@divinity test]# echo 0> file.txt

[root@divinity test]# cat file.txt

1. > yönlendirme
Açıklaması şöyle. Dosyayı truncate eder yani yeni baştan yaratır.
truncates the file to zero size, then writes;
Örnek
Açıklaması şöyle.
redirections are processed before the command is actually executed ...
Kural şöyle.
Never redirect to the file you are working on!
Elimizde şöyle bir kod olsun. Çıktı olarak boş dosya alırız çünkü önce file.txt boşaltılır. Daha sonra cat çalıştırılır. Dolayısıyla elimize boş dosya geçer.
cat file.txt | sed 's/a/1/' > file.txt
Örnek - Bir Dosyadan Okuyup Diğerine Yazma
Şöyle yaparız
somecommand < file1 > file2
Örnek - named pipe
Elimizde bir named pipe olsun. exec f'ten yani fifo'dan okur
$ mkfifo f; exec <f
Named pipe'a yazmak için şöyle yaparız. echo kapanırken named pipe'ı da kapatır.
$ echo ls > f
Aynı şeyi şöyle yaparsak
$ exec 3>f
$ echo ls >&3
2. >> yönlendirme - Appending Yönlendirme
Açıklaması şöyle. Dosyanın sonuna ekler.
doesn't truncate, it writes (appends) to the end of the file.
Örnek
Dosyanın başına eklemek için şöyle yapabiliriz. Önce date komutu çalışır, daha sonra dosya cat'lenir. Her ikisinin çıktısı file.new dosyasına yazılır. Daha sonra file.new file olarak taşınır.
{ date; cat file; } >file.new && mv file.new file
Çıktı olarak şunu alırız
Mon Aug 31 00:40:30 UTC 2020
Mon Aug 31 00:40:29 UTC 2020
Mon Aug 31 00:40:28 UTC 2020
Mon Aug 31 00:40:27 UTC 2020
3. &> yönlendirme - Standard Output,  Standard Error Birleştirme

4. &>> yönlendirme - Appending Standard Output  Standard Error Birleştirme
Bu sefer dosyaya append edilir.
Örnek
Şöyle yaparız. logfile'a sadece ekleme yapılır yeniden yaratılmaz ve hem stderr hem de stdout out.txt dosyasına yönlendirilir.
"command >> logfile 2>&1"

5. |> yönlendirme
noclobber (dosyaları ezmeme, overwrite yapmama) etkin olsa bile dosyanın üzerine yazar. Dosya ezmeme noclobber -C seçeneği ile etkinleştirilir.

-C seçeneği etkin iken
ls > out.txt
yaparsak out.txt dosyası mevcut ise değiştirilmez.

-C seçeneği etkin iken dosyaları ezmek için şöyle yaparız.
ls |> out.txt
6. <& yönlendirme
İki  tane input stream'i birleştirir. Burada bir örnek var.

Örnek
Açıklaması şöyle
if you really want to redirect standard input, while allowing writes, you can do so with the <> operator:
Şöyle yaparız
cat 0<> file.txt