File: bootcd.lib

package info (click to toggle)
bootcd 6.9.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 520 kB
  • sloc: sh: 1,940; makefile: 49
file content (229 lines) | stat: -rw-r--r-- 6,859 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# vim: set filetype=sh :
#        file: /usr/share/bootcd/bootcd.lib
#   copyright: Bernd Schumacher <bernd.schumacher@hpe.com> (2001-2020)
#     license: GNU General Public License, version 3
# description: bootcd.lib - functions needed by bootcd scripts

chng_opts()
{
  local O
  local C
  local CNOT
  local CADD
  O="$(echo " $1 " | sed "s/[[:blank:]]\+/ /g")"
  C="$(echo " $2 " | sed "s/[[:blank:]]\+/ /g")"
  CNOT="$(echo "$C" |
    sed -e "s/!//g" -e "s/ [^ -]\+//g" -e "s/^ //" -e "s/ $//")"
  CADD="$(echo "$C"| sed -e "s/ ![^ ]\+//g" -e "s/^ //" -e "s/ $//")"
# echo "DEBUG O=<$O>" >&2
# echo "DEBUG C=<$C>" >&2
# echo "DEBUG CNOT=<$CNOT>" >&2
# echo "DEBUG CADD=<$CADD>" >&2

  for i in $CNOT; do
#   echo "DEBUG i=<$i>" >&2
    O="$(echo "$O" | sed "s/ $i [^-]*/ /g")"
#   echo "DEBUG O=<$O>" >&2
  done
  O="$(echo "$O"| sed -e "s/^ //" -e "s/ $//")"
# echo "DEBUG O=<$O>" >&2
  echo "$O $CADD"
}
#echo "test: chng_opts \"-one -two 2 -three drei -four\" \"!-three !-one -two -five 5\"" >&2
#echo "expect: -four -two -five 5" >&2
#echo "result: $(chng_opts "-one -two 2 -three drei -four" "!-three !-one -two -five 5")" >&2
#echo "test: chng_opts \"--one --two 7 --three 8\" \"--three 9\""
#echo "result: $(chng_opts "--one --two 7 --three 8" "--three 9")"
#echo "test: chng_opts \"-a -b\" \"\""
#echo "result: $(chng_opts "-a -b" "")"
#exit 0

oneline()
{
  if [ $# -gt 0 ]; then
    echo "$*"
  else
    cat
  fi | gawk '{printf("%s ",$0)} END {printf("\n")}' | sed "s/ *$//"
}

# ex_proc [-r <PATH_TO_ROOTFS(eval)>] <directory(eval)> ...
# exclude proc, sys and run dirs from given directories
ex_proc()
{
  local SRCDISK SRCDI i n f
  SRCDISK=/
  if [ "$1" = "-r" ]; then
    SRCDISK="$(realpath "$2")"
    SRCDI="$SRCDISK"; [ "$SRCDI" = "/" ] && SRCDI=""
    shift 2
  fi
  if [ $# -gt 0 ]; then
    for i in "$@"; do
      if [ "$(realpath "$i")" = "$SRCDISK" ]; then
        eval "set $(find "$SRCDISK" -maxdepth 1 -exec /bin/sh -c '. /usr/share/shellia/ia; ia_set2var "$@"' sh {} +)"
        n=$#
        for f in "$@"; do
           echo "$f" | grep -e "^$SRCDISK$" -e "^$SRCDI/proc$" -e "^$SRCDI/sys$" -e "^$SRCDI/run$" >/dev/null || set -- "$@" "$f"
        done
        shift $n
        ia_set2var "$@"
      else
        ia_set2var "$(realpath "$i")"
      fi
      /bin/echo -n " "
    done | sed -e "s/ $//"
  fi
}
#. /usr/share/shellia/ia
#a="$(ex_proc /)"
#b="$(ia_set2var /home /proc /sys/ /run /etc)"
#exp="$(ia_set2var /proc /sys/ /run)"
#res="$(ia_set_minus "$b" "$a")"
#[ "$res" = "$exp" ] && echo "ex_proc-1 OK res=<$res>" || echo "ex_proc-1 ERR res=<$res> exp=<$exp>"
#SRCDISK="$(mktemp -d --suffix "s p a c e")"
#mkdir "$SRCDISK/a a" "$SRCDISK/proc" "$SRCDISK/b b"
#touch "$SRCDISK/a a/f"
#exp="$(ia_set2var "$SRCDISK/a a" "$SRCDISK/b b")"
#res="$(ex_proc -r "$SRCDISK" "$SRCDISK")"
#[ "$res" = "$exp" ] && echo "ex_proc-2 OK res=<$res>" || echo "ex_proc-2 ERR res=<$res> exp=<$exp>"
#rm -r "$SRCDISK"
#exit 0

# du_dir <dir> [...] - How much space do given dirs need togehter
du_dir()
{
  local LIST1
  local LAST
  local LIST2
  local LIST3
  local result
  if [ $# -gt 0 ]; then
    LIST1="$(for i in "$@"; do
      /usr/bin/echo -n "\"$(realpath "$i")\" "
      done | sort | sed "s/ $//")"
    LAST=""; LIST2=""
    eval "set -- $LIST1"
    for i in "$@"; do
      if [ "$LAST" ]; then
        if [ "$LAST" = "/" ]; then
          [ "$(echo "$i" | grep "^/")" ] && i=""
        else
          [ "$(echo "$i" | grep "^$LAST\>")" ] && i=""
        fi
      fi
      if [ "$i" ]; then
        [ "$LIST2" ] && LIST2="$LIST2 "
        LIST2="${LIST2}\"$i\""
        LAST="$i"
      fi
    done
    eval "set -- $LIST2"
    LIST3=$(ex_proc "$@")
    dbg "du -klsc $LIST3"
    eval "set -- $LIST3"
    result="$(du -klsc "$@" | tail -1 | gawk '{print $1}')"
    dbg "$result"
    echo "$result"
  fi
}
#. /usr/share/shellia/ia
#dbg() { :; }
#dbg() { echo "dbg <$1>" >&2; }
#d1="$(mktemp -d)"
#d2="$(mktemp -d --suffix " with blanks")"
#dd if=/dev/zero of="$d1/f" bs=10000000 count=1 >/dev/null 2>&1
#dd if=/dev/zero of="$d2/f" bs=10000000 count=2 >/dev/null 2>&1
#s1="$(du_dir "$d1")"
#s2="$(du_dir "$d2")"
#s3="$(du_dir "$d1" "$d2")"
#dbg "s1=<$s1> s2=<$s2> s3=<$s3>"
#[ "$((s1 + s2))" = "$s3" ] && echo "du_dir-1 OK" || echo "ERR du_dir-1 ERR: s1=<$s1> s2=<$s2> s3=<$s3>"
#s3="$(du_dir "$d1" "$d1" "$d1/f" "$d2" "$d2" "$d2/f")"
#[ "$((s1 + s2))" = "$s3" ] && echo "du_dir-2 OK" || echo "ERR du_dir-2 ERR: s1=<$s1> s2=<$s2> s3=<$s3>"
#s1="$(du_dir /etc)"
#s2="$(du_dir /etc/network)"
#s3="$(du_dir /usr/sbin)"
#s4="$(du_dir /etc /etc/network)"
#s5="$(du_dir /etc /usr/sbin)"
#dbg "s1=/etc=<$s1> s2-/etc/network=<$s2> s3=/usr/sbin=<$s3> s4=/etc,/etc/network=<$s4> s5=/etc/,/usr/sbin=<$s5>"
#[ "$s1" = "$s4" ] && echo "du_dir-2 OK s1=s4" || echo "du_dir-2 ERR: s1!=s4"
#[ "$((s1 + s3))" = "$s5" ] && echo "du_dir-3 OK s1+s3=s5" || echo "du_dir-3 ERR: s1+s3!=s5"
#exit 0

# df_file - How much free space has the filesystem on which file $1 resides ?
df_file()
{
  df -k -P | gawk '{printf("%s %s\n",$4,$6)}' |
  while read size filesys
  do
    if [ "$(echo "$1" | grep "^$filesys")" ]; then
      namelen=$(echo $filesys | wc -c)
      echo "$namelen $filesys $size"
    fi
  done |
  sort -n | tail -1 | gawk '{print $3}'
}
#df_file /home/bianca/daten
#exit 0

# disable_persisten_net_rules <old_root> <new_root>
disable_persistent_net_rules()
{
  local old_root
  local new_root
  local d
  local new
  old_root="$1"
  new_root="$2" # maybe $old_root=$new_root
  d=etc/udev/rules.d
  if [ -d $old_root/$d ]; then
    for i in $(cd $old_root/$d; ls *persistent-net.rules 2>/dev/null); do
      new="$(sed "s/^\([^#]\+.*\)/#\1 # disabled by $CMD/" $old_root/$d/$i)"
      mkdir -p $new_root/$d
      echo "$new" > $new_root/$d/$i
    done
  fi
}

# typefun <function> [...]
# returns <function> if <function> exists, or empty
# tested successful with bash, dash, busybox sh, mksh
# will not work with: posh
typefun()
{
  for i in $*; do
    LANG=C type $i 2>&1 | head -1 | sed -n -E "s/^(\S+) .* function$/\1/p"
  done
}
#RES="$(typefun typefun)"
#[ "$RES" ] && echo "OK mytype 1" || echo "ERROR mytype 1"
#RES="$(typefun nonexist)"
#[ ! "$RES" ] && echo "OK mytype 2" || echo "ERROR mytype 2"
#DISK95() { :; }
#DISK9() { :; }
#RES="$(typefun $(seq 0 99 | sed "s/^/DISK/"))"
#EXP="DISK9
#DISK95"
#[ "$RES" = "$EXP" ] && echo "OK mytype 3" || echo "ERROR mytype 3"
#unset -f DISK9
#unset -f DISK95
#exit 0

# cpmod <file1> <file2> # copy chmod of <file1> to <file2>
cpmod()
{
  chmod $(stat -c "%a" $1) $2
}
#a="$(mktemp)"
#b="$(mktemp)"
#chmod 750 $a
#chmod 755 $b
#RES="$(/bin/ls -l $a | cut -c1-10)"
#RES="$RES $(/bin/ls -l $b | cut -c1-10)"
#cpmod $a $b
#RES="$RES $(/bin/ls -l $b | cut -c1-10)"
#EXP="-rwxr-x--- -rwxr-xr-x -rwxr-x---"
#[ "$RES" = "$EXP" ] && echo "OK cpmod-1" || echo "ERROR cpmod-1"
#rm $a $b
#exit 0