From 373c3903c228ea6105545637de72726b657f8fd1 Mon Sep 17 00:00:00 2001 From: tedbo Date: Mon, 12 Apr 2010 10:52:40 -0700 Subject: [PATCH] Add sudo_clobber and sudo_append functions so that we can "echo" and "cat" stuff and pipe it to something that can write to a file as root. For example: echo "foo" | sudo_append /tmp/bar will append "foo" to the file /tmp/bar as root. While echo "foo" | sudo_clobber /tmp/bar will truncate /tmp/bar and then write "foo" to the file. Review URL: http://codereview.chromium.org/1610021 --- common.sh | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/common.sh b/common.sh index f75fa6b130..c5ae104132 100644 --- a/common.sh +++ b/common.sh @@ -149,8 +149,8 @@ esac # Functions function setup_board_warning { - echo - echo "$V_REVERSE================= WARNING ======================$V_VIDOFF" + echo + echo "$V_REVERSE================= WARNING ======================$V_VIDOFF" echo echo "*** No default board detected in " \ "$GCLIENT_ROOT/src/scripts/.default_board" @@ -162,8 +162,8 @@ function setup_board_warning { # Sets the default board variable for calling script function get_default_board { - DEFAULT_BOARD= - + DEFAULT_BOARD= + if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then DEFAULT_BOARD=`cat "$GCLIENT_ROOT/src/scripts/.default_board"` fi @@ -323,3 +323,17 @@ function eretry () { function remove_quotes() { echo "$1" | sed -e "s/^'//; s/'$//" } + +# Writes stdin to the given file name as root using sudo in overwrite mode. +# +# $1 - The output file name. +function sudo_clobber() { + sudo tee "$1" > /dev/null +} + +# Writes stdin to the given file name as root using sudo in append mode. +# +# $1 - The output file name. +function sudo_append() { + sudo tee -a "$1" > /dev/null +}