Hotline: 094.320.0088

Linux Standard Input Output Error

1. Khái niệm

– Ở trong hệ điều hành linux và unix các node dữ liệu đều là file, bao gồm tập tin, thư mục, các thiết bị
– Mỗi một file sẽ tương ứng với một con số gọi là File Descriptor.
– File Descriptor đặc biệt để dùng cho input và output
+ File Descriptor 0 tương ứng standard input –> dấu ” < ” hoặc dấu ” 0< ”
+ File Descriptor 1 tương ứng standard output –> dấu ” > ” hoặc dấu ” 1> ”
+ File Descriptor 2 tương ứng standard error –> dấu ” 2> ”

Linux File Descriptor

2. Thực hành các tính năng cơ bản input output error

– Thực hành sử dụng standard input và standart output

[root@vmware ~]# seq 16 > file_list
[root@vmware ~]# wc -l file_list
16 file_list

[root@vmware ~]# wc -l < file_list
16

[root@vmware ~]# wc -l << EOF
> dong 1
> dong 2
> dong 3
> EOF
3

[root@vmware ~]# cat << EOF > myscript.sh
> #!/bin/bash
> echo $PWD
> EOF

[root@vmware ~]# cat myscript.sh
#!/bin/bash
echo /root

[root@vmware ~]# cat > myscript.sh << EOF
> #!/bin/bash
> echo $PATH
> echo $PWD
> EOF

[root@vmware ~]# cat myscript.sh
#!/bin/bash
echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
echo /root

– Thực hành sử dụng standard error tương ứng dấu “2>”

[root@vmware ~]# touch 1 2> output.log
[root@vmware ~]# cat output.log
[root@vmware ~]# lenh_khong_co 2> output.log
[root@vmware ~]# cat output.log
-bash: lenh_khong_co: command not found

– Sử dụng standard output kết hợp với standard error. Ở ví dụ bên dưới sẽ redirect standard out và standard error vào file dirlist

[root@vmware ~]# mkdir Document
[root@vmware ~]# cd Document/
[root@vmware Document]# touch 1
[root@vmware Document]# touch 2
[root@vmware Document]# touch 3

[root@vmware ~]# cd
[root@vmware ~]# ls Document ABC > dirlist 2>&1

– Các bạn có thể tham khảo video hướng dẫn chi tiết ở dưới đây.

Để có thêm nhiều thông tin hữu ích các bạn có thể tham khảo nội dung về chương trình Linux tại đường link

Quản trị hệ thống mạng mã nguồn mở Linux