Trying to understand the syntax for echo $(($1 * 2))
Clash Royale CLAN TAG#URR8PPP
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.
linux bash shell-script
add a comment |
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.
linux bash shell-script
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
add a comment |
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.
linux bash shell-script
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.
linux bash shell-script
linux bash shell-script
edited Jan 29 at 20:12
Rui F Ribeiro
40.3k1479137
40.3k1479137
asked Jan 29 at 20:10
Michael PrammerMichael Prammer
62
62
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
add a comment |
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
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
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
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
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
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
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f497537%2ftrying-to-understand-the-syntax-for-echo-1-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
.
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
.
answered Jan 29 at 20:19
DopeGhotiDopeGhoti
45.6k55988
45.6k55988
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
add a comment |
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
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
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
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f497537%2ftrying-to-understand-the-syntax-for-echo-1-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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