Linux 如何从函数调用值到shell脚本

Linux 如何从函数调用值到shell脚本,linux,shell,unix,scripting,command,Linux,Shell,Unix,Scripting,Command,对于我的需求,我想在这个函数之外使用下面的时间变量exectime,并想从shell脚本调用它。 谁能帮我从shell脚本中调用它 time_check(){./u01/scripts/${1}.env导出 create_env_log=${logs}/create_env_log{dts}.log echo'STime:' 日期+%s>${create_env_log}“$@”echo'ETime:'日期+%s>> ${create_env_log} export st_time=grep-i

对于我的需求,我想在这个函数之外使用下面的时间变量exectime,并想从shell脚本调用它。 谁能帮我从shell脚本中调用它

time_check(){./u01/scripts/${1}.env导出 create_env_log=${logs}/create_env_log{dts}.log echo'STime:' 日期+%s>${create_env_log}“$@”echo'ETime:'日期+%s>> ${create_env_log}

export st_time=grep-i STime${create_env_log}cut-d':'-f2
export en_time=grep-i ETime${create_env_log}| cut-d':'-f2
export exectime=$((en_time-st_time))
exectime=$((totextime+exectime))
}


要在shell中将函数作为命令调用,需要对包含它的文件进行源代码(类似于在该函数中对
/u01/scripts/${1}.env
进行源代码)

带有
bash
的示例:

function time_check() {
    . /u01/scripts/${1}.env
    export create_env_log=${logs}/create_env_log_${dts}.log
    echo 'STime:' date +%s > ${create_env_log} "$@"
    echo 'ETime:' date +%s >> ${create_env_log}

    export st_time= grep -i STime ${create_env_log} |cut -d':' -f2
    export en_time= grep -i ETime ${create_env_log} |cut -d':' -f2
    export exectime=$((en_time-st_time))
    exectime=$((Totexectime+exectime))
}
然后,您的
.bashrc
文件将包含以下行,因此每次启动shell时都会获取源代码:

. /path/to/function_file

如果需要,此文件可以包含多个函数。因此,您不需要创建多个文件来生成多个函数。

您的代码需要完全重新格式化;我们没办法猜出断线应该在哪里。请回答你的问题;代码应缩进至少四个空格。你也可以在这篇文章中看到同样的内容。请看一下你的问题是如何格式化的,并尽量使这个问题更加清晰