I have a script that installs a bunch of programs (homebrew, git, etc) and I have it log to stdout
all the things that its doing along the way, but I want the ability to pass a flag, such as --no-log
to turn off the logging. The logging functions are sourced from a file that has a bunch of helper scripts and I'm having trouble getting the flag to pass around to these other files.
[EDIT]
Given (./bin/install.sh)
#!/usr/bin/env bash
# Test for known flags
for opt in $@
do
case $opt in
--no-log) export SILENT=true ;;
-*|--*)
e_warning "Invalid option $opt"
run_help
;;
esac
done
source ./lib/utils
e_process "Installing Homebrew"
ruby -e "$(curl -#fkL raw.github.com/Homebrew/homebrew/go/install)"
and (./lib/utils.sh)
#!/usr/bin/env bash
logging() {
# write your test however you want; this just tests if SILENT is non-empty
if [ -n "$SILENT" ]; then
"$@" &> /dev/null
else
"$@"
fi
}
# Command/Processing logging
e_process() {
logging printf "$(tput setaf 6)┃ $(tput sgr0)$(tput setaf 7)%s...$(tput sgr0)\n" "$@"
}
Then
If I run ./bin/dotfiles
I expect to see the logging ┃ Installing Homebrew...
, but if I run ./bin/dotfiles --no-log
I expect to not see the logging, however its not working.
Output (using bash -x
)
$ bash -x ./bin/install.sh
+ source ./lib/utils.sh
+ e_process 'Installing Homebrew'
++ tput setaf 6
++ tput sgr0
++ tput setaf 7
++ tput sgr0
+ logging printf '┃ %s...\n' 'Installing Homebrew'
+ '[' -n '' ']'
+ printf '┃ %s...\n' 'Installing Homebrew'
┃ Installing Homebrew...
Using the --no-log
bash -x ./bin/install.sh --no-log
+ for opt in '$@'
+ case $opt in
+ export SILENT=true
+ SILENT=true
+ source ./lib/utils.sh
+ e_process 'Installing Homebrew'
++ tput setaf 6
++ tput sgr0
++ tput setaf 7
++ tput sgr0
+ logging printf '┃ %s...\n' 'Installing Homebrew'
+ '[' -n true ']'
+ printf '┃ %s...\n' 'Installing Homebrew'
e_process
? – glenn jackman Mar 31 '14 at 21:33