Shell-Script Bash
I have this loop in the shell script, but it seems I don't fully understand what it does:
especially gawk -v
for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k | gawk -v s=4 '{print $1*s}'
done
Assume the arguments we have are 2 10 4
shell-script awk gawk
|
show 3 more comments
I have this loop in the shell script, but it seems I don't fully understand what it does:
especially gawk -v
for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k | gawk -v s=4 '{print $1*s}'
done
Assume the arguments we have are 2 10 4
shell-script awk gawk
2
I'd be happy to answer none school work questions. Could you elaborate on in what context this is used and how it fits into a larger picture? You specify three arguments - but only $1 and $3 is used..
– Claus Andersen
Dec 11 '18 at 15:27
@ClausAndersen gawk -v means that we are assigning the valuse of s to 4 but how about the{print $1*s}
– fatima
Dec 11 '18 at 15:50
This is not a question, just a statement.
– ctrl-alt-delor
Dec 11 '18 at 15:56
What shell is it for?
– ctrl-alt-delor
Dec 11 '18 at 15:57
1
Did you run the script and see what it does?
– RudiC
Dec 11 '18 at 16:32
|
show 3 more comments
I have this loop in the shell script, but it seems I don't fully understand what it does:
especially gawk -v
for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k | gawk -v s=4 '{print $1*s}'
done
Assume the arguments we have are 2 10 4
shell-script awk gawk
I have this loop in the shell script, but it seems I don't fully understand what it does:
especially gawk -v
for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k | gawk -v s=4 '{print $1*s}'
done
Assume the arguments we have are 2 10 4
shell-script awk gawk
shell-script awk gawk
edited Dec 11 '18 at 15:55
ctrl-alt-delor
11.2k42058
11.2k42058
asked Dec 11 '18 at 15:23
fatimafatima
92
92
2
I'd be happy to answer none school work questions. Could you elaborate on in what context this is used and how it fits into a larger picture? You specify three arguments - but only $1 and $3 is used..
– Claus Andersen
Dec 11 '18 at 15:27
@ClausAndersen gawk -v means that we are assigning the valuse of s to 4 but how about the{print $1*s}
– fatima
Dec 11 '18 at 15:50
This is not a question, just a statement.
– ctrl-alt-delor
Dec 11 '18 at 15:56
What shell is it for?
– ctrl-alt-delor
Dec 11 '18 at 15:57
1
Did you run the script and see what it does?
– RudiC
Dec 11 '18 at 16:32
|
show 3 more comments
2
I'd be happy to answer none school work questions. Could you elaborate on in what context this is used and how it fits into a larger picture? You specify three arguments - but only $1 and $3 is used..
– Claus Andersen
Dec 11 '18 at 15:27
@ClausAndersen gawk -v means that we are assigning the valuse of s to 4 but how about the{print $1*s}
– fatima
Dec 11 '18 at 15:50
This is not a question, just a statement.
– ctrl-alt-delor
Dec 11 '18 at 15:56
What shell is it for?
– ctrl-alt-delor
Dec 11 '18 at 15:57
1
Did you run the script and see what it does?
– RudiC
Dec 11 '18 at 16:32
2
2
I'd be happy to answer none school work questions. Could you elaborate on in what context this is used and how it fits into a larger picture? You specify three arguments - but only $1 and $3 is used..
– Claus Andersen
Dec 11 '18 at 15:27
I'd be happy to answer none school work questions. Could you elaborate on in what context this is used and how it fits into a larger picture? You specify three arguments - but only $1 and $3 is used..
– Claus Andersen
Dec 11 '18 at 15:27
@ClausAndersen gawk -v means that we are assigning the valuse of s to 4 but how about the
{print $1*s}
– fatima
Dec 11 '18 at 15:50
@ClausAndersen gawk -v means that we are assigning the valuse of s to 4 but how about the
{print $1*s}
– fatima
Dec 11 '18 at 15:50
This is not a question, just a statement.
– ctrl-alt-delor
Dec 11 '18 at 15:56
This is not a question, just a statement.
– ctrl-alt-delor
Dec 11 '18 at 15:56
What shell is it for?
– ctrl-alt-delor
Dec 11 '18 at 15:57
What shell is it for?
– ctrl-alt-delor
Dec 11 '18 at 15:57
1
1
Did you run the script and see what it does?
– RudiC
Dec 11 '18 at 16:32
Did you run the script and see what it does?
– RudiC
Dec 11 '18 at 16:32
|
show 3 more comments
2 Answers
2
active
oldest
votes
I can only imagine this is a homework question.
Let's break this down. Firstly you have a loop which starts at $1
and increments by $3
and will stop at 6
. So if you pass in 2 10 4
, then the loop will start at 2
, and increment by 4
. It will immediately stop because 2 + 4 = 6
.
So the following just print's 2
. With the arguments 2 10 4
.
for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k
done
Each time round the loop, you process the output using gawk -v s=4 '{print $1*s}'
. This is a very small "awk" program. It sets a variable s=4
. Then prints $1*s
(ie: it calculates 2 * 4
and just prints 8
.
add a comment |
The awk
bit just prints the current value of $k
(this is $1
in the awk
code as it is read from the input) times 4 (this is the value of the awk
variable s
, as set on the command line).
It would be shorter to do
printf '%dn' "$(( 4*k ))"
The loop goes from whatever the first argument is to 5 in steps of the third argument. The second argument does not make any difference.
Therefore, the whole thing could be simplified down to
seq "$(( 4*$1 ))" "$(( 4*$3 ))" 20
The three arguments to GNU seq
are "start, increment, and end". This is for the output, and the output will always be four times the current value of the loop variable. The loop starts at $1
, so the output starts at four times that. The loop increments by $3
, so we increment by four times that. The loop ends with $k
at a maximum of 5 (one less than 3+3
), so the output ends at 4*5
.
Or, if you want to do that seq
call as a bash
loop instead:
for (( k = 4*$1; k <= 20; k += 4*$3 )); do
printf '%dn' "$k"
done
And, as you see,
for (( k = $1; k <= 5; k += $3 )); do
printf '%dn' "$(( 4*k ))"
done
is not far from that.
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%2f487373%2fshell-script-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I can only imagine this is a homework question.
Let's break this down. Firstly you have a loop which starts at $1
and increments by $3
and will stop at 6
. So if you pass in 2 10 4
, then the loop will start at 2
, and increment by 4
. It will immediately stop because 2 + 4 = 6
.
So the following just print's 2
. With the arguments 2 10 4
.
for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k
done
Each time round the loop, you process the output using gawk -v s=4 '{print $1*s}'
. This is a very small "awk" program. It sets a variable s=4
. Then prints $1*s
(ie: it calculates 2 * 4
and just prints 8
.
add a comment |
I can only imagine this is a homework question.
Let's break this down. Firstly you have a loop which starts at $1
and increments by $3
and will stop at 6
. So if you pass in 2 10 4
, then the loop will start at 2
, and increment by 4
. It will immediately stop because 2 + 4 = 6
.
So the following just print's 2
. With the arguments 2 10 4
.
for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k
done
Each time round the loop, you process the output using gawk -v s=4 '{print $1*s}'
. This is a very small "awk" program. It sets a variable s=4
. Then prints $1*s
(ie: it calculates 2 * 4
and just prints 8
.
add a comment |
I can only imagine this is a homework question.
Let's break this down. Firstly you have a loop which starts at $1
and increments by $3
and will stop at 6
. So if you pass in 2 10 4
, then the loop will start at 2
, and increment by 4
. It will immediately stop because 2 + 4 = 6
.
So the following just print's 2
. With the arguments 2 10 4
.
for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k
done
Each time round the loop, you process the output using gawk -v s=4 '{print $1*s}'
. This is a very small "awk" program. It sets a variable s=4
. Then prints $1*s
(ie: it calculates 2 * 4
and just prints 8
.
I can only imagine this is a homework question.
Let's break this down. Firstly you have a loop which starts at $1
and increments by $3
and will stop at 6
. So if you pass in 2 10 4
, then the loop will start at 2
, and increment by 4
. It will immediately stop because 2 + 4 = 6
.
So the following just print's 2
. With the arguments 2 10 4
.
for ((k=$1; $k<3 + 3 ; k=$k + $3))
do
echo $k
done
Each time round the loop, you process the output using gawk -v s=4 '{print $1*s}'
. This is a very small "awk" program. It sets a variable s=4
. Then prints $1*s
(ie: it calculates 2 * 4
and just prints 8
.
edited Dec 12 '18 at 9:30
answered Dec 11 '18 at 16:54
coulingcouling
494311
494311
add a comment |
add a comment |
The awk
bit just prints the current value of $k
(this is $1
in the awk
code as it is read from the input) times 4 (this is the value of the awk
variable s
, as set on the command line).
It would be shorter to do
printf '%dn' "$(( 4*k ))"
The loop goes from whatever the first argument is to 5 in steps of the third argument. The second argument does not make any difference.
Therefore, the whole thing could be simplified down to
seq "$(( 4*$1 ))" "$(( 4*$3 ))" 20
The three arguments to GNU seq
are "start, increment, and end". This is for the output, and the output will always be four times the current value of the loop variable. The loop starts at $1
, so the output starts at four times that. The loop increments by $3
, so we increment by four times that. The loop ends with $k
at a maximum of 5 (one less than 3+3
), so the output ends at 4*5
.
Or, if you want to do that seq
call as a bash
loop instead:
for (( k = 4*$1; k <= 20; k += 4*$3 )); do
printf '%dn' "$k"
done
And, as you see,
for (( k = $1; k <= 5; k += $3 )); do
printf '%dn' "$(( 4*k ))"
done
is not far from that.
add a comment |
The awk
bit just prints the current value of $k
(this is $1
in the awk
code as it is read from the input) times 4 (this is the value of the awk
variable s
, as set on the command line).
It would be shorter to do
printf '%dn' "$(( 4*k ))"
The loop goes from whatever the first argument is to 5 in steps of the third argument. The second argument does not make any difference.
Therefore, the whole thing could be simplified down to
seq "$(( 4*$1 ))" "$(( 4*$3 ))" 20
The three arguments to GNU seq
are "start, increment, and end". This is for the output, and the output will always be four times the current value of the loop variable. The loop starts at $1
, so the output starts at four times that. The loop increments by $3
, so we increment by four times that. The loop ends with $k
at a maximum of 5 (one less than 3+3
), so the output ends at 4*5
.
Or, if you want to do that seq
call as a bash
loop instead:
for (( k = 4*$1; k <= 20; k += 4*$3 )); do
printf '%dn' "$k"
done
And, as you see,
for (( k = $1; k <= 5; k += $3 )); do
printf '%dn' "$(( 4*k ))"
done
is not far from that.
add a comment |
The awk
bit just prints the current value of $k
(this is $1
in the awk
code as it is read from the input) times 4 (this is the value of the awk
variable s
, as set on the command line).
It would be shorter to do
printf '%dn' "$(( 4*k ))"
The loop goes from whatever the first argument is to 5 in steps of the third argument. The second argument does not make any difference.
Therefore, the whole thing could be simplified down to
seq "$(( 4*$1 ))" "$(( 4*$3 ))" 20
The three arguments to GNU seq
are "start, increment, and end". This is for the output, and the output will always be four times the current value of the loop variable. The loop starts at $1
, so the output starts at four times that. The loop increments by $3
, so we increment by four times that. The loop ends with $k
at a maximum of 5 (one less than 3+3
), so the output ends at 4*5
.
Or, if you want to do that seq
call as a bash
loop instead:
for (( k = 4*$1; k <= 20; k += 4*$3 )); do
printf '%dn' "$k"
done
And, as you see,
for (( k = $1; k <= 5; k += $3 )); do
printf '%dn' "$(( 4*k ))"
done
is not far from that.
The awk
bit just prints the current value of $k
(this is $1
in the awk
code as it is read from the input) times 4 (this is the value of the awk
variable s
, as set on the command line).
It would be shorter to do
printf '%dn' "$(( 4*k ))"
The loop goes from whatever the first argument is to 5 in steps of the third argument. The second argument does not make any difference.
Therefore, the whole thing could be simplified down to
seq "$(( 4*$1 ))" "$(( 4*$3 ))" 20
The three arguments to GNU seq
are "start, increment, and end". This is for the output, and the output will always be four times the current value of the loop variable. The loop starts at $1
, so the output starts at four times that. The loop increments by $3
, so we increment by four times that. The loop ends with $k
at a maximum of 5 (one less than 3+3
), so the output ends at 4*5
.
Or, if you want to do that seq
call as a bash
loop instead:
for (( k = 4*$1; k <= 20; k += 4*$3 )); do
printf '%dn' "$k"
done
And, as you see,
for (( k = $1; k <= 5; k += $3 )); do
printf '%dn' "$(( 4*k ))"
done
is not far from that.
edited Dec 11 '18 at 17:30
answered Dec 11 '18 at 17:23
KusalanandaKusalananda
127k16240395
127k16240395
add a comment |
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%2f487373%2fshell-script-bash%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'd be happy to answer none school work questions. Could you elaborate on in what context this is used and how it fits into a larger picture? You specify three arguments - but only $1 and $3 is used..
– Claus Andersen
Dec 11 '18 at 15:27
@ClausAndersen gawk -v means that we are assigning the valuse of s to 4 but how about the
{print $1*s}
– fatima
Dec 11 '18 at 15:50
This is not a question, just a statement.
– ctrl-alt-delor
Dec 11 '18 at 15:56
What shell is it for?
– ctrl-alt-delor
Dec 11 '18 at 15:57
1
Did you run the script and see what it does?
– RudiC
Dec 11 '18 at 16:32