Welcome to the Tiny Core Linux Wiki at tinycorelinux.net!

'/etc/init.d/tc-functions' (source, 20150429)

The scripts in their present state are available at the GIT-repository.

 1  # Command Line, proc might not be mounted
 2  [ -f /proc/cmdline ] || /bb/mount /proc
 3  CMDLINE=""; CMDLINE=" $(/bb/cat /proc/cmdline)"
 4  # ANSI COLORS
 5  CRE="$(echo -e '\r\033[K')"
 6  RED="$(echo -e '\033[1;31m')"
 7  GREEN="$(echo -e '\033[1;32m')"
 8  YELLOW="$(echo -e '\033[1;33m')"
 9  BLUE="$(echo -e '\033[1;34m')"
10  MAGENTA="$(echo -e '\033[1;35m')"
11  CYAN="$(echo -e '\033[1;36m')"
12  WHITE="$(echo -e '\033[1;37m')"
13  NORMAL="$(echo -e '\033[0;39m')"
14  
15  useBusybox(){
16  alias ar="busybox ar"
17  alias awk="busybox awk"
18  alias cat="busybox cat"
19  alias clear="busybox clear"
20  alias cp="busybox cp"
21  alias cpio="busybox cpio"
22  alias dc="busybox dc"
23  alias df="busybox df"
24  alias du="busybox du"
25  alias depmod="busybox depmod"
26  alias expr="busybox expr"
27  alias fdisk="busybox fdisk"
28  alias fold="busybox fold"
29  alias grep="busybox grep"
30  alias gunzip="busybox gunzip"
31  alias hostname="busybox hostname"
32  alias kill="busybox kill"
33  alias killall="busybox killall"
34  alias ls="busybox ls"
35  alias md5sum="busybox md5sum"
36  alias mount="busybox mount"
37  alias sed="busybox sed"
38  alias sort="busybox sort"
39  alias swapoff="busybox swapoff"
40  alias sync="busybox sync"
41  alias tar="busybox tar"
42  alias umount="busybox umount"
43  alias wc="busybox wc"
44  alias wget="busybox wget"
45  alias sudo='sudo '
46  }
47  
48  trim() { echo $1; }
49  
50  stringinfile(){
51  case "$(cat $2)" in *$1*) return 0;; esac
52  return 1
53  }
54  
55  stringinstring(){
56  case "$2" in *$1*) return 0;; esac
57  return 1
58  }
59  
60  getbootparam(){
61  stringinstring " $1=" "$CMDLINE" || return 1
62  result="${CMDLINE##* $1=}"
63  result="${result%%[ 	]*}"
64  echo "$result"
65  return 0
66  }
67  
68  getparam(){
69  stringinstring " $1=" "$2" || return 1
70  result="${2##*$1=}"
71  result="${result%%[ 	]*}"
72  echo "$result"
73  return 0
74  }
75  
76  checkbootparam(){
77  stringinstring " $1" "$CMDLINE"
78  return "$?"
79  }
80  
81  getbasefile(){
82  BASENAME=`basename $1`
83  FIELDS=`echo $BASENAME|awk 'BEGIN{ FS="."} {print NF}'`
84  FIELDS=`busybox expr "$FIELDS" - "$2"`
85  INFO=`echo $BASENAME|cut -f1-$FIELDS -d.`
86  echo $INFO
87  return 0
88  }
89  
90  mounted(){
91  grep $1 /etc/mtab >/dev/null 2>&1
92  if [ $? == 0 ]; then return 0; fi
93  return 1
94  }
95  
96  find_mountpoint() {
97   MOUNTPOINT=""
98   MOUNTED="no"
99   D2="$1"
100   if [ "$D2" == "nfs" ]; then
101      MOUNTPOINT=/mnt/nfs
102      MOUNTED="yes"
103      return
104   fi
105   if [ "${D2:0:5}" == "UUID=" ]; then
106     D2=`/sbin/blkid -lt $D2 -o device`
107     if [ "$?" != 0 ]; then
108       MOUNTPOINT=""
109       return
110     else
111       D2="${D2%%:*}"
112     fi
113   elif [ "${D2:0:6}" == "LABEL=" ]; then
114     D2=`/sbin/blkid -lt $D2 -o device`
115     if [ "$?" != 0 ]; then
116       MOUNTPOINT=""
117       return
118     else
119       D2="${D2%%:*}"
120     fi
121   else
122     D2=/dev/$D2
123   fi
124   MOUNTPOINT="$(grep -i ^$D2\  /etc/mtab|awk '{print $2}'|head -n 1)"
125   if [ -n "$MOUNTPOINT" ]; then
126     MOUNTED="yes"
127     return
128   fi
129   
130  # Special case for virtual disk 
131   if [ "$D2" == "/dev/tcvd" ]; then
132     MOUNTPOINT="$(awk '/\/mnt\/tcvd/{print $2}' /etc/mtab|head -n 1)"
133     if [ -n "$MOUNTPOINT" ]; then
134       MOUNTED="yes"
135       return
136     fi
137   fi
138  
139   MOUNTPOINT="$(grep -i ^$D2\  /etc/fstab|awk '{print $2}'|head -n 1)"
140  }
141  
142  autoscan(){
143  FOUND=""
144  for DEVICE in `autoscan-devices`; do
145     find_mountpoint $DEVICE
146     if [ -n "$MOUNTPOINT" ]; then
147       if [ "$MOUNTED" == "no" ]; then
148         mount "$MOUNTPOINT" >/dev/null 2>&1
149       fi
150       if [ "-$2" "$MOUNTPOINT"/$1 ]; then
151         FOUND="yes"
152       fi
153       if [ "$MOUNTED" == "no" ]; then
154         umount "$MOUNTPOINT" >/dev/null 2>&1
155       fi
156       if [ -n "$FOUND" ]; then 
157         echo "$DEVICE"
158         return 0
159       fi
160     fi
161  done
162  DEVICE=""
163  return 1
164  }
165  
166  getpasswd(){
167    readpassword(){
168      PASSWD=""
169      until [ ${#PASSWD} -ge 8 ] && [ ${#PASSWD} -le 56 ]; do
170        PASSWD=""
171        CH="."
172        if [ "$2" == "confirm" ]; then
173          echo -n "${BLUE} Re-enter${NORMAL}: "
174        else
175          echo -n "${BLUE}Enter password (8 to 56 characters) for ${YELLOW}$1${NORMAL}: "
176        fi
177        while [ "$CH" != "" ]; do
178          read -s -n 1 CH
179          if [ "$CH" == "" -a ${#PASSWD} -gt 0 ]; then
180            echo -n -e "\b \b"
181          elif [ "$CH" != "" ]; then
182            PASSWD="$PASSWD$CH"
183            if [ "$CH" != "" ]; then echo -n "*"; fi
184          fi
185        done
186        [ ${#PASSWD} -lt 8 ] && echo " Password is too short!"
187        [ ${#PASSWD} -gt 56 ] && echo " Password is too long!"
188      done
189    }
190    OK=0
191    until [ "$OK" == 1 ]; do
192      readpassword $1
193      PASSWD1=$PASSWD
194      readpassword $1 confirm
195      if [ "$PASSWD1" == "$PASSWD" ]; then
196        OK=1
197        echo " ${GREEN}Accepted.${NORMAL}"
198      else
199        echo " ${RED}Mismatch.${NORMAL}"
200      fi
201    done
202    return 0
203  }
204  
205  status() {
206    local CHECK=$?
207    echo -en "\\033[70G[ "
208    if [ $CHECK = 0 ]; then
209      echo -en "\\033[1;33mOK"
210    else
211      echo -en "\\033[1;31mFailed"
212    fi
213    echo -e "\\033[0;39m ]"
214  }
215  
216  usleep_progress() {
217  # Wait 2 seconds
218    CHAR='.'
219    for i in `seq 1 79`
220    do
221      echo -n "$CHAR"
222      usleep 25316
223    done
224    echo "$CHAR"
225  }
226  
227  checkroot() {
228   if [ `/bb/id -u` -ne 0 ]; then
229     echo "Need root privileges." >&2
230     exit 1
231   fi
232  }
233   
234  checknotroot() {
235   if [ `/bb/id -u` -eq 0 ]; then
236     echo "Don't run this as root." >&2
237     exit 1
238   fi
239  }
240  
241  checkX() {
242   if [ -z ${DISPLAY} ]; then
243     echo "Requires X windows." >&2
244     exit 1
245   fi
246  }
247  
248  setupHome(){
249     read USER < /etc/sysconfig/tcuser
250     if [ ! -d /home/"$USER" ] || [ "`ls -la /home/${USER} | wc -l`" -le 3 ]
251     then
252        mkdir -p /home/"$USER"/.X.d
253        mkdir -p /home/"$USER"/.local/bin
254        chown -R "$USER".staff /home/"$USER/"
255        chmod -R g+s,o-rwx /home/"$USER/"
256     fi
257     LIST=`mktemp`
258     > "$LIST"
259     for S in `find /etc/skel -type f`
260     do
261  	ITEM="${S#/etc/skel/}"
262  	if [ ! -e /home/"$USER"/"$ITEM" ] 
263  	then
264  		echo "$ITEM" >> "$LIST"
265  	fi
266     done
267     if [ -s "$LIST" ]
268     then
269        tar -C /etc/skel -T "$LIST" -cf - | tar -C /home/"$USER" -xf -
270        while read ITEM
271  	do chown -R "$USER".staff /home/"$USER"/"${ITEM%%/*}"
272        done < "$LIST"
273     fi
274     rm "$LIST" 2>/dev/null
275  }
276  
277  merge() {
278  awk -v mergedata="$1" -v target="$3" '
279  {
280    if ( index($0,target) ) 
281    {
282       while (( getline item < mergedata ) > 0 )
283         print item
284       close(mergedata)
285    }
286    print $0
287  } ' "$2"
288  }
289  
290  replace() {                    
291  awk -v mergedata="$1" -v target="$3" '
292  {                                     
293    if ( index($0,target) )       
294    {                      
295       while (( getline item < mergedata ) > 0 )
296         print item                             
297       close(mergedata)                    
298    } else print $0    
299  } ' "$2"         
300  }
301  
302  purge(){
303  awk -v startTarget="$2" -v endTarget="$3" '
304  BEGIN { writeFlag=1 }
305  {
306    if (index($0, startTarget))
307    {
308      print $0 
309      writeFlag=0
310    } else
311      if (index($0, endTarget)) writeFlag=1
312    
313    if (writeFlag) print $0
314  } ' "$1"
315  }
316  
317  getMajorVer() {
318  awk '{printf "%d", $1}' /usr/share/doc/tc/release.txt 
319  }
320  
321  
322  getBuild() {
323  BUILD=`uname -m`
324  case ${BUILD} in
325  	armv6l) echo "armv6" ;;
326  	armv7l) echo "armv7" ;;
327  	i686)   echo "x86" ;;
328  	x86_64) [ -f /lib/ld-linux-x86-64.so.2 ] && echo "x86_64" || echo "x86" ;;
329  	*)      echo "x86" ;;
330  esac
331  }
332  
333  getMirror() {
334  BUILD="$1"
335  [ -z "$BUILD" ] && BUILD=$(getBuild)
336  read MIRROR < /opt/tcemirror
337  MIRROR="${MIRROR%/}/dCore/$BUILD"
338  }
339  
340  installed() {
341    if [ -e /usr/local/tce.installed/${1%.*} ]; then 
342      return 0
343    else 
344      return 1
345    fi
346  }  
347  
348  getKeyEventDevice() {
349  e=0
350  for i in /sys/class/input/input*/name; do
351    if grep -q "eyboard" $i; then break; fi 
352    e=`busybox expr "$e" + 1`
353  done
354  [ $e -gt 0 ] || exit 1
355  echo /dev/input/event$e
356  }
357  
358  parentOf()
359  {
360  	PID=$(pidof $1) || return
361  	PPID=$(awk '/^PPid:/{print $2}' /proc/$PID/status)
362  	awk '/^Name:/{print $2}' /proc/$PPID/status
363  }
364  
365  myParent()
366  {
367  	PID=$$
368  	PPID=$(awk '/^PPid:/{print $2}' /proc/$PID/status)
369  	awk '/^Name:/{print $2}' /proc/$PPID/status
370  }
371  
372  launchApp() {
373  	FREEDESKTOP=/usr/share/applications
374  	[ -e "$FREEDESKTOP"/"$1".desktop ] || FREEDESKTOP=/usr/local/share/applications
375  	if [ -e "$FREEDESKTOP"/"$1".desktop ]
376  	then
377  		E=`awk 'BEGIN{FS="="}/^Exec/{print $2}' "$FREEDESKTOP"/"$1".desktop`
378   		E="${E% \%*}"
379   		shift 1
380  		exec ${E} $@
381  	fi
382  }
Print/export