0

Concerning the command(s):

path=$1  # ---> I am not entirely sure what this means either
if grep -q -rHl --include \*.c --include \*.h "int main" $path 

So what I do understand is that it is recursively looking for .c and .h files which include "int main" within their contents and wants to echo the name of the file as well.

But what exactly does the "$path" mean and do?

1
  • "it is recursively looking for .c and .h files" ... Looking where? Commented Nov 18, 2018 at 3:11

3 Answers 3

1

In shell scripting, there is a concept called positional parameters. Essentially, you can pass 'n' number of arguments to a shell script or function from the command line. They are then stored in special variables named $0, $1, $2, and so on, which are then accessible inside the shell script. One point to note is the $0 variable refers to the script itself.

Taking your script as an example, you could run it in the following manner:

./myscript.bash /opt/src-code

Here, the parameter /opt/src-code gets stored into the positional variable $1. Your script then reassigns that value to another variable named path. Then the statement is effectively path=/opt/src-code.

The path variable then gets passed to the grep command as its argument. It then determines where to actually run the grep command and look for pattern matches. There is an if specified in your script, which checks if grep does return any value.

In effect, the above steps get reduced to the following grep command at runtime (ignoring the if statement):

grep -q -rHl --include \*.c --include \*.h "int main" /opt/src-code
0

path is set to the 1st argument passed to the script or function that code came from.

( also use # as a comment not --> )

2
  • Ah I see thank you for clearing that up. And then what does the "$path" do at the end of the grep command? Commented Nov 18, 2018 at 2:08
  • at the end of the grep command is the file or directory to be searched. this example has the -r flag to do a recursion of all files in the directory named at the end. Commented Nov 18, 2018 at 4:55
0

path is just a variable. It is created by being assigned the value from the first positional parameter. Then the expression $path is substituted with that value, where the expression is (at the end of the grep command).

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.