-
Notifications
You must be signed in to change notification settings - Fork 3
/
slasetattr
54 lines (45 loc) · 1.64 KB
/
slasetattr
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
#!/bin/bash
# $1 : sla file
# $2 : main XML tag <balise principale
# $3 : attribute
# $4 : value
# DEV NOTES
# use xmlstarlet to update value of an attribute
# ======================================================================
# cf http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html#d0e461
# xml ed -u "/xml/table/rec[@id=3]/@id" -v 5 xml/tab-obj.xml
#
# An alternative would be to use sed :
# http://www.unix.com/shell-programming-and-scripting/141609-changing-attribute-value-xml-file-using-shell.html
# sed -e 's/>myAsNode</>xxxxxx</' -e 's/>myAsNode2</>yyyyyy</' file > newfile
# sed -i '/server/,/<\/server/{s/myAsNode\b/xxxxxx/;s/myAsNode2/yyyyyy/}' infile
#
# or combine grep and sed :
# if [[ $(grep -E "Node Name=\"aaaaa\" Role=\"APPSERV" infile | sed 's/.*Role="\(.*\)"\/>/\1/') == "APPSERV,BACKEND,CLM" ]] ...
# ======================================================================
if [ $# -lt 3 ]
then
echo "error in slasetattr : missing document, tag or attribute parameter\(s\). See with -h option first."
exit
fi
if [ "$4" = "" ] && [ "$get" = "" ]
then
echo "error in slasetattr : missing value parameter\(s\). See with -h option first."
exit
fi
# ext=${filename##*\.} donne l'extension si on voulait gérer $1 "avec ou sans" extension SLA
if [ ! -f "$1.sla" ]
then
echo "Error in slasetattr : file \"$1.sla\" doesn't exist"
exit
fi
# rq : dans slacheckattr, xmlint teste xpath = "//$2/@$3"
if [ ! -f "$1.sla.bak" ]
then
cp "$1.sla" "$1.sla.first.bak"
else
cp -f "$1.sla" "$1.sla.last.bak"
fi
echo Set //$2/@$3 to $4 in $1.sla
xmlstarlet ed -u "//$2/@$3" -v "$4" "$1.sla" > ".$1.sla.tmp"
cp -f ".$1.sla.tmp" "$1.sla"