If you have a test.sh
containing
make -j$(nproc)
How could you temporarily adjust nproc, along the lines of nproc=1 ./test.sh
?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityCreate a wrapper that prints $nproc
:
#!/bin/sh
[ -z "$nproc" ] && exec /usr/bin/nproc "$@"
printf '%s\n' "$nproc"
Name it nproc
, make it executable (chmod +x nproc
) and place it in your /special/directory
for custom wrappers. Now you have two options:
On demand:
PATH="/special/directory:$PATH" nproc=1 ./test.sh
Permanent. Modify your PATH
so /special/directory
is always there as the first entry. Then you can just
nproc=1 ./test.sh
Notes:
PATH
, the solution may not work.nproc
(by its full path to avoid calling itself recursively). This way nproc
will behave exactly as before, unless you set the variable to any non-empty value.In your example $(nproc)
is not quoted. This means you can inject additional options via the variable (e.g. nproc="2 -i"
, the wrapper will print this string, in the script it will be split into two words and -i
will be passed to make
). You may
implement some logic into the wrapper, so its output is less likely to be split; example:
set -- $nproc
printf '%s\n' "$1"
# word splitting done in the wrapper, only the first word is printed
(proper solution) fix the script:
make -j"$(nproc)"
# if the wrapper prints unexpected string, make will comply
If test.sh
is a bash script, another way could be to export a function named nproc
:
bash-5.0$ cat foo.sh
#! /bin/bash
echo make -j $(nproc)
bash-5.0$ ./foo.sh
make -j 8
Creating and exporting a function:
bash-5.0$ nproc() { echo function 10; }
bash-5.0$ (export -f nproc; ./foo.sh)
make -j function 10
test.sh
so that the number of processors is passed in the first parameter, and themake
line becomesmake -j$1
? You can default to the current script action by preceding with[ $# == 0 ] && set $(nproc)
.