Ksh
intro
The Korn Shell (ksh) is a Unix shell developed by David Korn in the early 1980s at Bell Labs. It was designed to combine the features of the Bourne Shell (sh) and the C Shell (csh), offering enhanced functionality for both interactive use and scripting. It is known for its performance efficiency and advanced scripting capabilities.
catch log
➜ ksh ksh catuplog
[23:59:25 04/09/2025] Batch job id:RTPDS0231C
[23:59:36 04/09/2025] Result:COMPLETED
➜ ksh cat catuplog
#!/usr/bin/env ksh
awk '
/Batch job id:/ { job=$0 }
/Result:COMPLETED/ { res=$0 }
END { print job; print res }
' logfile
rerun
#!/usr/bin/ksh
# myjob.ksh
# 第一次启动时若未传入计数器,设成 0
(( TRY=${1:-0} ))
# ====== 真正要做的事情 ======
# 这里用 grep 一个不存在的串来模拟失败
grep "never_exist" /etc/passwd >/dev/null
RC=$?
# =============================
if (( RC == 0 )); then
print "Job OK"
exit 0
else
(( TRY++ ))
if (( TRY < 2 )); then
print "retry $TRY ..."
exec $0 $TRY # 关键:把计数器传给自己,重新执行
else
print "Failed after 2 tries"
exit 1
fi
fi
判断自己存在不存在的 好像 frg 用过 找一下 blog
#run and test on kali
shell chmod +x myjob.ksh
➜ shell ./myjob.ksh
retry 1 ...
retry 2 ...
Failed after 2 tries
第一次手动执行时不给参数,脚本内部把 ${1:-0} 当成 0。
每次失败都会 exec $0 $TRY,进程号不变(不会累积后台进程)。
task 1
add new parms
开始有 7个 最后一个为EXTRA_PARMS_LIST=$7
那我就得 加个$7 ,EXTRA_PARMS_LIST=$8