logoISU  

CS469/569 - Linux and Unix Administration and Networking

Spring 2022

Bash Script Examples

n x n box:

``` function box() { declare -i n=$1 r c; for ((r=0; r

n row high pyramid:

``` function pyramid() { declare -i n=$1 r c s a; let s=n-1; let a=1; for((r=0; r

params.sh

``` #!/bin/bash declare -i pp=1; for p; do echo "$pp: $p"; let pp++; done ```
  • Use: chmod a+rx params.sh to make executable.

sizes.sh

``` #!/bin/bash let total=0; # Always quote variables that may contain a filename as filenames may contain # spaces. # Iterates over the positional parameters, placing their values in f one at a # time: for f; do let size=$(stat -c%s "$f"); printf "%12d %s\n" $size "$f"; let total+=size; done printf "Total = %d\n" $total; ```

draw.sh

``` #!/bin/bash read rows cols < <(stty size); let row=rows/2; let col=cols/2; let pen=0; tput clear; while true do tput cup $row $col; if (( pen )); then echo -n -e "#\b"; else echo -n -e " \b"; fi read -n 1 -s key; case $key in [wW]) if (( row )); then let row--; fi ;; [sS]) if (( row < rows-1 )); then let row++; fi ;; [aA]) if (( col )); then let col--; fi ;; [dD]) if (( col < cols-1 )); then let col++; fi ;; [qQ]) break ;; *) let pen^=1 ;; esac done ```