Skip to content

Commit

Permalink
📝 correctly reading the last line of any file
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed Apr 23, 2024
1 parent 0fa36e2 commit 7addbe6
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 13 deletions.
6 changes: 5 additions & 1 deletion docs/working-on-bash-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,22 @@ IFS='' read -r -d '' myString < file
echo "${myString}"
```

> With this technique, the last line of the file is always read, even if it does not have a trailing newline.
### Read a file, line by line

This example is for the newline (`$'\n'`) delimiter which is the default delimiter of read, but you can specify any delimiter with `IFS=''` + the `-d ''` option.

Do:

```bash
while read -r myString; do
while read -r myString || [[ -n ${myString:-} ]]; do
echo "${myString}"
done < file
```

> Note the `|| [[ -n ${myString:-} ]]` which allows to read the last line even if the file does not have a trailing newline.
Or read into an array and then loop through it:

```bash
Expand Down
2 changes: 1 addition & 1 deletion tests.d/1005-lib-io/resources/file-to-read
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ The production of meat and other animal derived products places a heavy burden o

## For people

Just like veganism is the sustainable option when it comes to looking after our planet, plant-based living is also a more sustainable way of feeding the human family. A plant-based diet requires only one third of the land needed to support a meat and dairy diet. With rising global food and water insecurity due to a myriad of environmental and socio-economic problems, there's never been a better time to adopt a more sustainable way of living. Avoiding animal products is not just one of the simplest ways an individual can reduce the strain on food as well as other resources, it's the simplest way to take a stand against inefficient food systems which disproportionately affect the poorest people all over the world. Read more about how vegan diets can help people.
Just like veganism is the sustainable option when it comes to looking after our planet, plant-based living is also a more sustainable way of feeding the human family. A plant-based diet requires only one third of the land needed to support a meat and dairy diet. With rising global food and water insecurity due to a myriad of environmental and socio-economic problems, there's never been a better time to adopt a more sustainable way of living. Avoiding animal products is not just one of the simplest ways an individual can reduce the strain on food as well as other resources, it's the simplest way to take a stand against inefficient food systems which disproportionately affect the poorest people all over the world. Read more about how vegan diets can help people.
1 change: 0 additions & 1 deletion tests.d/1005-lib-io/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ The production of meat and other animal derived products places a heavy burden o
## For people
Just like veganism is the sustainable option when it comes to looking after our planet, plant-based living is also a more sustainable way of feeding the human family. A plant-based diet requires only one third of the land needed to support a meat and dairy diet. With rising global food and water insecurity due to a myriad of environmental and socio-economic problems, there's never been a better time to adopt a more sustainable way of living. Avoiding animal products is not just one of the simplest ways an individual can reduce the strain on food as well as other resources, it's the simplest way to take a stand against inefficient food systems which disproportionately affect the poorest people all over the world. Read more about how vegan diets can help people.
```

### Testing io::createFilePathIfNeeded
Expand Down
4 changes: 2 additions & 2 deletions tests.d/1100-self-config/01.self-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function testSelfConfig() {
selfConfig && exitCode=0 || exitCode=$?
echo
echo "cat \${configFile}"
io::readFile "${configFile}" && echo "${LAST_RETURNED_VALUE}"
io::cat "${configFile}"
endTest "Testing selfConfig" ${exitCode}

echo "→ selfConfig"
Expand All @@ -40,7 +40,7 @@ function testSelfConfig() {
) && exitCode=0 || exitCode=$?
echo
echo "cat \${configFile}"
io::readFile "${configFile}" && echo "${LAST_RETURNED_VALUE}"
io::cat "${configFile}"
endTest "Testing selfConfig override export" ${exitCode}

VALET_CONFIG_FILE="${originalConfigFile}"
Expand Down
2 changes: 1 addition & 1 deletion tests.d/1300-valet-cli/.before-test
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function echoTempFileWithTimeStampSubstitution() {
local file="${GLOBAL_TEST_TEMP_FILE}"
local line
local IFS=$'\n'
while read -rd $'\n' line; do
while read -rd $'\n' line || [[ -n ${line:-} ]]; do
line="${line//??:??:??/HH:MM:SS}"
line="${line//????-??-??/YYYY:MM:DD}"
echo "${line}"
Expand Down
6 changes: 4 additions & 2 deletions valet.d/commands.d/self-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,12 @@ function summarize() {

# Bump the valet build version by one patch.
function bumpValetBuildVersion() {
local versionFile currentVersion
local versionFile
versionFile="${GLOBAL_VALET_HOME}/valet.d/version"

IFS= read -rd '' currentVersion <"${versionFile}" || :
io::readFile "${versionFile}"
local currentVersion="${LAST_RETURNED_VALUE:-0.0.0}"
currentVersion="${currentVersion%%$'\n'*}"

string::bumpSemanticVersion "${currentVersion}" "patch" "false"

Expand Down
4 changes: 2 additions & 2 deletions valet.d/commands.d/self-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ function createRelease() {
fi

# read the version from the valet file
local version
IFS= read -rd '' version <"${GLOBAL_VALET_HOME}/valet.d/version" || :
io::readFile "${GLOBAL_VALET_HOME}/valet.d/version"
local version="${LAST_RETURNED_VALUE}"
version="${version%%$'\n'*}"
log::info "The current version of valet is: ${version}."

Expand Down
4 changes: 2 additions & 2 deletions valet.d/lib-io
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ function io::readFile() {
return 0
fi

local IFS=
local IFS=''
if [[ "${maxCharacters}" -gt 0 ]]; then
read -rd '' -n "${maxCharacters}" LAST_RETURNED_VALUE <"${filePath}" || true
else
Expand Down Expand Up @@ -376,5 +376,5 @@ function io::sleep() {
# io::cat "myFile"
function io::cat() {
io::readFile "${1}"
printf "%s" "${LAST_RETURNED_VALUE}"
echo "${LAST_RETURNED_VALUE}"
}
2 changes: 1 addition & 1 deletion valet.d/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.7.146
0.7.164

0 comments on commit 7addbe6

Please sign in to comment.