|
1 |
| -#!/bin/bash |
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
2 | 3 |
|
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. |
6 | 7 | # Usage: prepend_text_to_file <file> <text>
|
7 |
| - |
8 | 8 | 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 |
14 | 13 | return 1
|
15 | 14 | fi
|
16 | 15 |
|
17 | 16 | 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 |
19 | 26 |
|
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 |
22 | 29 | echo "Notice: File '$file' does not exist. Creating file..."
|
23 | 30 | touch "$file"
|
24 | 31 | fi
|
25 | 32 |
|
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 |
28 | 50 |
|
29 |
| - # Display log info in the console |
| 51 | + # Log the action. |
30 | 52 | echo "The following text was prepended to the file '$file':"
|
31 | 53 | echo "$text"
|
32 | 54 | }
|
33 | 55 |
|
| 56 | +# Main function to call the prepend_text_to_file function. |
34 | 57 | 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 "$@" |
37 | 63 | }
|
38 | 64 |
|
39 | 65 | main "$@"
|
40 |
| - |
|
0 commit comments