linux - 带参数运行 shell 脚本

标签 linux bash shell grep

目前我正在手动搜索三个字符,它们可以是 dir1 dir2 dir3 等中的任何内容

通过 grep -i -r abc dir1

然后

grep -i -r abc dir2

grep -i -r abc dir3

等等

尝试将其自动化并考虑编写一个 shell 脚本,例如 搜索.sh

然后当我想在上述目录中搜索某些内容时,我可以输入我正在搜索的三个字母

例如:运行search.sh $Mid = abc

shell 脚本会执行如下操作:

$mid = Mid;
grep -i -r $mid nab-prep1001 | grep -i -r $mid nab-prep1002 | grep -i -r $mid multi-account-bpay-report | grep -i -r $mid nab-prep1004 | grep -i -r $mid nab-prep100 | grep -i -r $mid nab-prep1006 | grep -i -r $mid nab-prep1007

最佳答案

非常简单的脚本和直接的方法。参数通过 $n 传递,这里 n 是参数的数量 1,2,3 等。

#!/bin/bash
echo "Simple Script"
echo "$1" "$2

输出:

$ ./simple.sh hello world
Simple Script
hello world

关于linux - 带参数运行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46822327/

相关文章:

linux - 替换字符串并在目录中的所有 .html 文件中包含日期

linux - Bash 是编译的还是解释的?

linux - 在调用函数时使用 $ 或 `(tilt) 与通常使用其名称调用函数有什么区别

c++ - 默认情况下使用 g++ 而不是 arm 编译器进行构建?

Linux 上的 Java Robot - 无大写字符(使用 x11vnc )

linux - ESP8266 模块在启动时失败

bash - 在 Bash 中批量删除文件名中的前导破折号

bash - 避免在 bash 中重复设置变量

java - 新定义的环境变量在 WSL2 之外看不到?

bash - 在 bash shell 脚本中检查目录的问题