function - bash Call ext func in a echo -
been testing out external functions. overall seem work expected, except one.
i set colors in extern script (mylib)
this seems work fine:
#!/bin/bash . mylib red echo " red text " . mylib white echo " white text "
this isn't, not sure how should go. i'm looking change colors on same line. (without escape coding)
#!/bin/bash . mylib red echo "red text " . mylib white " white text"
i tried out few brackets , '$' ideas, fell short.
thanks dave
you can this, it's ridiculously inefficient.
echo "red text $(source mylib; white) white text"
it slightly better source library once:
source mylib # needs done once! echo "red text $(white) white text"
think it: every single time want change colors, you're asking shell this:
- call
fork()
create subshell - in parent shell, start reading output emitted subshell
- in subshell, read file
mylib
, execute each line (in former case) - in subshell, call function
white
- in subshell, exit status of last command run (in case, command being
white
function). [because subshell exiting, means work did reading , parsingmylib
thrown away, , next time color change needed,mylib
need reread , reparsed scratch]. - in parent shell, see child shell has closed; call
waitpid()
reap process table. - in parent shell, substitute content read subshell
echo
command being run.
it much, more efficient if running source mylib
set variable named white
, , sourced mylib
once , thereafter included variable reference:
source mylib # needs done once! echo "red text ${white} white text"
in case, there's no subshell ever required, less 1 per color change.
you might wish review bashfaq #37 discussion of best practices dealing colors.
Comments
Post a Comment