File: fai-sed

package info (click to toggle)
fai 6.5.6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,076 kB
  • sloc: sh: 6,720; perl: 5,622; makefile: 138
file content (85 lines) | stat: -rwxr-xr-x 1,530 bytes parent folder | download | duplicates (2)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! /bin/bash

# fai-sed, call sed with diff before writing
#
# (c) Thomas Lange 2019-2025 lange@debian.org
#

# idempotent sed. Execute sed command on a file
# but do not overwrite file if nothing has changed

dryrun=0
showchanged=0

while getopts nE opt ; do
        case "$opt" in
        E) showchanged=1 ;;
        n) dryrun=1 ;;
        esac
done
shift $((OPTIND - 1))


cmd=$1
shift
filename=$1
shift

if [ -z "$cmd" ] || [ -z "$filename" ]; then
    printf "Please add filename and sed command\n"
    exit 3
fi

# do not allow more than one file
if [ -n "$1" ]; then
    printf "Aborting. Too many arguments.\n"
    exit 5
fi

if [ -n "$target" ]; then
    filename="$target$filename"
fi

if [ ! -f $filename ]; then
    printf "WARNING: $filename does not exists. Skipping\n"
    exit 0
fi

tmp=$(mktemp)
trap "rm $tmp" EXIT

printf "sed -e $cmd $filename: "
# cp file so we can use sed -i, otherwise the new file created by sed has different chmod, owner
cp -p $filename $tmp
sed -i -e "$cmd" $tmp
res=$?
if [ $res -ne 0 ]; then
    printf "sed error $res\n"
    exit $res
fi

cmp -s $filename $tmp
res=$?
if [ $res -eq 0 ]; then
    printf " No changes.\n"
    exit 0
fi

if [ $res -eq 1 ]; then
    if [ $dryrun -eq 1 ]; then
	printf " Dry-run. Changes not applied.\n"
	exit 0
    fi
    mv $tmp $filename
    printf " File changed.\n"
    trap '' EXIT
    if [ $showchanged -eq 1 ]; then
	exit 9
    fi
    exit 0
fi

if [ $res -eq 2 ]; then
    printf " diff error $res. No changes made to $filename\n"
    exit 2
fi