Understand the bash scripting to use in the day-to-day life of the developer.

Introduction

To find the bash version:

bash --version

There are three I/Os to consider

  • standard input
  • standard output
  • standard error

Using I/O redirect, redirect output to a file :

ls -l t1.json > out

The error message has to be redirect as follows:

ls -l not.available 2> err

To send standard output to the sout and standard error to the serror:

ls -l t1.json not.available > sout 2> serror

To redirect both to the same file

ls -l t1.json not.available &> sout
or
ls -l t1.json not.available > sout 2>&1

The second line says to redirect the error to the same as file descriptor 1.

Files and Directories

To create multiple directories:

mkdir {a..c}

to create multiple files in that directories:

touch {a..c}/{1..2}

The output is

.
├── a
│   ├── 1
│   └── 2
├── b
│   ├── 1
│   └── 2
└── c
    ├── 1
    └── 2

To find the files

To get to 2nd depth to find png files:

find . -maxdepth 3 -iname '*.png' -type f

To find the size

find . -maxdepth 2 -iname '*.png' -type f | xargs du -hsc

Env variables

For example concat:

x=2 ; echo $x+1

But to add 1:

echo $((x+1))

However, the following shows space for \n:

x=abc$'\n'def ; printf $x

using printf

printf "%s\n"  I am 'Ojitha Hewa'

Output is

I
am
Ojitha Hewa

To add and print x and y ends:

x=1; y=2; printf "sum of $x + $y is %d\n" "$(( x + y ))"

image-20221030112329586

Call the function

func() { printf "%d\n" "$(( $1 + $2 ))"; } ; printf 'call func %s\n' "$(func 2 3)"

image-20221030113645932

Arrys

Define the array in the bash shell:

typeset -a arr=( 1 2 3 )

Iterate over the array

for e in ${arr[@]}; do printf '%d\n' "$e" ; done

define associative array

typeset -A assoc=( a 5 b 6 c 7 d 8 )

You can access elements directly by the key

echo ${assoc[a]} # 5

To iterate over the associated array:

for k  in ${!assoc[@]}; do printf '[%s]=%d\n' $k ${assoc[$k]} ; done

image-20221030122029641

Seq

Here the simple example for seq

seq 1 10

and

seq 1 10 | while read line; do  printf '%d\n' $((line)); done

use with sed regular expressions:

seq 1 10 |  sed -r 's/^/# /'

output is

# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# 10