Ksh:修订间差异

来自linuxsa wiki
跳转到导航 跳转到搜索
Evan留言 | 贡献
创建页面,内容为“=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. =task 1= == add new parms== 开始有 7个 最后一个为EXTRA_PARMS_LIST=$7 那我就得 加个$7 ,EXTRA_PARMS_LIST=$8”
 
Evan留言 | 贡献
第1行: 第1行:
=intro=
=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.
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.
=rerun=
<pre>
#!/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,进程号不变(不会累积后台进程)。
</pre>


=task 1=
=task 1=
第8行: 第54行:


那我就得 加个$7 ,EXTRA_PARMS_LIST=$8
那我就得 加个$7 ,EXTRA_PARMS_LIST=$8
[[category:shell]]

2025年9月7日 (日) 16:00的版本

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.

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