Skip to content

Commit 332e1f2

Browse files
authored
Update prepend_text_to_file.sh
1 parent 5831817 commit 332e1f2

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

src/prepend_text_to_file.sh

+44-19
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,65 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
2+
set -euo pipefail
23

3-
# Function Name: prepend_text_to_file
4-
# Description: This function prepends provided text to the specified file.
5-
# If the file does not exist, it will be created, and the text will be written to it.
4+
# Function: prepend_text_to_file
5+
# Description: Prepend provided text to the specified file.
6+
# If the file does not exist, it will be created.
67
# Usage: prepend_text_to_file <file> <text>
7-
88
prepend_text_to_file() {
9-
10-
# Validate the number of arguments
11-
if [ $# -ne 2 ]; then
12-
echo "Error: Incorrect number of arguments."
13-
echo "Usage: prependTextToFile <file> <text>"
9+
# Require at least two arguments: a file and some text.
10+
if [ "$#" -lt 2 ]; then
11+
echo "Error: Incorrect number of arguments." >&2
12+
echo "Usage: prepend_text_to_file <file> <text>" >&2
1413
return 1
1514
fi
1615

1716
local file="$1"
18-
local text="$2"
17+
shift
18+
# Combine all remaining arguments as the text to prepend.
19+
local text="$*"
20+
21+
# If the file exists but is not a regular file, exit with error.
22+
if [ -e "$file" ] && [ ! -f "$file" ]; then
23+
echo "Error: '$file' exists but is not a regular file." >&2
24+
return 1
25+
fi
1926

20-
# Handle the case where the file does not exist by creating it
21-
if [ ! -f "$file" ]; then
27+
# If the file doesn't exist, create it.
28+
if [ ! -e "$file" ]; then
2229
echo "Notice: File '$file' does not exist. Creating file..."
2330
touch "$file"
2431
fi
2532

26-
# Prepend the text to the file
27-
{ echo "$text"; cat "$file"; } > "temp" && mv "temp" "$file"
33+
# Ensure the file is writable.
34+
if [ ! -w "$file" ]; then
35+
echo "Error: File '$file' is not writable." >&2
36+
return 1
37+
fi
38+
39+
# Create a secure temporary file.
40+
local tmpfile
41+
tmpfile=$(mktemp) || { echo "Error: Could not create temporary file." >&2; return 1; }
42+
# Ensure the temporary file is removed on function exit.
43+
trap 'rm -f "$tmpfile"' RETURN
44+
45+
# Prepend the text: write the new text first, then the existing file content.
46+
{ echo "$text"; cat "$file"; } > "$tmpfile"
47+
mv "$tmpfile" "$file"
48+
# Clear the temporary file trap (temporary file has been moved).
49+
trap - RETURN
2850

29-
# Display log info in the console
51+
# Log the action.
3052
echo "The following text was prepended to the file '$file':"
3153
echo "$text"
3254
}
3355

56+
# Main function to call the prepend_text_to_file function.
3457
main() {
35-
# Call the function
36-
prepend_text_to_file "$1" "$2"
58+
if [ "$#" -lt 2 ]; then
59+
echo "Usage: $0 <file> <text>" >&2
60+
exit 1
61+
fi
62+
prepend_text_to_file "$@"
3763
}
3864

3965
main "$@"
40-

0 commit comments

Comments
 (0)