Need some help with a bash script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

In the currenty directory are the following files: asprin, benz, burroughs, claremont, eddings, doms, gardner, lucas, martin, mckiernam,
pabst, pratchett, schlederer, tolkien, vollenbruch

Which of the files lead to a change of the variable k.
And how often are the following statements invoked:

1."read WORT"
2. "echo $WORT|grep s>/dev/null"
3. "echo $WORT|grep r>/dev/null"
4. "let k=$k+1"
5. "echo $k"

  1. Relevant commands, code, scripts, algorithms:
myscript.sh
k=0
while read WORT
do
	echo $WORT|grep s>/dev/null || echo $WORT|grep r>/dev/null || let k=$k+1
done
echo $k

The script is started with the following line:

ls | /home/notroot/scripts/myscript.sh
  1. The attempts at a solution (include all code and scripts):

If I understand the script correctly, k would be increased every time the script comes across a file that contains at least one S and at least one R. So the files Asprin, Burroughs and Schlederer would lead to an increment of the variable k.

As for how often the statements are invoked:

  1. 15 times
  2. 7 times
  3. 3 times
  4. 3 times
  5. Once

Just wanna know whether I understood the script correctly.
Thanks in advance.

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

University of Applied Sciences, Trier, Germany, Systemadministration

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

2 and 3 are wrong.

1 Like

Welcome to the forum.

As to your questions:

May I question your reasoning?
a) Where in the script are S or R to be matched? Both upper case?
b) Do you know the meaning of the || operator in shell scripts?

  1. How many input items do you have? And, how is the end-of-file detected?
  2. Once in the loop, how many times do you think the commands or lists will be executed?
    3.and
  3. Again: Do you know the meaning of || in shell scripts? How would it influence the execution of the command list?
  4. Correct!
1 Like

Thanks for your answer. Would you mind helping me to understand how exactly those statements work then.

Is my assumption that "grep s>/dev/null" and "grep r>/dev/null" check whether or not a filename contains an S and an R correct?

So looking over it again, for 2. would this mean it's invoked 15 times too, because it checks every single file.
And as for 3. could it be 7 times, since you only reach the "grep r>/dev/null" part when it found an S in the name (since there are only 7 files with an S)?

---------- Post updated at 09:10 AM ---------- Previous update was at 09:02 AM ----------

a) I believe the filenames were actually all lower case. (As a german, we capitalize names, so that was my mistake when I wrote this down)

b) || means we only reach the right side, when the left side is true, iirc. So once it found a filename with an "s", it checks for an "r"?

  1. We have 15 items. So it goes through the list of files until every single one was checked?
  2. Also 15 times, since it checks every single item for an "s"?
  3. 7 times, since we only reach the second part, when we found an item with an "s"
  4. 3 times, since we only reach that part, when we found an item with an "s" and an "r"

I'm glad to help, but: instead of delivering ready made answers, I prefer to make you cogitate and find the answers yourself.

Partly. Why should grep s try to match an S ? And, recheck your conclusions (what does and when applies the || ?).

Correct!

Count again...

---------- Post updated at 09:10 AM ---------- Previous update was at 09:02 AM ----------

So...?

Please double check the || operator in shell scripts.

And how is end-of-file detected?

Drop the "also"; then, yes.

see answer to b)

see answer to b)

1 Like

Just making sure we're not talking past each other here. The file names are all supposed to be lower case. So grep s is only looking for a lower case "s".

|| Now I see, I believe switched that up. You only reach the right side, when the left side is false.

So to summarize

  1. I'm kinda at a loss here. The answer is probably super simple, but I just don't get it. Could it be only 1 time. Since we don't reach that part anymore because there's no loop for it?
  2. 15 times.
  3. 8 times, since we only reach grep r , when there's no "s" in a file name.
  4. 2 times, since there are only 2 items with neither "s" or "r"
  5. 1 time.

EDIT: WAIT, now I get it.

  1. 16 times. Since we have 15 files and then another time for the EoF.
  2. 15 times. Because we have to check every single one for an "s"
  3. 8 times. Since grep s is false 8 times.
  4. 2 times. Since that's the amount of items with neither an "s" nor an "r".
  5. 1 time.

Exactly. I insisted so you are clear about the difference between exact and sloppy phrasing.

Excellent! What are the conclusions?

Shouldn't it read 15 items PLUS one EOF? The question was "How often executed", no?

Closer. Here some approaches from my side:

ls | ../shscr |& grep  "+ grep" | sort | uniq -c
      9 + grep r
     15 + grep s
ls | ../shscr |& grep "+ echo [A-Z]" | sort | uniq -c | grep -o "^.*echo" | sort | uniq -c
      6       1 + echo           contains an "s"
      9       2 + echo           doesn't contain "s"
ls | ../shscr |& grep -o "+ let" | sort | uniq -c
      2 + let
1 Like

PLEASE don't modify posts (here: file names in post#1) if people have already answered to it, pulling the rug from under their feet. Moving Schlederer to schlederer makes my count 9 to be 8 (as you said before).