-1

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.

  • 2
    I fail to see how that script will accomplish anything you said you'd like to do. All the snippet in the title does is to take the commands first command line parameter, multiply it by two. – tink Jan 29 at 20:17
  • 1
    If all you're curious about is the $(( [...] )) syntax, it would be helpful to edit all of the irrelevant context out of the question so that it does not distract from your actual query. – DopeGhoti Jan 29 at 20:24
  • I would have shortened it, had I written the code myself and known exactly how it was supposed to work. But the script was written by my professors and we were told to use it to manipulate six different csv-files via multithreading. We were told that the bash-script would multiply all 100 entries by the factor 2 and create new files. The problem is that I do not know what &(( [...] )) means syntaxwise. I couldn't see how a simple if-condition could be used to manipulate 100 entries at once, so I thought that maybe the solution was in the part I didn't understand. – Michael Prammer Jan 29 at 20:36
2

The syntax $(( [...] )) is for performing integer math.

$ echo $(( 2 + 2 ))
4
$ foo=5
$ echo $(( $foo + 2 ))
7
$ echo $(( $foo / 2 ))
2
$ echo $(( $foo % 2 ))
1

In your case, your script is using $1, which evaluates to the first parameter when it is expanded, so if your script were invoked with ./script.sh 43, $1 would evaluate to 43, and so $(( $1 * 2)) would evaluate to 86.

  • Thank you for your help. I thought myself that the $ would probably be some kind of parameter. But I was confused, since we had 100 entries and not just one. But I probably just misunderstood the assignment. Maybe the idea was to let the bash-script run a loop inside the Python-script until all 100 entries are multiplied. Thank you again for your help - I think now I know where to look next. – Michael Prammer Jan 29 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.