我试图加强我的通知脚本。脚本的工作方式是将其放在一个长时间运行的shell命令后面,然后在长时间运行的脚本完成后调用各种通知。
例如:
sleep 100; my_notify
获取长时间运行脚本的退出码会很不错。问题在于调用 my_notify
会创建一个新进程,该进程无法访问 $?
变量。
比较一下:
~ $: ls nonexisting_file; echo "exit code: $?"; echo "PPID: $PPID"
ls: nonexisting_file: No such file or directory
exit code: 1
PPID: 6203
vs.
->对比。
~ $: ls nonexisting_file; my_notify
ls: nonexisting_file: No such file or directory
exit code: 0
PPID: 6205
my_notify
脚本包含以下内容:
#!/bin/sh
echo "exit code: $?"
echo "PPID: $PPID"
我想找一种方法,在不过多更改命令结构的情况下获取先前命令的退出代码。我知道如果将其更改为更像time
的方式,例如my_notify longrunning_command...
,我的问题将得到解决,但我实际上喜欢将其附加在命令末尾,并担心这种第二种解决方案会带来复杂性。这是否可行,还是与shell的工作方式根本不兼容?
我的shell是Z shell(
zsh
),但我也希望它能与Bash一起使用。