Bash script, get number of physical cores as number and iterate












0














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









share|improve this question
























  • 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










  • 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




    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
















0














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









share|improve this question
























  • 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










  • 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




    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














0












0








0


0





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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 10 at 14:07

























asked Dec 10 at 13:25









Brian Brown

92414




92414












  • 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










  • 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




    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










  • 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








  • 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










1 Answer
1






active

oldest

votes


















1














I'm spotting a few issues:




  • Why the echo $sudoPW piped to cat? cat ignores it without a - somewhere.


  • And $phycores doesn'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 $phycores were 4 then it just sets the $i variable to {1..4}. See this Q on stackoverflow for more details. (A newline in $phycores shouldn'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





share|improve this answer





















  • 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













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
});


}
});














draft saved

draft discarded


















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









1














I'm spotting a few issues:




  • Why the echo $sudoPW piped to cat? cat ignores it without a - somewhere.


  • And $phycores doesn'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 $phycores were 4 then it just sets the $i variable to {1..4}. See this Q on stackoverflow for more details. (A newline in $phycores shouldn'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





share|improve this answer





















  • 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


















1














I'm spotting a few issues:




  • Why the echo $sudoPW piped to cat? cat ignores it without a - somewhere.


  • And $phycores doesn'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 $phycores were 4 then it just sets the $i variable to {1..4}. See this Q on stackoverflow for more details. (A newline in $phycores shouldn'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





share|improve this answer





















  • 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
















1












1








1






I'm spotting a few issues:




  • Why the echo $sudoPW piped to cat? cat ignores it without a - somewhere.


  • And $phycores doesn'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 $phycores were 4 then it just sets the $i variable to {1..4}. See this Q on stackoverflow for more details. (A newline in $phycores shouldn'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





share|improve this answer












I'm spotting a few issues:




  • Why the echo $sudoPW piped to cat? cat ignores it without a - somewhere.


  • And $phycores doesn'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 $phycores were 4 then it just sets the $i variable to {1..4}. See this Q on stackoverflow for more details. (A newline in $phycores shouldn'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






share|improve this answer












share|improve this answer



share|improve this answer










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




















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

Mangá

Eduardo VII do Reino Unido