I need to use the following bash-script as base for an assignment.
#!/bin/bash lockdir1=/tmp/.myscript.lock1 lockdir2=/tmp/.myscript.lock2 if mkdir "$lockdir1" 2> /dev/null then rm -r "$lockdir1" echo $(($1 * 2)) exit elif mkdir "$lockdir2" 2> /dev/null then echo $(($1 * 2)) rm -r "$lockdir2" exit else echo "Error" rm -r "$lockdir1" rm -r "$lockdir2" pkill calc.sh exit fi
The basic function is that multiple existing CSV-files shall be read and manipulated at the same time, and output will be saved under a new filename. Multithreading is used with semaphores to allow parallel access of exactly two threads (this part will be written as seperate piece of code in Python 3.7).
I think I know what
"$lockdir1" does - it looks like it was a placeholder for the absolute path, which would probably by annyoing if typed directly into the script. It looks like it's purpose is to create temporary directories which are used to save a copy of the files that will be manipulated.
2> /dev/null is probably used to suppress error messages by throwing them away directly.
rm -r is also quite clear: after the files (and their contents) have successfully been manipulated, the temporary files are deleted so that other threads can continue with their business to prevent a deadlock (if the directories were to stay,
mkdir would lead to
"Error" all the time).
The tricky part is getting to understand
echo $(($1 * 2)) - I think I already know what it does: It probably reads contents of the CSV-files (100 unsorted numbers each), duplicating all numbers by a factor of 2. So far so good. But I have no idea how the syntax works.
Why does it have to be expressed like that? What does
echo & mean? Does it show that the following part will be passed as a parameter? And what exactly is the meaning of
(()) - it would make more sense to me if there was some kind of loop, but it is just a condition (if/then/else/). But in our assignment, we will need to double all 100 numbers in each CSV-file by using this script.
Maybe I got the assignment wrong and we are expected to write the loop ourselves (using Python 3.7) but I'd still have a more secure feeling if I understood exactly how
echo $(($1 * 2)) works syntaxwise.