Shell Bsh / Programmierung
 
StartSeite | ShellBsh/ | Neues | TestSeite | ForumSeite | Teilnehmer | Kategorien | Index | Hilfe | Einstellungen | Ändern

Beispiele: bsh-Scripts

Permanente Überwachung der aktuellen lokalen dynamischen WAN-IP
(DSL-Router Netgear RT314)

 
#!/home/bin/bsh

if [ "$1" == rdip ]
then
HTM1='
'
HTM2='
'
HTM3='
'
Ftp="open schellong.de
user uuuuuuuu ssssssss
binary
put /tmp/wanhtm /klb/server.html
quit
"
catv Ftp > /tmp/wanftp
Ip=000000000000000
ip=000000000000000
tm=-500
st=01
   while sleep $st
         catv 0 =ip: < /tmp/wanip
         [ "$ip" != x ]
   do
      ifset ip || continue
      st=10
      let "$SECONDS-tm>300" && {
         tm=$SECONDS
         ping www.t-online.de > /dev/null
      }
      cmpv ip Ip && continue
      Ip=$ip
      expr "$HTM2" :Hbuf '999.888.777.666' += "$ip"
      > /tmp/wanhtm
      catv HTM1 Hbuf
      [ -s /tmp/wanipa ] && cat /tmp/wanipa
      catv HTM3
      ><
      ftp -n < /tmp/wanftp
   done
   exit 0
fi


if [ "$1" == rdtm ]
then
Ip=++++++++++++++++++++
Ip0=++++++++++++++++++++
ip=000000000000000
ipne=000000000000000
size=0000000000
size0=0000000000
neq=0000
tne=0000
nstart=0000
offs=000000
   while fstat -sv size /tmp/wanout
         [ size -lt 3500 ]
   do
      [ size -gt offs ] && catv $offs,$((size-offs)),0  < /tmp/wanout
      offs=$size
      let "++nstart>100" && exit 0
      sleep -m 400
   done
   ipne=0 nstart=0
   while :
   do
      [ $ipne != 0 ] && let "$SECONDS-tne>=15||nstart==0" && {
         Ip0=$ipne
         echo $ipne%c > /tmp/wanip
         echo $ipne >> /tmp/wanipa
         ipne=0 nstart=1
      }
      fstat -sv size /tmp/wanout
      [ size -gt offs ] && catv $offs,$((size-offs)),0  < /tmp/wanout
      offs=$size
      [ size -eq size0 ] && {
         let "++neq>2" && exit 0
         sleep 4
         continue
      }
      neq=0 size0=0 offs=0
      tr '\27' '\10' < /tmp/wanout > /tmp/wanz
      : > /tmp/wanout
      ip=
      < /tmp/wanz
      while readl Z
      do
         expr "$Z" :: '^%[11;[234][0-9]H[0-9. ]%{1,}' || continue
         expr "$Z" :col '^....%(..%)H' || continue
         let "col<24||col>41" && continue
         let "col-=24"
         expr "$Z" :ip 'H%([0-9. ]%{1,}%)'
         catv ip =$col,,Ip
      done
      ><
      ifset ip || continue
      expr "$Ip" :ip %
         '%([0-9]%{1,3}%.[0-9]%{1,3}%.[0-9]%{1,3}%.[0-9]%{1,3}%)+*$' || continue
      cmpv ip Ip0 && continue
      ipne=$ip tne=$SECONDS
   done
fi

#^[[11;24..41H
#[ ...]ip_addr(-teil)


: > /tmp/wanout
: > /tmp/wanip
dsl.bsh rdip dsl.bsh rdtm Pid=$!
telnet dsl >> /tmp/wanout
#kill $Pid
echo x%c > /tmp/wanip
wait

exit 0 


Erzeugt vertikale Durchnumerierungen durch Ersetzen:
Im vi-Editor:
 :12,34 !ninc.bsh 1 4 #
#!/home/bin/bsh

start=0
inc=1
muster='@'
width=2
fill=' '

[ -t 0 ] &| expr "$1" :: '[^0-9-]' && {
   print -u2 ninc.bsh: usage: start inc muster width fillchar
   exit 0
}

for 2 nam val in start "$1" inc "$2" muster "$3" width "$4" fill "$5"
do
   ifset val || continue
   $nam="$val"
done

cat | {
   while readl Z
   do
      prints "vsf$fill$width" subst "$start"
      expr "$Z" :Z "$muster" = "$subst" && let "start+=inc"
      catv Z /%j
   done
}

exit 0

Zum Thema Mikrokontroller-Praxis:

Die folgenden beiden Dateien

495.asm
.PROGRAM 495
.TITLE   495
.section IOBASE, IO, locate=0x0000


.GLOBAL __pdr0,     __pdr1,     __pdr2,     __pdr3,     __pdr4      
.GLOBAL __pdr5,     __pdr6,     __ddr0,     __ddr1,     __ddr2      
.GLOBAL __ddr3,     __ddr4,     __ddr5,     __ddr6,     __ader      
.GLOBAL __smr0,     __scr0,     __sidr0,    __sodr0,    __ssr0      
.GLOBAL __cdcr0,    __ses0,     __smr1,     __scr1,     __sidr1     
.GLOBAL __sodr1,    __ssr1,     __cdcr1,    __enir,     __eirr      
.GLOBAL __elvr0,    __elvr1,    __elvr,     __adcs0,    __adcs1     
.GLOBAL __adcs,     __adcr0,    __adcr1,    __adcr,     __ppgc0     
.GLOBAL __ppgc1,    __ppgc01,   __ppg01,    __ppgc2,    __ppgc3     
.GLOBAL __ppgc23,   __ppg23,    __ipcp0,    __ipcp1,    __ics01     
.GLOBAL __ics23,    __tcdt,     __tccs,     __tccsh,    __ipcp2     
.GLOBAL __ipcp3,    __tmcsr0,   __tmcsr1,   __romm,     __bvalr     
.GLOBAL __treqr,    __tcanr,    __tcr,      __rcr,      __rrtrr     
.GLOBAL __rovrr,    __rier,     __pacsr,    __dirr,     __lpmcr     
.GLOBAL __ckscr,    __arsr,     __hacr,     __ecsr,     __wdtc      
.GLOBAL __tbtc,     __wtc,      __fmcs,     __icr,      __tmr0      

__pdr0      .res.b 1
__pdr1      .res.b 1
__pdr2      .res.b 1
__pdr3      .res.b 1
__pdr4      .res.b 1
__pdr5      .res.b 1
__pdr6      .res.b 1
.org 0x0010
__ddr0      .res.b 1
__ddr1      .res.b 1
__ddr2      .res.b 1
;....................
.org 0x00b0
__icr       .res.b 16


.section IOXTND, DATA, locate=0x1FF0


.GLOBAL __tmrl0,    __tmr1,     __tmrl1,    __prll0,    __prlh0     
.GLOBAL __prl0,     __prll1,    __prlh1,    __prl1,     __prll2     
.GLOBAL __prlh2,    __prl2,     __prll3,    __prlh3,    __prl3      
.GLOBAL __idr,      __idrx,     __dlcr,     __dtr,      __dtr_word  
.GLOBAL __dtr_byte, __dtr_dword,__csr,      __leir,     __rec       
.GLOBAL __tec,      __rtec,     __btr,      __ider,     __trtrr     
.GLOBAL __rfwtr,    __tier,     __amsr,     __amr0,     __amr1      
.GLOBAL ___endio                                                    

.org 0x3900
__tmr0      .res.b 2
.org 0x3900
__tmrl0     .res.b 2
__tmr1      .res.b 2
.org 0x3c40
__dtr_byte  .res.b 64
.org 0x3c40
__dtr_dword .res.b 64
.org 0x3d00
;....................
___endio    .res.b 1
.end

495.h
.................
__IO_EXTENDED            IO_LWORD  _idrx[8];
#define                   IDRX(a)  _idrx[a]

typedef union {
   IO_WORD word;
   IO_BYTE byte;
   struct {
         IO_BYTE   DLC0:1;
         IO_BYTE   DLC1:1;
         IO_BYTE   DLC2:1;
         IO_BYTE   DLC3:1;
         IO_BYTE       :1;
         IO_BYTE       :1;
         IO_BYTE       :1;
         IO_BYTE       :1;
   } bit;
   struct {
         IO_BYTE    DLC:4;
         IO_BYTE       :4;
   } bitc;
} DLCR_t;
__IO_EXTENDED          DLCR_t  _dlcr[8];
#define               DLCR0    _dlcr[0].byte
#define  DLCR0_DLC0        _dlcr[0].bit.DLC0
#define  DLCR0_DLC1        _dlcr[0].bit.DLC1
#define  DLCR0_DLC2        _dlcr[0].bit.DLC2
#define  DLCR0_DLC3        _dlcr[0].bit.DLC3
#define  DLCR0_DLC         _dlcr[0].bitc.DLC

#define               DLCR1    _dlcr[1].byte
#define  DLCR1_DLC0        _dlcr[1].bit.DLC0
........................

__IO_EXTENDED             IO_WORD  _dtr[8][4];
#define                  DTR(a,b)  _dtr[a][b]

__IO_EXTENDED             IO_WORD  _dtr_word[8][4];
#define             DTR_WORD(a,b)  _dtr_word[a][b]

__IO_EXTENDED             IO_BYTE  _dtr_byte[8][8];
#define             DTR_BYTE(a,b)  _dtr_byte[a][b]

__IO_EXTENDED            IO_LWORD  _dtr_dword[8][2];
#define            DTR_DWORD(a,b)  _dtr_dword[a][b]

__IO_EXTENDED       IO_WORD  _csr;
#define                 CSR  _csr

typedef union {
   IO_BYTE byte;
   struct {
         IO_BYTE   MBP0:1;
         IO_BYTE   MBP1:1;
         IO_BYTE   MBP2:1;
         IO_BYTE       :1;
         IO_BYTE       :1;
         IO_BYTE    RCE:1;
         IO_BYTE    TCE:1;
         IO_BYTE    NTE:1;
   } bit;
   struct {
         IO_BYTE    MBP:3;
         IO_BYTE       :5;
   } bitc;
} LEIR_t;
__IO_EXTENDED          LEIR_t  _leir;
#define                LEIR    _leir.byte
#define  LEIR_MBP0         _leir.bit.MBP0
#define  LEIR_MBP1         _leir.bit.MBP1
#define  LEIR_MBP2         _leir.bit.MBP2
#define  LEIR_RCE          _leir.bit.RCE
#define  LEIR_TCE          _leir.bit.TCE
#define  LEIR_NTE          _leir.bit.NTE
#define  LEIR_MBP          _leir.bitc.MBP

__IO_EXTENDED       IO_BYTE  _rec;
#define                 REC  _rec
............................
__IO_EXTENDED       IO_BYTE  __endio;
#define              _ENDIO  __endio

#endif
bis > 100KB

wurden hieraus erzeugt (IO-Map):
0       pdr0 p00 p01 p02 p03 p04 p05 p06 p07
+       pdr1    p10 p11 p12 p13 p14 p15 p16 p17
...................
3914    prl2 2
+       prll3
+       prlh3
3916    prl3 2
3c10    idr 4:2 (8)
3c10    idrx 4 (8)
3c30    dlcr 2:1 8 dlc0 dlc1 dlc2 dlc3 . . . .
3c40    dtr 2 (8,4)
3c40    dtr_word 2 (8,4)
3c40    dtr_byte 1 (8,8)
3c40    dtr_dword 4 (8,2)
...................
3fff    _endio

mit dem folgenden bsh-Script:
 
#!/u/bin/bsh
#!/usr/bin/bsh
#sc/20.04.01
#nur bsh_VV


[ -s "$1" ] || { print -u2 "Keine (gueltige) Datei: '$1'"; exit 1; }
set -f

asm1='

.PROGRAM ~nameteil~
.TITLE   ~nameteil~
.section IOBASE, IO, locate=0x0000

'
asm2='

.section IOXTND, DATA, locate=0x1FF0

'
h1='
#ifndef   __MB90XXX_H
........................
'
zeile="$h1"

mktemp Tmpf1
mktemp Tmpf2
mktemp Tmpf3


Err()  {
   [ $# -gt 0 ] && print -u2 "ash.bsh: ERROR: '$*'"
   remove $Tmpf1 $Tmpf2 $Tmpf3
   [ $# -eq 0 ] && exit 0
   exit 1
   return
}


PrGlob()  {
   local n=1 v=.. g1 g2 g3 g4 g5
   echo >> $Tmpf1
   for 5 g1 g2 g3 g4 g5 in $Glob
   do
      conv -l g1 g2 g3 g4 g5
      for n from 1 to 5 repeat
      do
         v=g$n
         ifset g$n && g$n="__${{v}"
         ifset g$((n+1)) && g$n="${{v}, "
      done
      prints ss-12s-12s-12s-12s-12 ".GLOBAL " %
             "$g1" "$g2" "$g3" "$g4" "$g5" >> $Tmpf1
   done
   echo >> $Tmpf1
   Glob=''
   return
}


MkBitF()  {
   local bf=,,,,,,,, bfc0=,,,,,,,, bfc=,,,,,,,,
   local b=00 b2=00 d=00 d0=00
   BitFc='' bfc0=''
   for bf in $BitF
   do
      let ++b
      expr "$bf" :bfc '^%(..*%)%D$' && expr "$bf" :d '%(%D%)$' && {
         let "d-d0==1&&(!b2&&!d0||b2)" && [ "$bfc0" == "$bfc" ] && { d0=$d
            let "b2==0" && { b2=1
               let "(b-=2)>0" && BitFc="$BitFc . $b"
               b=2
            }
            continue
         }
         let b2 && BitFc="$BitFc $bfc0 $((b-1))"  b=1
         bfc0=$bfc d0=$d b2=0
         continue
      }
      let b2 && BitFc="$BitFc $bfc0 $((b-1))"  b=1
      bfc0='' d0=0 b2=0
   done
   ifset BitFc && {
      let b2 && BitFc="$BitFc $bfc0 $b"
      let !b2 && BitFc="$BitFc . $b"
      return 0
   }
   return 1
}



expr "$1" :ifn '^%(..*%)%.[^.]%{1,}$' || ifn="$1"
expr "$asm1" :asm1 '~nameteil~' += "$ifn"
catv asm1 >> $Tmpf1
catv h1 >> $Tmpf3
adr=00000000
siz=00000001
siz2=00000000
arr=00000000
arr1=00000000
arr2=00000000
nam=..........
namu=..........
nam0=............
org=........
extrn=...........................
typ=................
typ2=................
typ20=................
typ21=................
type=................
prints vs2500 Glob
prints vs300 BitF
prints vs300 BitFc
Glob=''
nbitf=000 n=00000000 rb=000

local adrlim=00000255 mk=..............  ak=..............
local ab=..............  ar=.............. s=00000000


PrBitFields()  {
   local nn="$1" nk="$2" typ="$3"
   local n=.......... s=00
   [ -n "$nn" ] && let "arr>=10&&nn<10" && nn=0$nn
   prints s-17s10s "#define" "$namu$nn" "    _$nam$nk.$typ"
   {  #nbitf
      for n in $BitF
      do
         expr "$n" :: '[.-]' && continue
         prints s-9s-16s "#define" $namu${nn}_$n "  _$nam$nk.bit.$n"
      done
   }
   for 2 n s in $BitFc
   do
      expr "$n" :: '[.-]' && continue
      prints s-9s-16s "#define" $namu${nn}_$n "  _$nam$nk.bitc.$n"
   done
   echo
}


GetZeile()  {
   local z='                                          '
   while readl zeile
   do
      ifset zeile || continue
      expr "$zeile" :: '%W' || continue
      while expr "$zeile" :zeile '^%(..*%)[ %t]*+%{1,}$'
      do
         readl z || break
         zeile="$zeile $z"
      done
      return 0
   done
   return 1
}


DataFromZeile()  {
   local cas=adr w=.....................
   for w in $zeile
   do
      case "$cas" in
        adr)  cas=nam
              expr "$w" :: '^+$' && continue
              expr "$w" :: '^[0-9][0-9a-fA-F]*$' && {
                 let "16#$w==adr" && continue
                 let "16#$w<adr" && let "siz=adr-16#$w"
                 let "adr=16#$w"
                 org=$w
                 continue
              }
              ;,
        nam)  expr "$w" :: '^[a-zA-Z_]%W*$' || Err "$w"
              cas=siz
              nam=$w
              conv -l nam
              [ "$nam" == "$nam0" ] && Err "Doppelt: $nam"
              nam0="$nam"
              Glob="$Glob $w"
              ;;
        siz)  cas=arr
              expr "$w" :: '^%D$' && { siz=$w ; continue; }
              expr "$w" :siz '^%(%D%):%D$' &                 expr "$w" :siz2 '^%D:%(%D%)$' && continue
              ;,
        arr)  cas=bit
              expr "$w" :arr '^%(%D%{1,2}%)' &                 expr "$w" :w '^%D%{1,2}%(.*%)$'
              expr "$w" :arr1 '^(%(%D%{1,2}%))$' && continue
              expr "$w" :: '^(%D%{1,2},%D%{1,2})$' && {
                 expr "$w" :arr1 '%(%D%{1,}%)'
                 expr "$w" :arr2 ',%(%D%{1,}%))$'
                 continue
              }
              [ "$arr" != 0000 ] && continue
              ;,
        bit)  expr "$w" :: '^[-.a-zA-Z_]%W*$' || Err "$w"
              expr "$w" :: '^[-.].' && Err "$w"
              conv -u w
              BitF="$BitF $w"
              let ++nbitf
              ;;
          *)  Err Interner Fehler ;;
      esac
   done
   [ "$cas" == adr -o "$cas" == nam ] && Err "$zeile"
   [ "$cas" == bit ] &      let "nbitf>0&&(siz2==0&&nbitf!=siz*8||siz2!=0&&nbitf!=siz2*8)" &         Err "Anzahl:$BitF"
   return
}


< "$1"
while GetZeile
do
   BitF='' org=''
   nbitf=000 arr=0000 arr1=00 arr2=00
   siz=1 siz2=0
   DataFromZeile

   let "siz!=1&&siz!=2&&siz!=4" && Err "sz==$siz"
   let "siz2!=0&&siz2!=1&&siz2!=2&&siz2!=4" && Err ":sz==$siz2"
   let "siz2>=siz" && Err "sz:sz $siz<=$siz2"
   let "adrlim>0&&adr>adrlim" && { adrlim=0
      PrGlob
      cat $Tmpf2 >> $Tmpf1
      : > $Tmpf2
      catv asm2 >> $Tmpf1
   }
   ifset org && prints sbssf04 .org 0x $org >> $Tmpf2
   s=$siz
   let arr  && let "s*=arr"
   let arr1 && let "s*=arr1"
   let arr2 && let "s*=arr2"
   prints s-12sbs __$nam .res.b $s >> $Tmpf2
   let "adr+=s"

   extrn="__IO_EXTERN __io"
   let "adr-s>255" && extrn="__IO_EXTENDED   "
   let siz==1 && typ=IO_BYTE typ2=byte
   let siz==2 && typ=IO_WORD typ2=word
   let siz==4 && typ=IO_LWORD typ2=dword
   let siz2==1 && typ20=IO_BYTE typ21=byte
   let siz2==2 && typ20=IO_WORD typ21=word
   let siz2==4 && typ20=IO_LWORD typ21=dword
   ar= mk= ak= ab= rb=10
   let "arr"  && ar="[$arr]"
   let "arr1" && mk="(a)" ak="[$arr1]" ab="[a]" rb=16
   let "arr2" && mk="(a,b)" ak="[$arr1][$arr2]" ab="[a][b]" rb=16
   namu="$nam"
   conv -u namu
   >> $Tmpf3
   let "nbitf==0&&siz2==0" && {
      prints s-17s${rb}sss "$extrn" $typ "  _$nam" "$ar$ak" ";"
      let "!arr" && {
         prints s-17s${rb}ssn "#define" $namu$mk "  _$nam" "$ab"
         >< ; continue
      }
      for n from 0 to 0$((arr-1)) repeat
      do
         prints s-17s${rb}ss "#define" $namu$n$mk "  _$nam" "[$n]$ab"
      done
      echo
      >< ; continue
   }
   let "nbitf==0&&siz2!=0" && {
      echo "typedef union {"
      echo "   $typ $typ2;"
      echo "   $typ20 $typ21;"
      echo "} ${namu}_t;"
      prints s-17s${rb}sss "$extrn" ${namu}_t "  _$nam" "$ar$ak" ";"
      let "!arr" && {
         prints s-17s${rb}sn "#define" $namu$mk "  _$nam$ab.$typ21"
         >< ; continue
      }
      for n from 0 to 0$((arr-1)) repeat
      do
         prints s-17s${rb}s "#define" $namu$n$mk "  _${nam}[$n]$ab.$typ21"
      done
      echo
      >< ; continue
   }
   let arr1 && Err "Operation nicht moeglich: $ar$ak"
   type=$typ
   echo "typedef union {"
   echo "   $typ $typ2;"
   let siz2 && type=$typ20 && echo "   $typ20 $typ21;"
   {  #nbitf
      echo "   struct {"
      for n in $BitF
      do
         expr "$n" :: '[.-]' && n=''
         prints ss10s7s "      " $type "$n" ":1;"
      done
      echo "   } bit;"
   }
   MkBitF && {
      echo "   struct {"
      for 2 n s in $BitFc
      do
         expr "$n" :: '[.-]' && n=''
         prints ss10s7s "      " $type "$n" ":$s;"
      done
      echo "   } bitc;"
   }
   echo "} ${namu}_t;"
   prints s-17s12sss "$extrn" ${namu}_t "  _$nam" "$ar" ";"
   type=$typ2
   let siz2 && type=$typ21
   let  arr && {
      for n from 0 to 0$((arr-1)) repeat
      do
         PrBitFields $n "[$n]" $type
      done
   }
   let !arr && PrBitFields "" "" $type
   ><
done
><


echo ".end%n" >> $Tmpf2
PrGlob
cat $Tmpf2 >> $Tmpf1
cat $Tmpf1 > $ifn.asm

echo "#endif%n" >> $Tmpf3
cat $Tmpf3 > $ifn.h


Err
exit 0 
Die Variablen-Defaults sind meist redundant.

Die Generierung beider Dateien dauert etwa 0.5s, weshalb dieses Problem sinnvoll mit bsh gelöst ist, anstatt mit C oder einer anderen Compiler-Sprache.


--HelmutSchellong


KategorieProgrammierung KategorieSchellong
StartSeite | ShellBsh/ | Neues | TestSeite | ForumSeite | Teilnehmer | Kategorien | Index | Hilfe | Einstellungen | Ändern
Text dieser Seite ändern (zuletzt geändert: 7. September 2003 18:14 (diff))
Suchbegriff: gesucht wird
im Titel
im Text