首页 > 解决方案 > 在脚本中组合不同的 shell 命令

问题描述

我正在尝试使用 bash 脚本完成一系列 4 个步骤来提取和移动文件

  1. Cd to a folder "/folder/test1"
  2. Run a shell script to get the variable "$hours"
  3. Cd to a folder "/folder/test2"
  4. Use the variable "$hours" created in step 2 to run the below command:
           grep -h projname $(find check** -maxdepth 1 -mtime $hours) > folder/extraction_08_03_2021.txt

目前,我手动 cd 到文件夹并单独运行脚本/命令。我是 shell 脚本的新手,不确定这 4 个步骤是否可以组合在 shell 脚本中,以便我只运行 .sh 脚本并自动执行。

任何建议/线索将不胜感激。

标签: bash

解决方案


  1. 而不是cd来回前进,将find/shell 命令设置为所需的文件夹

    find /folder/test1/check** ...
    
  2. 使用bash 命令替换调用shell 脚本

    find ... -mtime $(myShellScriptThatReturnsHours)
    
  3. 而不是cd'ing back,用于../获取父目录

  4. 使用上述几点,您可以使用类似的东西

    grep -h projname $(find /folder/test1/check** -maxdepth 1 -mtime "$(myShellScriptThatReturnsHours)) > folder/extraction_08_03_2021.txt
    

    注意

    如果../“更改”目录还不够,您可以调用myShellScriptThatReturnsHours外壳程序,我们将导航到所需的文件夹

    -mtime $(( cd /folder/test1/; myShellScriptThatReturnsHours))
    

推荐阅读