Vâng, câu hỏi không phải là tái rất rõ ràng: những gì tác giả ban đầu muốn sau khi chặn các lỗi trong kịch bản có nguồn gốc, tuy nhiên, như là một điểm khởi đầu cho các giải pháp sau đây sẽ đủ:
Bạn có thể thiết lập một bẫy trên ERR và xử lý lỗi bên trong tập lệnh có nguồn gốc ở đó. Dưới đây là hai kịch bản: một kịch bản có tập lệnh có nguồn gốc sử dụng "set -e" và tập lệnh khác có tập lệnh nguồn gốc KHÔNG sử dụng "set -e".
Các kịch bản chính đang kêu gọi các kịch bản phổ thông với định nghĩa "thiết lập -e" và bắt một lỗi: đánh bắt
[galaxy => ~]$ cat primary.sh
#!/bin/sh
set -e
echo 'Primary script'
trap 'echo "Got an error from the secondary script"' ERR
source secondary.sh
trap - ERR
echo 'Primary script exiting'
[galaxy => ~]$ cat secondary.sh
#!/bin/sh
echo 'Secondary script'
set -e
echo 'Secondary script generating an error'
false
echo 'Secondary script - should not be reached'
[galaxy => ~]$ ./primary.sh
Primary script
Secondary script
Secondary script generating an error
Got an error from the secondary script
[galaxy => ~]$
Các kịch bản chính đang kêu gọi các kịch bản thứ mà không cần "đặt -e" và một lỗi:
[galaxy => ~]$ cat primary.sh
#!/bin/sh
set -e
echo 'Primary script'
trap 'echo "Got an error from the secondary script"' ERR
source secondary.sh
trap - ERR
echo 'Primary script exiting'
[galaxy => ~]$ cat secondary.sh
#!/bin/sh
echo 'Secondary script'
echo 'Secondary script generating an error'
false
echo 'Secondary script - should not be reached if sourced by primary.sh'
[galaxy => ~]$ ./primary.sh
Primary script
Secondary script
Secondary script generating an error
Got an error from the secondary script
[galaxy => ~]$
Như một phần thưởng: chặn một lỗi trong kịch bản có nguồn gốc và tiếp tục:
[galaxy => ~]$ cat primary.sh
#!/bin/sh
echo 'Primary script'
i=0
while [ $i = 0 ]; do
i=1
trap 'echo "Got an error from the secondary script"; break' ERR
source secondary.sh
done
trap - ERR
echo 'Primary script exiting'
[galaxy => ~]$ cat secondary.sh
#!/bin/sh
echo 'Secondary script'
echo 'Secondary script generating an error'
false
echo 'Secondary script - should not be reached if sourced by primary.sh'
[galaxy => ~]$ ./primary.sh
Primary script
Secondary script
Secondary script generating an error
Got an error from the secondary script
Primary script exiting
[galaxy => ~]$
@BlueMoon Bài đăng của OP giải thích tại sao điều đó không hiệu quả. –
Bạn không thể đặt bẫy thoát trong môi trường gọi điện và xử lý nó ở đó? –