scheduling - How to add a `nice` switch to my bash script -
i have bash script runs jobs. want able run jobs nice lower priority on server.
for example, if executable a.out, can run terminal nice a.out lower job priority.
in bash script have variable nice. 1 of following 2 things:
# either nice set nice="nice" # or left unset nice="" i run job using
"$nice$ ./a.out later in script.
this works when nice="nice" not work when nice left set nice="".
one way around use if statement:
if [ "$nice" == "nice" ] nice ./a.out else ./a.out fi but becomes 5 or 6 lines of code rather single line.
is possible accomplish attempting using nice variable launch executable niceness or no niceness?
your first guess almost right.
from bash man page (emphasis mine):
explicit null arguments ("" or '') retained , passed commands empty strings. unquoted implicit null arguments, resulting expansion of parameters have no values, removed. if parameter no value expanded within double quotes, null argument results , retained , passed command empty string. when quoted null argument appears part of word expansion non-null, null argument removed. is, word -d'' becomes -d after word splitting , null argument removal.
so case, drop quotes:
$nice ./a.out which, depending on value of $nice, expand following two-word command:
'nice' './a.out' or one-word command:
'./a.out' you can use set -x debug kind of expansion -- prints expanded word list can see if has gone amiss.
Comments
Post a Comment