bash - 在脚本中组合不同的 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 脚本并自动执行。
任何建议/线索将不胜感激。
解决方案
而不是
cd
来回前进,将find
/shell 命令设置为所需的文件夹find /folder/test1/check** ...
使用bash 命令替换调用shell 脚本
find ... -mtime $(myShellScriptThatReturnsHours)
而不是
cd
'ing back,用于../
获取父目录使用上述几点,您可以使用类似的东西
grep -h projname $(find /folder/test1/check** -maxdepth 1 -mtime "$(myShellScriptThatReturnsHours)) > folder/extraction_08_03_2021.txt
注意:
如果
../
“更改”目录还不够,您可以调用myShellScriptThatReturnsHours
子外壳程序,我们将导航到所需的文件夹。-mtime $(( cd /folder/test1/; myShellScriptThatReturnsHours))
推荐阅读
- sql-server - 使用 CTE 递归替换字符串
- pine-script - 在交易视图的每日开盘时绘制水平射线
- reactjs - 比上一次渲染时渲染了更多的钩子。使用 React Hooks 发布表单数据时
- optaplanner - Optaplanner 中的可空链式规划变量
- python - 如何使用 scikit-build 编译扩展模块作为另一个 setup.py 的一部分
- javascript - 如何在 MongoDB 的数组中列出对象中的字段?
- angular - 通过宿主元素动画角度元素不起作用
- sql - 为什么使用索引但sql仍然很慢
- android - 如何让 Flutter 抽屉在里面有 Dismissible 时关闭?
- kubernetes - 如何配置 Helm Chart 的 Ingress 文件来部署 gRPC 服务?