Hi there,
I'm currently preparing for an exam and came across a shell script.
I can't try this out myself because I don't have access my to my PC right now.
This is the script
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 then started with the line:
ls | /home/notroot/scripts/myscript.sh
And these are the files in the current directiory: Asprin, Benz, Burroughs, Claremont, Eddings, Doms, Gardner, Lucas, Martin, McKiernam,
Pabst, Pratchett, Schlederer, Tolkien, Vollenbruch
Question now is, 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"
The results I came to are the following:
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:
- Once
- 7 times
- 3 times
- 3 times
- Once
So yeah, I would appreciate it greatly if someone could tell me whether I understood how the script works correctly or not.
Thanks in advance and cheers!