diff --git a/build_library/build_common.sh b/build_library/build_common.sh index a6a9cfa0ac..09a31e6df4 100644 --- a/build_library/build_common.sh +++ b/build_library/build_common.sh @@ -42,7 +42,7 @@ copy_image() { local dst="${BUILD_DIR}/$2" if should_build_image $1; then echo "Creating $2 from $1..." - $COMMON_PV_CAT "${src}" >"${dst}" || die "Cannot copy $1 to $2" + $(pv_cat_cmd) "${src}" >"${dst}" || die "Cannot copy $1 to $2" else mv "${src}" "${dst}" || die "Cannot move $1 to $2" fi diff --git a/common.sh b/common.sh index 980d02717b..7d05f2b953 100644 --- a/common.sh +++ b/common.sh @@ -16,13 +16,22 @@ fi # Ensure that any sub scripts we invoke get the max proc count. export NUM_JOBS="${NUM_JOBS}" -# True if we have the 'pv' utility - also set up COMMON_PV_CAT for convenience -COMMON_PV_OK=1 -COMMON_PV_CAT=pv -pv -V >/dev/null 2>&1 || COMMON_PV_OK=0 -if [ $COMMON_PV_OK -eq 0 ]; then - COMMON_PV_CAT=cat -fi +# Returns the pv command if it's available, otherwise plain-old cat. Note that +# this function echoes the command, rather than running it, so it can be used +# as an argument to other commands (like sudo). +pv_cat_cmd() { + if pv -V >/dev/null 2>&1; then + # Limit pv's output to 80 columns, for readability. + local term_cols=$(stty size 2>/dev/null | cut -d' ' -f2) + if [ ${term_cols:-0} -gt 80 ]; then + echo pv -w 80 + else + echo pv + fi + else + echo cat + fi +} # Make sure we have the location and name of the calling script, using # the current value if it is already set. diff --git a/image_to_usb.sh b/image_to_usb.sh index 30a143272f..0395c002f4 100755 --- a/image_to_usb.sh +++ b/image_to_usb.sh @@ -350,7 +350,7 @@ if [ -b "${FLAGS_to}" ]; then fi if [ ${FLAGS_install} -ne ${FLAGS_TRUE} ]; then - sudo ${COMMON_PV_CAT} "${SRC_IMAGE}" | + sudo $(pv_cat_cmd) "${SRC_IMAGE}" | sudo dd of="${FLAGS_to}" bs=4M oflag=sync status=noxfer sync else @@ -369,7 +369,7 @@ else # Output to a file, so just make a copy. if [ "${SRC_IMAGE}" != "${FLAGS_to}" ]; then echo "Copying image ${SRC_IMAGE} to file ${FLAGS_to}..." - ${COMMON_PV_CAT} "${SRC_IMAGE}" >"${FLAGS_to}" + $(pv_cat_cmd) "${SRC_IMAGE}" >"${FLAGS_to}" fi info "To copy onto a USB/MMC drive /dev/sdX, use: "