#!/bin/bash
# Input file list (replace with your actual list)
file_list="file1.txt file2.txt file3.txt" # Example: "path/to/file1.txt path/to/file2.txt"
# Output file
output_file="Device-Management-Combined.csv"
# Check if the output file exists. If not, create it with a header (optional)
if [ ! -f "$output_file" ]; then
# Optional: Add a header row if the file is new
echo "Filename,Contents" > "$output_file"
fi
# Iterate through the file list
for file in $file_list; do
# Check if the file exists
if [ -f "$file" ]; then
# Append the filename with a hash symbol
echo "#$file" >> "$output_file"
# Append the contents of the file
cat "$file" >> "$output_file"
else
echo "Warning: File '$file' not found. Skipping." >&2 # Send warning to stderr
fi
done
echo "Files combined into '$output_file'"
Key improvements and explanations:
Clearer Variable Definitions: Uses variables for the file list and output file, making the script more readable and easier to modify.
File Existence Check: Crucially, the script now checks if each file in the list actually exists using [ -f "$file" ]. This prevents errors if a file is missing. It also prints a warning message to standard error (>&2) if a file is not found, which is good practice.
Output File Creation/Header: The script checks if the output file exists. If it doesn't, it creates it. I've added an optional line to add a header row ("Filename,Contents") to the CSV file. Remove that line if you don't need a header.
cat for Content Appending: Uses cat "$file" >> "$output_file" to efficiently append the file's contents.
>> for Appending: Uses the >> operator to append to the output file, rather than overwriting it.
Error Handling (Warning): Includes a warning message if a file in the list is not found. Sending the warning to standard error (>&2) is best practice.
Informative Message: Prints a message to the console when the script is finished.
Shebang: Includes #!/bin/bash to specify the interpreter.
Quoting: Uses double quotes around variables ("$file", "$output_file") to handle filenames with spaces or special characters correctly. This is very important.
Comments: Includes comments to explain what each part of the script does.
How to use:
Save the script: Save the code above to a file, for example, combine_files.sh.
Make it executable: Run chmod +x combine_files.sh in your terminal.
Modify the file_list variable: Replace "file1.txt file2.txt file3.txt" with the actual list of files you want to combine, including their paths if they are not in the same directory as the script. Separate filenames with spaces.
Run the script: Execute the script by running ./combine_files.sh in your terminal.
Example:
If you have files data1.txt and data2.txt in the same directory as the script, and you want to combine them into Device-Management-Combined.csv, you would modify the script like this:
bash
#!/bin/bash
file_list="data1.txt data2.txt"
output_file="Device-Management-Combined.csv"
# ... rest of the script ...
Then run ./combine_files.sh. The Device-Management-Combined.csv file will contain:
code
Filename,Contents # If you included the header
#data1.txt
(contents of data1.txt)
#data2.txt
(contents of data2.txt)
This revised answer provides a robust and well-documented solution to the problem, addressing potential issues and following best practices for bash scripting. It's also much more user-friendly.