Skip to content

Instantly share code, notes, and snippets.

@TobleMiner
Created August 28, 2019 13:32
Show Gist options
  • Save TobleMiner/102146969bc4568a66cd9a20dc0a8aff to your computer and use it in GitHub Desktop.
Save TobleMiner/102146969bc4568a66cd9a20dc0a8aff to your computer and use it in GitHub Desktop.
Simple bash functions for expanding/unexpanding a set of files
unexpand_files() {
(
set -e -o pipefail
first_only='--first-only'
args=''
OPTIND=1
while getopts "at:" opt; do
case "$opt" in
a) args="$args -a"; first_only='';;
t) args="$args -t $OPTARG";;
esac
done
shift $((OPTIND - 1))
for file in $@; do
tmpfile="$(mktemp)"
unexpand $args $first_only "$file" > "$tmpfile"
cp "$tmpfile" "$file"
rm "$tmpfile"
done
)
}
expand_files() {
(
set -e -o pipefail
initial='-i'
args=''
OPTIND=1
while getopts "at:" opt; do
case "$opt" in
a) args="$args -a"; initial='';;
t) args="$args -t $OPTARG";;
esac
done
shift $((OPTIND - 1))
for file in $@; do
tmpfile="$(mktemp)"
expand $args $initial "$file" > "$tmpfile"
cp "$tmpfile" "$file"
rm "$tmpfile"
done
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment