Processing a List of Files from an Input File
Suppose we have a list of files stored in a text file and we want to perform an operation on each of them within a bash script. How would we go about doing that? Well, there are several options, here are a few.
$ cat input-files.txt
file-1.txt
file-2.txt
file-3.txt
file-4.txt
file-5.txt
file-6.txt
file-7.txt
file-8.txt
file-9.txt
Now, if we want to operate on each file in this list we can do something like this:
while IFS= read -r filename
do
echo "Do something on ${filename} here..."
done < "input-files.txt"
or alternatively,
input="input-files.txt"
while IFS= read -r filename
do
printf '%s\n' "${filename}"
done < "${input}"
Tags: cli, bash, shell-scripting, motd
String Processing with Bash
There are various tools built into bash that enable you to manipulate a variable or string which come in handy when writing shell scripts. Here are a few notable ones:
Find the length of a string
${#string}
Get a Substring from a String
${string:pos} or ${string:pos:len}
Removing Substrings from a String
${string#substr}
${string%substr}
${string##substr}
${string%%substr}
Some examples
Here’s a few examples of how you can process a variable that points to an absolute path of a file and shows how to extract certain parts of said file’s path:
var=/home/user/code/blogstuff/index.html.j2
echo ${var} # => /home/user/code/blogstuff/index.html.j2
echo ${var#*.} # => html.j2
echo ${var##*.} # => j2
echo ${var%/*.*} # => /home/user/code/blogstuff
Replace a Substring of a String
${string/pattern/substr} # => Replaces the 1st match found
${string//pattern/substr} # => Replaces all of the matches found
Replace the Beginning or End of a String
${string/#pattern/substr}
${string/%pattern/substr}
file=${var##/*/} # => index.html.j2
echo ${file/#index/fubar} # => fubar.html.j2
echo ${file/%j2/fubar} # => index.html.fubar
Tags: cli, bash, shell-scripting, motd