Çoğaltma burada gerçekten önemli bir parçadır.
Yeniden yönlendirmeden önce dosya tanımlayıcılarının nereye gideceğini görelim. Bu normalde akım terminalidir, örneğin:
STDOUT ---> /dev/pts/1
STDERR ---> /dev/pts/1
Şimdi, ls -lyönlendirme olmadan ararsak, çıkış ve hata mesajları aşağıdaki terminalime gider /dev/pts/1.
İlk STDOUTolarak bir dosyaya ( ls -l > dirlist) yönlendirirsek, şöyle görünür:
STDOUT ---> /home/bon/dirlist
STDERR ---> /dev/pts/1
Biz ne zaman sonra yönlendirmek STDERRa yinelenen bir STDOUTbireyin dosya tanımlayıcı ( ls -l > dirlist 2>&1), STDERRbir kopyası gider /home/bon/dirlist:
STDOUT ---> /home/bon/dirlist
STDERR ---> /home/bon/dirlist
Biz istiyorsanız ilk yönlendirmek STDERRkopyası için STDOUTbireyin dosya tanımlayıcı ( ls -l 2>&1):
STDOUT ---> /dev/pts/1
STDERR ---> /dev/pts/1
ve sonra STDOUT bir dosyaya ( ls -l 2>&1 > dirlist), şunu elde ederiz:
STDOUT ---> /home/bon/dirlist
STDERR ---> /dev/pts/1
Burada STDERRhala terminale gidiyor.
Gördüğünüz gibi, man sayfasındaki sipariş doğrudur.
Yeniden Yönlendirmeyi Test Etme
Şimdi, bunu kendiniz test edebilirsiniz. Kullanarak ls -l /proc/$$/fd/, STDOUT(fd 1 ile) ve STDERR(fd 2 ile), geçerli işlemin nereye gittiğini görürsünüz :
$ ls -l /proc/$$/fd/
total 0
lrwx------ 1 bon bon 64 Jul 24 18:19 0 -> /dev/pts/1
lrwx------ 1 bon bon 64 Jul 24 18:19 1 -> /dev/pts/1
lrwx------ 1 bon bon 64 Jul 24 07:41 2 -> /dev/pts/1
lrwx------ 1 bon bon 64 Jul 24 18:19 255 -> /dev/pts/1
Dosya tanımlayıcılarınızın nereye yönlendirildiğini gösteren küçük bir kabuk betiği oluşturalım. Bu şekilde, arama sırasında her zaman ls, arama kabuğundan herhangi bir yönlendirme de dahil olmak üzere durumu alırız .
$ cat > lookfd.sh
#!/bin/sh
ls -l /proc/$$/fd/
^D
$ chmod +x lookfd.sh
(İle CtrlDbir dosya sonu gönderirsiniz ve böylece catkomutu okumayı durdurursunuz STDIN.)
Şimdi, bu komut dosyasını farklı yönlendirme kombinasyonlarıyla çağırın:
$ ./lookfd.sh
total 0
lrwx------ 1 bon bon 64 Jul 24 19:08 0 -> /dev/pts/1
lrwx------ 1 bon bon 64 Jul 24 19:08 1 -> /dev/pts/1
lrwx------ 1 bon bon 64 Jul 24 19:08 2 -> /dev/pts/1
lr-x------ 1 bon bon 64 Jul 24 19:08 255 -> /home/bon/lookfd.sh
$ ./lookfd.sh > foo.out
$ cat foo.out
total 0
lrwx------ 1 bon bon 64 Jul 24 19:10 0 -> /dev/pts/1
l-wx------ 1 bon bon 64 Jul 24 19:10 1 -> /home/bon/foo.out
lrwx------ 1 bon bon 64 Jul 24 19:10 2 -> /dev/pts/1
lr-x------ 1 bon bon 64 Jul 24 19:10 255 -> /home/bon/lookfd.sh
$ ./lookfd.sh 2>&1 > foo.out
$ cat foo.out
total 0
lrwx------ 1 bon bon 64 Jul 24 19:10 0 -> /dev/pts/1
l-wx------ 1 bon bon 64 Jul 24 19:10 1 -> /home/bon/foo.out
lrwx------ 1 bon bon 64 Jul 24 19:10 2 -> /dev/pts/1
lr-x------ 1 bon bon 64 Jul 24 19:10 255 -> /home/bon/lookfd.sh
$ ./lookfd.sh > foo.out 2>&1
$ cat foo.out
total 0
lrwx------ 1 bon bon 64 Jul 24 19:11 0 -> /dev/pts/1
l-wx------ 1 bon bon 64 Jul 24 19:11 1 -> /home/bon/foo.out
l-wx------ 1 bon bon 64 Jul 24 19:11 2 -> /home/bon/foo.out
lr-x------ 1 bon bon 64 Jul 24 19:11 255 -> /home/bon/lookfd.sh
1 (for STDOUT) ve 2 (for STDERR) dosya tanımlayıcılarının değiştiğini görebilirsiniz. Eğlenmek STDINiçin, sonucu yeniden yönlendirebilir ve görebilirsiniz:
$ ./lookfd.sh < /dev/zero
total 0
lr-x------ 1 bon bon 64 Jul 24 19:18 0 -> /dev/zero
lrwx------ 1 bon bon 64 Jul 24 19:18 1 -> /dev/pts/1
lrwx------ 1 bon bon 64 Jul 24 19:18 2 -> /dev/pts/1
lr-x------ 1 bon bon 64 Jul 24 19:18 255 -> /home/bon/lookfd.sh
(Soru okuyucuya bırakıldı: Dosya tanımlayıcı 255 nerede gösteriyor? ;-))