Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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'
share|improve this question
    
I don't see where, in the install script, you source the utils script. How does install.sh know how to execute e_process? –  glenn jackman Mar 31 at 21:33
    
Sorry @glennjackman, the script does actually source the file, so I edited to add it in the example. –  chrisopedia Mar 31 at 21:43

1 Answer 1

up vote 0 down vote accepted

You source the utils script before you set the value of the environment variable, so the global variable $silent_mode is empty.

Don't use a "temp" variable in the utils script, stick to $SILENT in those functions.

share|improve this answer
    
Similarly, in the install script, you don't need the no_log var: just export SILENT=true in the case branch. –  glenn jackman Mar 31 at 21:48
    
I've edited the scripts above to reflect the changes, but it still doesn't appear to suppress the logging. When I'm testing this, I'm running bash on OSX 10.9 from within the scripts directory (./bin/dotfiles --no-log) and it still shows all print statments. –  chrisopedia Mar 31 at 21:59
    
run your script with bash -x and see what bash is doing –  glenn jackman Mar 31 at 22:01
    
added to the example above, using bash -x –  chrisopedia Mar 31 at 22:16
    
thats appears to work, so thanks for that –  chrisopedia Mar 31 at 22:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.