Skip to content

Commit 84ecb85

Browse files
committed
printf
1 parent 5a4e661 commit 84ecb85

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

usr/libexec/helper-scripts/strings.bsh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ check_is_not_empty_and_only_one_line() {
77
local varname value number_of_lines
88

99
if [ -z "$1" ]; then
10-
echo "$0: ERROR: No variable name provided to check_is_not_empty_and_only_one_line." >&2
10+
printf '%s\n' "$0: ERROR: No variable name provided to check_is_not_empty_and_only_one_line." >&2
1111
return 1
1212
fi
1313

@@ -19,7 +19,7 @@ check_is_not_empty_and_only_one_line() {
1919

2020
# Check if value is empty or only contains spaces/newlines.
2121
if [[ -z "$trimmed_value" || -z "${value//[[:space:]]/}" ]]; then
22-
echo "$0: ERROR: Variable '$varname' is empty or contains only whitespace/newlines." >&2
22+
printf '%s\n' "$0: ERROR: Variable '$varname' is empty or contains only whitespace/newlines." >&2
2323
return 1
2424
fi
2525

@@ -28,7 +28,7 @@ check_is_not_empty_and_only_one_line() {
2828
number_of_lines="${#lines[@]}"
2929

3030
if [ "$number_of_lines" -ne 1 ]; then
31-
echo "$0: ERROR: Variable '$varname' contains more than one line. Content:
31+
printf '%s\n' "$0: ERROR: Variable '$varname' contains more than one line. Content:
3232
'$value'" >&2
3333
return 1
3434
fi
@@ -45,15 +45,15 @@ trim_collapse_split() {
4545
local trimmed_list cleaned_list formatted_list
4646

4747
## Step 1: Trim leading and trailing spaces.
48-
trimmed_list="$(echo "$input_list" | sed 's/^\s\+//g; s/\s\+$//g')"
48+
trimmed_list="$(printf '%s\n' "$input_list" | sed 's/^\s\+//g; s/\s\+$//g')"
4949

5050
## Step 2: Replace multiple spaces with a single space.
51-
cleaned_list="$(echo "$trimmed_list" | sed 's/\s\+/ /g')"
51+
cleaned_list="$(printf '%s\n' "$trimmed_list" | sed 's/\s\+/ /g')"
5252

5353
## Step 3: Replace spaces with ' \n' (newline prefixed by space).
54-
formatted_list="$(echo "$cleaned_list" | sed 's/\s/ \\\n/g')"
54+
formatted_list="$(printf '%s\n' "$cleaned_list" | sed 's/\s/ \\\n/g')"
5555

56-
echo "$formatted_list"
56+
printf '%s\n' "$formatted_list"
5757
}
5858

5959
## success: whole numbers such as 1 2 3 456 etc.
@@ -78,18 +78,18 @@ run_test() {
7878
expected_exit_code="$4"
7979

8080
eval "$varname=\"\$value\""
81-
echo "$0: Running test: $test_name"
81+
printf '%s\n' "$0: Running test: $test_name"
8282

8383
exit_code=0
8484
check_is_not_empty_and_only_one_line "$varname" 2>/dev/null || { exit_code=$? ; true; };
8585

8686
if [ "$exit_code" -eq "$expected_exit_code" ]; then
87-
echo "$0: Test Passed"
87+
printf '%s\n' "$0: Test Passed"
8888
else
89-
echo "$0: Test Failed (Expected exit code: $expected_exit_code, Got: $exit_code)" >&2
89+
printf '%s\n' "$0: Test Failed (Expected exit code: $expected_exit_code, Got: $exit_code)" >&2
9090
return 1
9191
fi
92-
echo "---------------------------------"
92+
printf '%s\n' "---------------------------------"
9393
}
9494

9595
tests() {

0 commit comments

Comments
 (0)