Bash script, get number of physical cores as number and iterate
I wrote a script that shows a number of physical cores of the machine. However, I would like the result to be a number, not a string.
Here's the script:
phycores=echo $sudoPW | cat /proc/cpuinfo | grep -m 1 "cpu cores" | awk '{print $ 4;}'
echo $phycores  
for i in {1..$phycores}
do
   echo "Core $i"
done
bash cpu script
add a comment |
I wrote a script that shows a number of physical cores of the machine. However, I would like the result to be a number, not a string.
Here's the script:
phycores=echo $sudoPW | cat /proc/cpuinfo | grep -m 1 "cpu cores" | awk '{print $ 4;}'
echo $phycores  
for i in {1..$phycores}
do
   echo "Core $i"
done
bash cpu script
 
 
 
 
 
 
 - bashconvert on the fly strings to numbers. so you should not concern about this
 – Romeo Ninov
 Dec 10 at 13:27
 
 
 
 
 
 
 
 
 
 But why am I getting a newline while trying to echo the- $phycores? It shows 2 [Enter], not only 2
 – Brian Brown
 Dec 10 at 13:29
 
 
 
 
 
 
 
 
 
 What do you mean by "physical cores"? What you're trying to extract from- cpuinfomay not be what you want. See this question.
 – Kamil Maciorowski
 Dec 10 at 13:41
 
 
 
 
 
 
 
 1
 
 
 
 
 bash doesn't have the notion of numbers. Everything is a string or an array of strings.
 – gronostaj
 Dec 10 at 13:54
 
 
 
 
 
 
 
 
 
 @BrianBrown If you only need the "number" then strip the trailing newline- nr,- n
 – dmb
 Dec 10 at 14:20
 
 
 
add a comment |
I wrote a script that shows a number of physical cores of the machine. However, I would like the result to be a number, not a string.
Here's the script:
phycores=echo $sudoPW | cat /proc/cpuinfo | grep -m 1 "cpu cores" | awk '{print $ 4;}'
echo $phycores  
for i in {1..$phycores}
do
   echo "Core $i"
done
bash cpu script
I wrote a script that shows a number of physical cores of the machine. However, I would like the result to be a number, not a string.
Here's the script:
phycores=echo $sudoPW | cat /proc/cpuinfo | grep -m 1 "cpu cores" | awk '{print $ 4;}'
echo $phycores  
for i in {1..$phycores}
do
   echo "Core $i"
done
bash cpu script
bash cpu script
edited Dec 10 at 14:07
asked Dec 10 at 13:25
Brian Brown
92414
92414
 
 
 
 
 
 
 - bashconvert on the fly strings to numbers. so you should not concern about this
 – Romeo Ninov
 Dec 10 at 13:27
 
 
 
 
 
 
 
 
 
 But why am I getting a newline while trying to echo the- $phycores? It shows 2 [Enter], not only 2
 – Brian Brown
 Dec 10 at 13:29
 
 
 
 
 
 
 
 
 
 What do you mean by "physical cores"? What you're trying to extract from- cpuinfomay not be what you want. See this question.
 – Kamil Maciorowski
 Dec 10 at 13:41
 
 
 
 
 
 
 
 1
 
 
 
 
 bash doesn't have the notion of numbers. Everything is a string or an array of strings.
 – gronostaj
 Dec 10 at 13:54
 
 
 
 
 
 
 
 
 
 @BrianBrown If you only need the "number" then strip the trailing newline- nr,- n
 – dmb
 Dec 10 at 14:20
 
 
 
add a comment |
 
 
 
 
 
 
 - bashconvert on the fly strings to numbers. so you should not concern about this
 – Romeo Ninov
 Dec 10 at 13:27
 
 
 
 
 
 
 
 
 
 But why am I getting a newline while trying to echo the- $phycores? It shows 2 [Enter], not only 2
 – Brian Brown
 Dec 10 at 13:29
 
 
 
 
 
 
 
 
 
 What do you mean by "physical cores"? What you're trying to extract from- cpuinfomay not be what you want. See this question.
 – Kamil Maciorowski
 Dec 10 at 13:41
 
 
 
 
 
 
 
 1
 
 
 
 
 bash doesn't have the notion of numbers. Everything is a string or an array of strings.
 – gronostaj
 Dec 10 at 13:54
 
 
 
 
 
 
 
 
 
 @BrianBrown If you only need the "number" then strip the trailing newline- nr,- n
 – dmb
 Dec 10 at 14:20
 
 
 
bash convert on the fly strings to numbers. so you should not concern about this– Romeo Ninov
Dec 10 at 13:27
bash convert on the fly strings to numbers. so you should not concern about this– Romeo Ninov
Dec 10 at 13:27
But why am I getting a newline while trying to echo the
$phycores? It shows 2 [Enter], not only 2– Brian Brown
Dec 10 at 13:29
But why am I getting a newline while trying to echo the
$phycores? It shows 2 [Enter], not only 2– Brian Brown
Dec 10 at 13:29
What do you mean by "physical cores"? What you're trying to extract from
cpuinfo may not be what you want. See this question.– Kamil Maciorowski
Dec 10 at 13:41
What do you mean by "physical cores"? What you're trying to extract from
cpuinfo may not be what you want. See this question.– Kamil Maciorowski
Dec 10 at 13:41
1
1
bash doesn't have the notion of numbers. Everything is a string or an array of strings.
– gronostaj
Dec 10 at 13:54
bash doesn't have the notion of numbers. Everything is a string or an array of strings.
– gronostaj
Dec 10 at 13:54
@BrianBrown If you only need the "number" then strip the trailing newline
nr, n– dmb
Dec 10 at 14:20
@BrianBrown If you only need the "number" then strip the trailing newline
nr, n– dmb
Dec 10 at 14:20
add a comment |
                                1 Answer
                                1
                        
active
oldest
votes
I'm spotting a few issues:
- Why the - echo $sudoPWpiped to cat? cat ignores it without a- -somewhere.
- And - $phycoresdoesn't get set without backticks or- $()that can't be the actual script you're running, and it still sets $phycores to a number with a newline?
- The - {1..n}construct doesn't work with a variable, if your- $phycoreswere 4 then it just sets the- $ivariable to- {1..4}. See this Q on stackoverflow for more details. (A newline in- $phycoresshouldn't matter)
Anyway, this should be a more working script
phycores=$(echo $sudoPW|cat - /proc/cpuinfo|grep -m 1 "cpu cores"|awk '{print $ 4;}')
echo $phycores  
for ((i=1;i<=phycores;++i))
do
   echo "Core $i"
done
 
 
 
 
 
 
 In absence of a Linux tag, :) I'll add the (simpler) FreeBSD equivalent: phycores=$(sysctl -n hw.ncpu)
 – Jim L.
 Dec 12 at 22:57
 
 
 
 
 
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fsuperuser.com%2fquestions%2f1382323%2fbash-script-get-number-of-physical-cores-as-number-and-iterate%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
I'm spotting a few issues:
- Why the - echo $sudoPWpiped to cat? cat ignores it without a- -somewhere.
- And - $phycoresdoesn't get set without backticks or- $()that can't be the actual script you're running, and it still sets $phycores to a number with a newline?
- The - {1..n}construct doesn't work with a variable, if your- $phycoreswere 4 then it just sets the- $ivariable to- {1..4}. See this Q on stackoverflow for more details. (A newline in- $phycoresshouldn't matter)
Anyway, this should be a more working script
phycores=$(echo $sudoPW|cat - /proc/cpuinfo|grep -m 1 "cpu cores"|awk '{print $ 4;}')
echo $phycores  
for ((i=1;i<=phycores;++i))
do
   echo "Core $i"
done
 
 
 
 
 
 
 In absence of a Linux tag, :) I'll add the (simpler) FreeBSD equivalent: phycores=$(sysctl -n hw.ncpu)
 – Jim L.
 Dec 12 at 22:57
 
 
 
 
 
add a comment |
I'm spotting a few issues:
- Why the - echo $sudoPWpiped to cat? cat ignores it without a- -somewhere.
- And - $phycoresdoesn't get set without backticks or- $()that can't be the actual script you're running, and it still sets $phycores to a number with a newline?
- The - {1..n}construct doesn't work with a variable, if your- $phycoreswere 4 then it just sets the- $ivariable to- {1..4}. See this Q on stackoverflow for more details. (A newline in- $phycoresshouldn't matter)
Anyway, this should be a more working script
phycores=$(echo $sudoPW|cat - /proc/cpuinfo|grep -m 1 "cpu cores"|awk '{print $ 4;}')
echo $phycores  
for ((i=1;i<=phycores;++i))
do
   echo "Core $i"
done
 
 
 
 
 
 
 In absence of a Linux tag, :) I'll add the (simpler) FreeBSD equivalent: phycores=$(sysctl -n hw.ncpu)
 – Jim L.
 Dec 12 at 22:57
 
 
 
 
 
add a comment |
I'm spotting a few issues:
- Why the - echo $sudoPWpiped to cat? cat ignores it without a- -somewhere.
- And - $phycoresdoesn't get set without backticks or- $()that can't be the actual script you're running, and it still sets $phycores to a number with a newline?
- The - {1..n}construct doesn't work with a variable, if your- $phycoreswere 4 then it just sets the- $ivariable to- {1..4}. See this Q on stackoverflow for more details. (A newline in- $phycoresshouldn't matter)
Anyway, this should be a more working script
phycores=$(echo $sudoPW|cat - /proc/cpuinfo|grep -m 1 "cpu cores"|awk '{print $ 4;}')
echo $phycores  
for ((i=1;i<=phycores;++i))
do
   echo "Core $i"
done
I'm spotting a few issues:
- Why the - echo $sudoPWpiped to cat? cat ignores it without a- -somewhere.
- And - $phycoresdoesn't get set without backticks or- $()that can't be the actual script you're running, and it still sets $phycores to a number with a newline?
- The - {1..n}construct doesn't work with a variable, if your- $phycoreswere 4 then it just sets the- $ivariable to- {1..4}. See this Q on stackoverflow for more details. (A newline in- $phycoresshouldn't matter)
Anyway, this should be a more working script
phycores=$(echo $sudoPW|cat - /proc/cpuinfo|grep -m 1 "cpu cores"|awk '{print $ 4;}')
echo $phycores  
for ((i=1;i<=phycores;++i))
do
   echo "Core $i"
done
answered Dec 10 at 15:31
Xen2050
9,93431536
9,93431536
 
 
 
 
 
 
 In absence of a Linux tag, :) I'll add the (simpler) FreeBSD equivalent: phycores=$(sysctl -n hw.ncpu)
 – Jim L.
 Dec 12 at 22:57
 
 
 
 
 
add a comment |
 
 
 
 
 
 
 In absence of a Linux tag, :) I'll add the (simpler) FreeBSD equivalent: phycores=$(sysctl -n hw.ncpu)
 – Jim L.
 Dec 12 at 22:57
 
 
 
 
 
In absence of a Linux tag, :) I'll add the (simpler) FreeBSD equivalent: phycores=$(sysctl -n hw.ncpu)
– Jim L.
Dec 12 at 22:57
In absence of a Linux tag, :) I'll add the (simpler) FreeBSD equivalent: phycores=$(sysctl -n hw.ncpu)
– Jim L.
Dec 12 at 22:57
add a comment |
Thanks for contributing an answer to Super User!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fsuperuser.com%2fquestions%2f1382323%2fbash-script-get-number-of-physical-cores-as-number-and-iterate%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
bashconvert on the fly strings to numbers. so you should not concern about this– Romeo Ninov
Dec 10 at 13:27
But why am I getting a newline while trying to echo the
$phycores? It shows 2 [Enter], not only 2– Brian Brown
Dec 10 at 13:29
What do you mean by "physical cores"? What you're trying to extract from
cpuinfomay not be what you want. See this question.– Kamil Maciorowski
Dec 10 at 13:41
1
bash doesn't have the notion of numbers. Everything is a string or an array of strings.
– gronostaj
Dec 10 at 13:54
@BrianBrown If you only need the "number" then strip the trailing newline
nr,n– dmb
Dec 10 at 14:20