Skip to content

Shell Script

References

Shell Scripting Tutorial

Syntax

Array

Handling array in shell script.

    rxPwr=(7 7 7) # declaring an array
    for i in 0 1 2 3
    do
        rxPwr[$i]=$i # access element in array
        echo ${rxPwr[$i]}
    done

Variable handle

    dpd_chan=$1 #Parsing value to variable from argument
    pa_chan=$((dpd_chan+1)) #Adding 1

Loop

For

    for i in 0 1 2 3
    do
        rxPwr[$i]=`dpdctl check $i | grep "rx" | awk {'print $4'}`
        printf "%0.2f, " ${rxPwr[$i]} >> $logdir
    done

While

    while true; do
        # Do something here
    done

Example:

    ADDR=0x43c40000

    while [ $(($ADDR)) -lt $((0x43c00010)) ]; do
        VALUE=`peek $ADDR`
        printf "0x%08x\t0x%08x\n" $ADDR $VALUE >> regdump.txt
        ADDR=$(($ADDR + 4))
    done

Condition state

    if [ "$1" = "all" ]; then
        # Add code here
    fi

Check if a file exist or not

    if [ -f $LOG_MON_FILE ]; then
        # Contents
    fi

printf: print a string with format

    printf "%0.2f, " ${txCpl[$i]} >> $logdir
    printf "0x%08x" $ADDR

Passing Argurments

    $#: number of arguments
    $i: the i-th argument

Operations

  • Bitwise: &, |, ~
    a=0x1
    b=0x2
    c=$(($a | $b)) # or (two operands)
    c=$((($a | $b) & $b)) # more than two operands
    c=$((~$a)) # not (only one operands)
  • Shift:
    a=0x1
    c=$(($a << 2)) # shift left a 2 postition and then assign result to c
  • Relational operators
Back to top