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 my_command_here arg1 arg2 |& less
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/nullKlasik 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.
$ ( 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
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.
ÖrnekYönlendirmeyi kaldırmak için şöyle
yaparızexec 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
ÖrnekAynı şeyi şöyle
yaparız.
my command &> out.txt
ÖrnekAynı ş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
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.