-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix.sh
More file actions
executable file
·29 lines (23 loc) · 823 Bytes
/
fix.sh
File metadata and controls
executable file
·29 lines (23 loc) · 823 Bytes
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
# create a script that read from file in the following format:
# file:text
# the text is to be prepended to the file at `WORKING_DIR/$file`
# make WORKING_DIR read from argv 1
#!/usr/bin/env bash
WORKING_DIR="$1"
while IFS= read -r line; do
FILE_PATH="${WORKING_DIR}/$(echo "$line" | cut -d: -f1)"
echo "Prepending to $FILE_PATH"
TEXT_TO_PREPEND="$(echo "$line" | cut -d: -f2-)"
# Create the directory if it doesn't exist
mkdir -p "$(dirname "$FILE_PATH")"
# only prepend if the file exists
if [ ! -f "$FILE_PATH" ]; then
echo "File $FILE_PATH does not exist. Skipping."
continue
fi
# Prepend the text to the file
{
echo "$TEXT_TO_PREPEND"
cat "$FILE_PATH"
} > "${FILE_PATH}.tmp" && mv "${FILE_PATH}.tmp" "$FILE_PATH"
done < args.txt