| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | |
| 3 | # ncpus - Print the number of (virtual) processors |
| 4 | # |
| 5 | # Copyright 2009 Chuan-kai Lin |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | |
| 19 | CPUS=`grepscip-shell system . . grep. '^processor' /proc/cpuinfoscip-shell filesystem . . `/proc/cpuinfo`. | wcscip-shell system . . wc. -l` |
| 20 | |
| 21 | function isnumberscip-shell shell shell-project 0.0.0 isnumber(). { |
| 22 | [scip-shell system . . `[`. "$1" -eq "$1" ] 2> /dev/nullscip-shell filesystem . . `/dev/null`. |
| 23 | } |
| 24 | |
| 25 | function limitscip-shell shell shell-project 0.0.0 limit(). { |
| 26 | if [scip-shell system . . `[`. "$1" -gt 0 ] |
| 27 | then echoscip-shell system . . echo. "$1" |
| 28 | else echoscip-shell system . . echo. 1 |
| 29 | fi |
| 30 | } |
| 31 | |
| 32 | function offsetscip-shell shell shell-project 0.0.0 offset(). { |
| 33 | if isnumberscip-shell shell shell-project 0.0.0 isnumber(). "$1" |
| 34 | then limitscip-shell shell shell-project 0.0.0 limit(). "$[CPUS$1]" |
| 35 | else echoscip-shell system . . echo. "$CPUS" |
| 36 | fi |
| 37 | } |
| 38 | |
| 39 | if [scip-shell system . . `[`. -z "$1" ] |
| 40 | then echoscip-shell system . . echo. "$CPUS" |
| 41 | elif [scip-shell system . . `[`. "$1" != "${1##+}" ] |
| 42 | then offsetscip-shell shell shell-project 0.0.0 offset(). "$1" |
| 43 | elif [scip-shell system . . `[`. "$1" != "${1##-}" ] |
| 44 | then offsetscip-shell shell shell-project 0.0.0 offset(). "$1" |
| 45 | elif isnumberscip-shell shell shell-project 0.0.0 isnumber(). "$1" |
| 46 | then limitscip-shell shell shell-project 0.0.0 limit(). "$1" |
| 47 | else echoscip-shell system . . echo. "$CPUS" |
| 48 | fi |