Skip to content

Instantly share code, notes, and snippets.

@korjaa
Last active May 18, 2022 06:11
Show Gist options
  • Save korjaa/089928fe1451844e73c668dc23f4c730 to your computer and use it in GitHub Desktop.
Save korjaa/089928fe1451844e73c668dc23f4c730 to your computer and use it in GitHub Desktop.
Bash snippets.
#
# Loops
#
# Loop files with weird names and reading from console stdin.
while IFS= read -r -d $'\0' FILE; do
read -p "Press Enter to continue to \"$FILE\"" </dev/tty
done < <(find . -printf "%P\0" | grep --null-data "filter")
# Loop files with less surprises
# Better ones https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash for best versions
while read -u 10 LINE; do
echo "$LINE"
done 10< <(cat lines.txt | grep filter)
# Loop through array variables.
array=( item1 item2 item3 )
for item in "${array[@]}"; do
echo $item
done
# Update old DIA diagram files
while IFS= read -r -d $'\0' SPATH; do
RPATH="${SPATH%.dia}.png"
if [ "$SPATH" -nt "$RPATH" ]; then
dia --export "$RPATH" "$SPATH"
fi
done < <(find . -name "*.dia" -printf "%P\0")
#
# Utility
#
# Flattens a folder structure with "_"
find -type f -printf '%P\0' | while read -d $'\0' i ; do mv "$i" "${i////_}" ; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment