如何从函数到Shell脚本调用值

时间:2018-08-12 08:56:08

标签: 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}“ $ @”回显'ETime:'日期+%s >>   $ {create_env_log}

     

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

1 个答案:

答案 0 :(得分:0)

要在shell中作为命令调用函数,您需要获取包含该文件的文件(类似于在该函数中获取========== Preparing Containers ========== Getting Docker containers ready... C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -NoProfile -WindowStyle Hidden -ExecutionPolicy RemoteSigned -File "C:\Users\Admin\AppData\Local\Temp\GetVsDbg.ps1" -Version vs2017u5 -RuntimeID debian.8-x64 -InstallPath "C:\Users\Admin\vsdbg\vs2017u5" Info: Using vsdbg version '15.7.20425.2' Info: Using Runtime ID 'linux-x64' Info: Latest version of VsDbg is present. Skipping downloads docker-compose -f "C:\Users\Admin\source\repos\docker-compose.yml" -f "C:\Users\Admin\source\repos\docker-compose.override.yml" -f "C:\Users\Admin\source\repos\obj\Docker\docker-compose.vs.debug.g.yml" -p dockercompose16069050365708511483 --no-ansi config The DOCKER_REGISTRY variable is not set. Defaulting to a blank string. services: test100: build: context: C:\Users\Admin\source\repos dockerfile: test100/Dockerfile target: base entrypoint: tail -f /dev/null environment: ASPNETCORE_ENVIRONMENT: Development DOTNET_USE_POLLING_FILE_WATCHER: '1' NUGET_FALLBACK_PACKAGES: /root/.nuget/fallbackpackages image: test100:dev labels: com.microsoft.visualstudio.debuggee.arguments: ' --additionalProbingPath /root/.nuget/packages --additionalProbingPath /root/.nuget/fallbackpackages bin/Debug/netcoreapp2.0/test100.dll' com.microsoft.visualstudio.debuggee.killprogram: /bin/bash -c "if PID=$$(pidof -x dotnet); then kill $$PID; fi" com.microsoft.visualstudio.debuggee.program: dotnet com.microsoft.visualstudio.debuggee.workingdirectory: /app ports: - target: 80 volumes: - /c/Users/Admin/source/repos/test100:/app:rw - /c/Users/Admin/vsdbg/vs2017u5:/remote_debugger:ro - /c/Program Files/dotnet/sdk/NuGetFallbackFolder:/root/.nuget/fallbackpackages:ro - /c/Users/Admin/.nuget/packages:/root/.nuget/packages:ro test101: build: context: C:\Users\Admin\source\repos dockerfile: ../test101/Dockerfile target: base entrypoint: tail -f /dev/null environment: ASPNETCORE_ENVIRONMENT: Development DOTNET_USE_POLLING_FILE_WATCHER: '1' NUGET_FALLBACK_PACKAGES: /root/.nuget/fallbackpackages image: test101:dev labels: com.microsoft.visualstudio.debuggee.arguments: ' --additionalProbingPath /root/.nuget/packages --additionalProbingPath /root/.nuget/fallbackpackages bin/Debug/netcoreapp2.0/test101.dll' com.microsoft.visualstudio.debuggee.killprogram: /bin/bash -c "if PID=$$(pidof -x dotnet); then kill $$PID; fi" com.microsoft.visualstudio.debuggee.program: dotnet com.microsoft.visualstudio.debuggee.workingdirectory: /app ports: - target: 80 volumes: - /c/Users/Admin/source/test101:/app:rw - /c/Users/Admin/vsdbg/vs2017u5:/remote_debugger:ro - /c/Program Files/dotnet/sdk/NuGetFallbackFolder:/root/.nuget/fallbackpackages:ro - /c/Users/Admin/.nuget/packages:/root/.nuget/packages:ro version: '3.4' docker ps --filter "status=running" --filter "name=dockercompose16069050365708511483_test100_" --format {{.ID}} -n 1 docker ps --filter "status=running" --filter "name=dockercompose16069050365708511483_test101_" --format {{.ID}} -n 1 docker-compose -f "C:\Users\Admin\source\repos\docker-compose.yml" -f "C:\Users\Admin\source\repos\docker-compose.override.yml" -f "C:\Users\Admin\source\repos\obj\Docker\docker-compose.vs.debug.g.yml" -p dockercompose16069050365708511483 --no-ansi build The DOCKER_REGISTRY variable is not set. Defaulting to a blank string. Building test101 Step 1/3 : FROM microsoft/aspnetcore:2.0 AS base ---> c69d39472da9 Step 2/3 : WORKDIR /app ---> Using cache ---> 8ec70a82ff5c Step 3/3 : EXPOSE 80 ---> Using cache ---> 449b52457632 Successfully built 449b52457632 Successfully tagged test101:dev Building test100 Step 1/3 : FROM microsoft/aspnetcore:2.0 AS base ---> c69d39472da9 Step 2/3 : WORKDIR /app ---> Using cache ---> 8ec70a82ff5c Step 3/3 : EXPOSE 80 ---> Using cache ---> 449b52457632 Successfully built 449b52457632 Successfully tagged test100:dev docker-compose -f "C:\Users\Admin\source\repos\docker-compose.yml" -f "C:\Users\Admin\source\repos\docker-compose.override.yml" -f "C:\Users\Admin\source\repos\obj\Docker\docker-compose.vs.debug.g.yml" -p dockercompose16069050365708511483 --no-ansi up -d --no-build --force-recreate --remove-orphans The DOCKER_REGISTRY variable is not set. Defaulting to a blank string. Creating network "dockercompose16069050365708511483_default" with the default driver Creating dockercompose16069050365708511483_test101_1 ... Creating dockercompose16069050365708511483_test100_1 ... Creating dockercompose16069050365708511483_test101_1 ... done Creating dockercompose16069050365708511483_test100_1 ... done Done! Docker containers are ready. ========== Debugging ========== docker ps --filter "status=running" --filter "name=dockercompose16069050365708511483_test100_" --format {{.ID}} -n 1 9dbf250042f3 docker ps --filter "status=running" --filter "name=dockercompose16069050365708511483_test101_" --format {{.ID}} -n 1 e2e33faeb36c ========== Closing Solution ========== Shutting down running containers. ========== Preparing Containers ========== Getting Docker containers ready... C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -NoProfile -WindowStyle Hidden -ExecutionPolicy RemoteSigned -File "C:\Users\Admin\AppData\Local\Temp\GetVsDbg.ps1" -Version vs2017u5 -RuntimeID debian.8-x64 -InstallPath "C:\Users\Admin\vsdbg\vs2017u5" A debug operation has started during container preparation, so the operation will be cancelled. ========== Debugging ========== docker ps --filter "status=running" --filter "name=dockercompose16069050365708511483_test102_" --format {{.ID}} -n 1 b4d77df1598d ========== Debugging ========== docker ps --filter "status=running" --filter "name=dockercompose16069050365708511483_test102_" --format {{.ID}} -n 1 b4d77df1598d 的方式)。

. /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)) } 文件将包含以下行,因此每次启动Shell时都会获取该文件的源代码:

.bashrc

如果需要,此文件可以包含多个功能。因此,您无需创建多个文件即可提供多种功能。