-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappend2file.sh
More file actions
executable file
·47 lines (36 loc) · 1.17 KB
/
append2file.sh
File metadata and controls
executable file
·47 lines (36 loc) · 1.17 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
#!/bin/sh
# append2file.sh
# Useful with sudo (instead of ">>")
# Example:
# $ sudo append2file.sh /etc/hosts 192.168.0.9 foobar
# Created on 2014-02-17 by Julien MOREAU aka PixEye
# Last commit of this file (GMT) :
# $Id$
# Local time: $Date$
nbwp=2 # Number of wanted parameters (without options)
cmd=`basename $0` # Command name
usage="Usage: $cmd [-h]\n" # Help message:
usage=$usage"\t Display this help message.\n"
usage=$usage"\n"
usage=$usage"Usage: $cmd <file_path> [<words>]\n"
usage=$usage"\t Add words (extra parameters) or lines (from stdin) to a file.\n"
usage=$usage"\t Useful with sudo."
echo "\ntest"|grep -q ntest && e="-e" # Does 'echo' need the -e option?
if [ "$#" -lt $nbwp -o "$1" = "-h" ] ; then # Check parameters number
echo $e $usage 1>&2 ; exit 2 # Display message help and exit
fi
file_path="$1"
shift
if test ! -w "$file_path" ; then # If the file in not writable:
echo $e "$cmd: cannot write file \"$file_path\"!" 1>&2 ; exit 3
fi
if test "$#" -gt 0
then echo $* >> "$file_path"
else
while read line
do echo "$line" >> "$file_path" || break
done
fi
exit 0 # Normal exit
# Prefs for vim editing:
# vim: tabstop=8 shiftwidth=8 textwidth=80