Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: 更新shell脚本模板,规避使用eval带来的不可预期的风险 #2992

Closed
nekzhang opened this issue May 21, 2024 · 0 comments
Closed
Assignees
Labels
done 已上线到正式环境并验收通过 for test 可以在测试环境进行验收 kind/enhancement 功能改进特性

Comments

@nekzhang
Copy link
Collaborator

nekzhang commented May 21, 2024

  • 替换原对输出日期时间和PID的处理方式,去除 eval 的使用以避免在用户脚本处理逻辑中带来不可预期的影响

将下面这段脚本替换掉shell的默认脚本模板,注意!最后有3行空行,别忘了!

中文版:

#!/bin/bash


# 定义获取当前时间和PID的函数
function job_get_now
{
    echo "[`date +'%Y-%m-%d %H:%M:%S'`][PID:$$]"
}

# 在脚本开始运行时调用,打印当前的时间戳及PID
function job_start
{
    echo "$(job_get_now) job_start"
}

# 在脚本执行成功的逻辑分支处调用,打印当前的时间戳及PID
function job_success
{
    local msg="$*"
    echo "$(job_get_now) job_success: [$msg]"
    exit 0
}

# 在脚本执行失败的逻辑分支处调用,打印当前的时间戳及PID
function job_fail
{
    local msg="$*"
    echo "$(job_get_now) job_fail: [$msg]"
    exit 1
}

# 在当前脚本执行时,第一行输出当前时间和进程ID,详见上面函数:job_get_now
job_start

###### 作业平台中执行脚本成功和失败的标准只取决于脚本最后一条执行语句的返回值
###### 如果返回值为0,则认为此脚本执行成功,如果非0,则认为脚本执行失败
###### 可在此处后面开始编写您的脚本逻辑代码


英文版:

#!/bin/bash


# Define function to get current time and PID
function job_get_now
{
    echo "[`date +'%Y-%m-%d %H:%M:%S'`][PID:$$]"
}

# Call this function at the beginning of the script to print the current time and PID
function job_start
{
    echo "$(job_get_now) job_start"
}

# Call this function when the script execution succeeds to print the current time and PID with a success message
function job_success
{
    local msg="$*"
    echo "$(job_get_now) job_success: [$msg]"
    exit 0
}

# Call this function when the script execution fails to print the current time and PID with a failure message
function job_fail
{
    local msg="$*"
    echo "$(job_get_now) job_fail: [$msg]"
    exit 1
}

# Call the job_start function to print the current time and PID
job_start

# The success or failure of the script execution depends on the return value of the last executed command
# A return value of 0 indicates success, while a non-zero value indicates failure
# You can add your own script logic below this comment


@nekzhang nekzhang added kind/enhancement 功能改进特性 backlog 需求初始状态,等待产品进行评估 labels May 21, 2024
@nekzhang nekzhang changed the title perf: 更新shell脚本模板,增加对shell内置扩展规则匹配模式的优化 perf: 更新shell脚本模板,规避使用eval带来的不可预期的风险 May 22, 2024
hLinx added a commit to hLinx/bk-job that referenced this issue May 29, 2024
hLinx added a commit to hLinx/bk-job that referenced this issue May 29, 2024
hLinx added a commit to hLinx/bk-job that referenced this issue May 29, 2024
@bkjob-bot bkjob-bot added for gray 可以在灰度环境/预发布环境验收 done 已上线到正式环境并验收通过 and removed backlog 需求初始状态,等待产品进行评估 for gray 可以在灰度环境/预发布环境验收 labels Jun 7, 2024
hLinx added a commit to hLinx/bk-job that referenced this issue Jun 12, 2024
hLinx added a commit to hLinx/bk-job that referenced this issue Jun 12, 2024
hLinx added a commit to hLinx/bk-job that referenced this issue Jun 12, 2024
hLinx added a commit that referenced this issue Jun 12, 2024
perf: 更新shell脚本模板,规避使用eval带来的不可预期的风险 #2992
@bkjob-bot bkjob-bot added for gray 可以在灰度环境/预发布环境验收 and removed for gray 可以在灰度环境/预发布环境验收 labels Jun 12, 2024
@bkjob-bot bkjob-bot added the for test 可以在测试环境进行验收 label Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
done 已上线到正式环境并验收通过 for test 可以在测试环境进行验收 kind/enhancement 功能改进特性
Projects
None yet
Development

No branches or pull requests

4 participants