Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a bash script which asks a user for the number of CPU cores and saves it to variable named $cores. Now I want to add this variable to .bashrc, so I ask user how much CPU cores he has and then if he wants to save this value to .bashrc.

Now the question, how I can check if $cores already exist in .bashrc so the script won't ask the user again?

share|improve this question
2  
Note that the .bashrc belongs to the user and may be shared by different machines. –  Stéphane Chazelas Jun 20 at 12:54
add comment

3 Answers

up vote -1 down vote accepted

you can check whether a variable is set in bash using:

if [[ -z "$cores" ]]
then
    echo "not set"
else
    echo "set"
fi

This will check whether $cores variable is set or not. that is if $cores is null it will display "not set" otherwise "set". As a matter of fact .bashrc is not sourced automatically for non-interactive shells, such as those started when you execute a shell script. So you would put . .bashrc near the beginning of your .bash_login file, to ensure that .bashrc is sourced for both login and non-login interactive shells.

share|improve this answer
1  
You're missing a $ in the conditional. As written, that will always print "set". –  godlygeek Jun 20 at 12:22
    
@godlygeek: Thanks for pointing the mistake :) problem in pasting. –  harish.venkat Jun 20 at 12:34
1  
That checks if the variable is empty. That condition is true for unset variables but also for variables that are set to the empty string. [[ -z "${cores+set}" ]] would be for checking specifically if the variable is unset (that also works for array or hash variables with bash or zsh, but not ksh93 (though with bash if an array or hash is set but has no member, it's considered unset (a WA is to use declare -p array > /dev/null 2>&1))). –  Stéphane Chazelas Jun 20 at 12:41
add comment

Try this:

'awk /\$core/ { print }'
share|improve this answer
    
The {print} is redundant in this case (it is awk's default)... –  jasonwryan Jun 21 at 1:40
add comment

Instead of prompting the user for how many cores the system has, why not just ask the system? This is better because it doesn't involve writing to a user-owned file. See something like this, which uses 'getconf' to request NPROCESSORS_CONF variable. Or for other systems, the ideas presented here may be helpful - using sysctl or a grep over /proc/cpuinfo to find the number of cores.

share|improve this answer
    
It basically needs number of "make -jX" to use. Usually CPU Core number is used, but somebody may use something else. –  albru123 Jun 20 at 12:29
    
See also info libc 'Processor Resources' on a GNU system for more details. Using NPROCESSORS_ONLN (the number of online processors), would be better than NPROCESSORS_CONF (the number of processors present). –  Stéphane Chazelas Jun 20 at 12:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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