-1

At the moment I am manually searching for three characters which can be anything in dir1 dir2 dir3 etc

By going grep -i -r abc dir1

then

grep -i -r abc dir2

grep -i -r abc dir3

etc

Trying to automate this somewhat and thought about writing a shell script, something like search.sh

and then when I want to search for something in the above directories I can put the three letters that I'm searching for

For example: run search.sh $Mid = abc

The shell script would do something like this:

$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
codeforester
  • 39,467
  • 16
  • 112
  • 140
amanda89
  • 17
  • 1
  • 2
  • 3
  • Your question is not clear, but I think you just want `grep -ir abc dir1 dir2 dir3` or better yet `grep -ir abc dir{1..3}`. – wjandrea Oct 19 '17 at 03:27
  • Otherwise, this is a duplicate of [How to pass parameters to a Linux Bash script?](https://stackoverflow.com/q/2645636/4518341) – wjandrea Oct 19 '17 at 03:36

2 Answers2

2

Very simple script and straightforward approach. Arguments are passed with $n, here n is number of the arguments 1,2,3 etc.

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

Output:

$ ./simple.sh hello world
Simple Script
hello world
danglingpointer
  • 4,708
  • 3
  • 24
  • 42
0

You can pass in arguments to a script and retrieve them as positional parameters inside your script.

So running:

./search.sh abc

You can access the argument "abc" with $1 inside the script (assuming you are passing in a single parameter).

I would recommend simply reading up on Linux Script Arguments online.

Jesse
  • 1,814
  • 1
  • 21
  • 25