-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprepend
More file actions
executable file
·62 lines (58 loc) · 1.6 KB
/
prepend
File metadata and controls
executable file
·62 lines (58 loc) · 1.6 KB
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
#!/usr/bin/env bash
# author: andreasl
show_help() {
script_name="${0##*/}"
msg="${script_name}\n"
msg+="Prepend a given file with a given string from stdin.\n"
msg+="\n"
msg+="Usage:\n"
msg+=" ${script_name} [OPTIONS] <file> <<< <input-string>\n"
msg+=" echo <input-string> | ${script_name} <file>\n"
msg+="\n"
msg+="Options:\n"
msg+=" -h, --help: Print the help message.\n"
msg+=" -i, --inline: Write output to same file as input file.\n"
msg+=" -o <file>, --output <file>: Write output to specified output file.\n"
msg+="\n"
msg+="Examples:\n"
msg+=" ${script_name} script.py <<< '#!/usr/env/bin python3\\\n# -*- coding: utf-8 -*-'\n"
msg+=" printf 'Step 0: ' | ${script_name} myfile.txt\n"
printf "$msg"
}
inline=false
while [ "$#" -gt 0 ]; do
case "$1" in
-i | --inline)
inline=true
;;
-o | --output)
output_file="$2"
shift # past argument
;;
-h | --help)
show_help
exit 0
;;
*) # unknown option
input_file="$1"
;;
esac
shift # past argument or value
done
if [ -z "$input_file" ]; then
printf "Error: No input file specified.\n"
exit 1
fi
if [ "$inline" == true ]; then
output_file="$input_file"
fi
if [ -z "$output_file" ]; then
printf "Error: No output file specified.\n"
exit 1
fi
while IFS= read -rN1 character; do
input_to_prepend+="$character"
done
file_content="$(<"$input_file")"
printf -- "$input_to_prepend" >"$output_file"
printf -- "$file_content" >>"$output_file"