UNSORTED/DEV & SCRIPTING/BASH
Crappy method
for line in $(cat file); do
echo $line
done
While / Read method
echo "--- FILE READING # Method n°1 ---"
while IFS= read -r line; do
printf '%s\n' "$line"
done <data
Fast & efficient
echo -e "\n\n--- FILE READING #Method n°2 ---"
value=$(<data)
echo "$value"
GOOD METHOD
state=$(<base64.txt)
for i in {1..100}; do
state=$(<<<"$state" base64 --decode)
done
echo "$state"
Notes:
• Backticks (`) are discouraged. Use $(...) instead. bash deprecated and obsolete syntax
• for i in cat is a common antipattern in bash. How to read a file line by line in bash
• If the file contains one line only, there is no need to iterate over the words in the file.
• In bash echo "$item" | is a useless usage of echo (and is also a small risk that it may not work, when ex. item=-e). You can use a here string instead when in bash.
for folder in `ls /ftp`; do
if [ -z "$(ls -A /ftp/$folder)" ]; then
echo "$folder: EMPTY"
else
echo -ne "$folder: "
find /ftp/$folder/ -type f -exec stat \{} --printf="%y\n" \; |sort -n -r | head -n1
fi
done