-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathprepend_text_to_file.sh
executable file
·65 lines (55 loc) · 1.89 KB
/
prepend_text_to_file.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env bash
set -euo pipefail
# Function: prepend_text_to_file
# Description: Prepend provided text to the specified file.
# If the file does not exist, it will be created.
# Usage: prepend_text_to_file <file> <text>
prepend_text_to_file() {
# Require at least two arguments: a file and some text.
if [ "$#" -lt 2 ]; then
echo "Error: Incorrect number of arguments." >&2
echo "Usage: prepend_text_to_file <file> <text>" >&2
return 1
fi
local file="$1"
shift
# Combine all remaining arguments as the text to prepend.
local text="$*"
# If the file exists but is not a regular file, exit with error.
if [ -e "$file" ] && [ ! -f "$file" ]; then
echo "Error: '$file' exists but is not a regular file." >&2
return 1
fi
# If the file doesn't exist, create it.
if [ ! -e "$file" ]; then
echo "Notice: File '$file' does not exist. Creating file..."
touch "$file"
fi
# Ensure the file is writable.
if [ ! -w "$file" ]; then
echo "Error: File '$file' is not writable." >&2
return 1
fi
# Create a secure temporary file.
local tmpfile
tmpfile=$(mktemp) || { echo "Error: Could not create temporary file." >&2; return 1; }
# Ensure the temporary file is removed on function exit.
trap 'rm -f "$tmpfile"' RETURN
# Prepend the text: write the new text first, then the existing file content.
{ echo "$text"; cat "$file"; } > "$tmpfile"
mv "$tmpfile" "$file"
# Clear the temporary file trap (temporary file has been moved).
trap - RETURN
# Log the action.
echo "The following text was prepended to the file '$file':"
echo "$text"
}
# Main function to call the prepend_text_to_file function.
main() {
if [ "$#" -lt 2 ]; then
echo "Usage: $0 <file> <text>" >&2
exit 1
fi
prepend_text_to_file "$@"
}
main "$@"