Set of small scripts, which demonstrate some features of bash programming.

1
2
3
4
5
6
7
8
//===================================================================================
// set new prompt
//===================================================================================

PS1=">"
PS1="[${LOGNAME}@$(hostname)] # "
PS1="[${LOGNAME}] # "
PS1='$PWD $ '
1
2
3
4
5
6
7
8
9
//===================================================================================
// set (and automatically create) shell variable
//===================================================================================

$ test="test"

$ homedir='pwd'

string="The man said \" hello \"."
1
2
3
4
5
//===================================================================================
// To use the variable within the shell, it is preceded by a $
//===================================================================================
homedir=$HOME
cd $homedir
1
2
3
4
5
6
7
8
9
//===================================================================================
// how to print shell variable
//===================================================================================

echo PS1

echo $PS1

echo $USERNAME
1
2
3
4
5
6
7
8
9
10
//===================================================================================
// predefined shell variables
//===================================================================================
HOME    name of users login directory
IFS     internal field separators
PATH    search path used for finding commands
PS1     shell prompt
OSTYPE
USERNAME
SHELL 
1
2
3
4
5
6
7
//===================================================================================
// The shell supports pattern matching
//===================================================================================
* Match all characters in a string 
? Match a single character

ls *.dat
1
2
3
4
//===================================================================================
// Command Substitution
//===================================================================================
today=`date`
1
2
3
4
5
//===================================================================================
#---------------------------------------------------
# empty sh script program
#---------------------------------------------------
#!/bin/sh
1
2
3
4
5
#---------------------------------------------------
# comments
#---------------------------------------------------
#!/bin/sh
# this is comment
1
2
3
4
5
6
7
#---------------------------------------------------
# printing of string constant
#---------------------------------------------------
#!/bin/sh
echo 'hello'
echo "hello"
echo hello
1
2
3
4
5
6
7
8
#---------------------------------------------------
# declaration and printing of string variable
#---------------------------------------------------
#!/bin/sh

x='Wonderful new World'
echo $x
echo x # just string 'x'
1
2
3
4
5
#---------------------------------------------------
# call of other programs
#---------------------------------------------------
#!/bin/sh
ls
1
2
3
4
5
6
7
8
9
10
#---------------------------------------------------
# indirect call of other programs
#---------------------------------------------------
#!/bin/sh

x='pwd'
$x

x='ls -l'
$x
1
2
3
4
5
6
7
8
9
#---------------------------------------------------
# indirect call with indirect parameters
#---------------------------------------------------
#!/bin/sh
u='-l'

x='ls '

$x $u
1
2
3
4
5
6
#---------------------------------------------------
# print current shell name ???
#---------------------------------------------------
#!/bin/sh

echo $SHELL
1
2
3
4
5
6
7
8
9
10
#---------------------------------------------------
# Anything enclosed in double quotes is passed on exactly 
# as presented with the exception that the values of 
# shell variables are substituted
#---------------------------------------------------
#!/bin/sh

v1="abc "
v2="$v1 d"
echo $v1 $v2
1
2
3
4
5
6
7
8
9
10
#---------------------------------------------------
# Any matter enclosed in single quotes is passed on 
# exactly as presented. The values of shell variables 
# are not substituted. 
#---------------------------------------------------
#!/bin/sh

v1="abc"
v2='$v1 d'
echo $v1 $v2
1
2
3
4
5
6
7
8
9
10
#---------------------------------------------------
# Back quotes are used to enclose commands. An item
# enclosed in back quotes is replaced by the standard
# output of the command. Shell variable values
# are substituted within back quotes.
#---------------------------------------------------
#!/bin/sh

date=`date`
echo the date is $date
1
2
3
4
5
6
7
8
#---------------------------------------------------
# escaping
#---------------------------------------------------
#!/bin/sh

msg=`echo Your Current Directory is \`pwd\``

echo $msg
1
2
3
4
5
6
7
8
9
#---------------------------------------------------
# reading of text line from keyboard
#---------------------------------------------------
#!/bin/sh

read x

echo $x
echo $x
1
2
3
4
5
6
7
8
9
10
11
#---------------------------------------------------
# reading of text line from keyboard with help comment
# echo without new line at the end
#---------------------------------------------------
#!/bin/sh

echo -n "Input text line=? "
read x

echo $x
echo $x
1
2
3
4
5
6
7
8
#---------------------------------------------------
# syntax:  many commands in one line !!!!
#---------------------------------------------------
#!/bin/sh

echo "a"; echo "b"; echo "c"

var=5; echo `expr $var + $var` 
1
2
3
4
5
6
7
8
9
10
11
12
#---------------------------------------------------
# integer variable and its increment
# does not works in sh !!!
#---------------------------------------------------
#!/bin/bash

var=12345
let var=$var+1 # let is important
echo $var

v=12345
v=$v+1 # result "12345+1"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#---------------------------------------------------
# integer arithmetics - bash only !
#---------------------------------------------------
#!/bin/bash

echo 'number=?' ; read x

let y=$x+$x ; echo 'x+x=' $y

let y=$x*$x ; echo 'square=' $y

let y=$x/3 ; echo 'x/3=' $y

let y=$x%7 ; echo 'x%7=' $y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#---------------------------------------------------
# integer arithmetics in sh !!! using expr - slow
#---------------------------------------------------
#!/bin/sh

a=123
b=12

c=`expr $a + $b` # addition
echo $c

c=`expr $a \* $b` # multiplication
echo $c

c=`expr $a / $b` # division
echo $c

c=`expr $a % $b` # residual
echo $c
1
2
3
4
5
6
7
8
9
10
11
12
#---------------------------------------------------
# very simple 'if' 
#---------------------------------------------------
#!/bin/sh

echo 'number=?'
read x

if [ $x -eq 5 ]
then
  echo "five"
fi  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#---------------------------------------------------
# if ... else
#---------------------------------------------------
#!/bin/sh

echo 'number=?'
read x

if [ $x -eq 5 ]
then
  echo "five"
else
  echo "not 5"  
fi  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#---------------------------------------------------
# if ... elif ... else
#---------------------------------------------------
#!/bin/sh

echo 'number=?'
read x

if [ $x -eq 5 ]
then
  echo "five"
elif [ $x -eq 7 ]
then
  echo "seven"  
else
  echo "not 5 and not 7"  
fi  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#---------------------------------------------------
# comparison -lt  and  -gt, nested if
#---------------------------------------------------
#!/bin/sh

echo -n 'number=?'
read x

if [ $x -gt 0 ]
then
  if [ $x -lt 10 ]
  then
    echo "0 < x < 10"
  fi
fi
1
2
3
4
5
6
7
8
9
10
11
12
#---------------------------------------------------
# while loop - print first 10 integers from 0
#---------------------------------------------------
#!/bin/bash

x=0

while  [ $x -lt 10 ]
do
  echo $x
  let x=$x+1
done
1
2
3
4
5
6
7
8
9
10
11
12
#---------------------------------------------------
# 10 random numbers generation
#---------------------------------------------------
#!/bin/bash

i=0
while [ $i -lt 10 ]
do
  x=$RANDOM
  echo $x
  let i=$i+1
done
1
2
3
4
5
6
7
8
9
10
#---------------------------------------------------
# endless loop: interrupting by ctrl-c
#---------------------------------------------------
#!/bin/sh

while [ 1 ]
do
  read x
  echo $x$x
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#---------------------------------------------------
# divisors of integer number
#---------------------------------------------------
#!/bin/bash

echo -n 'number=?'
read x

i=2 # possible divisor
k=1
let n=$x/2 # top limit for divisor

while [ $i -le $n ]
do
  let k=$x%$i # residual
  if [ $k -eq 0 ]
  then
    echo -n "Divisor= "
    echo $i
  if
  let i=$i+1
done
1
2
3
4
5
6
7
8
9
#---------------------------------------------------
# simple use of  for ... in ...
#---------------------------------------------------
#!/bin/sh

for i in "abc" "xyz" 1 2 99
do
  echo $i
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#---------------------------------------------------
# use for as in C-programming
# sum of the first n integer numbers
#---------------------------------------------------
#!/bin/bash

echo -n "number=?"
read n

s=0 # here sum

for((i=1; i <=n ; i++))
do
  let s=$s+$i 
done

echo "sum= "$s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#---------------------------------------------------
# operator case for selection of logical branches
# end marker ;; of branch
#---------------------------------------------------
#!/bin/sh

echo "input string=?"
read str

case "$str" in
  abc) echo "string = abc" 
    ;;
  xyz) echo "string = xyz" 
    ;;
  *)   echo "not abc, not zyz" ;;
esac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#---------------------------------------------------
# exit operator
#---------------------------------------------------
#!/bin/sh

while [ 1 ]
do
  read x
  echo $x
  if [ $x -eq 0 ] # in $x must be number!
  then
    echo "script done ..."
    exit 0
  fi
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#---------------------------------------------------
# string comparing
#---------------------------------------------------
#!/bin/sh

echo "Input string=?"
read str

if [ $str = "abc" ]
then
  echo "You got it!"
else
  echo "Its not 'abc'"
fi
1
2
3
4
5
6
7
8
9
10
11
12
13
#---------------------------------------------------
# simple strings concatenation
#---------------------------------------------------
#!/bin/sh

echo "Input string=?"
read str

s2=$str"AAAA"
echo $s2

s3="XXX"$s2
echo $s3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#---------------------------------------------------
# strings concatenation
#---------------------------------------------------
#!/bin/sh

echo "Input string=?"
read str1

echo "Input second string=?"
read str2

s3=$str1$str2 # it works!
echo $s3

s4=${str1}${str2} # it works too!
echo $s4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#---------------------------------------------------
# testing whether a string is null
#---------------------------------------------------
#!/bin/sh

echo "Input string=?"
read str

if [ $str ]
then
  echo "Not empty"
else
  echo "Empty"
fi
1
2
3
4
5
6
7
8
9
10
#---------------------------------------------------
# length of string
#---------------------------------------------------
#!/bin/sh

echo "Input string=?"
read str

leng=`expr length $str`
echo "length= "$leng
1
2
3
4
5
6
7
8
#---------------------------------------------------
# how to insert string to constant string
#---------------------------------------------------
#!/bin/sh

var="good"

echo "This is $var test"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#---------------------------------------------------
# simplest function example
#---------------------------------------------------
#!/bin/sh

#---------------------------
func()
{
  echo "Inside function"
}

#---------------------------
echo "Now function call..."
func
echo "end of main"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#---------------------------------------------------
# function can see variables of main program
#---------------------------------------------------
#!/bin/sh

#--------------------------
func()
{
  echo $var
}
#--------------------------

var="test of global "
func
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#---------------------------------------------------
# pass of parameters to function
#---------------------------------------------------
#!/bin/sh

#---------------------------------
func()
{
  echo "We are in function now"
  echo $0  # shell script name
  echo $1  # first parameter
  echo $2  # second parameter
  echo "We leave function..."
  exit 0
}

#---------------------------------

func  123  "abc"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#---------------------------------------------------
# passing variable parameters
#---------------------------------------------------
#!/bin/bash

#-------------------------------
func2()
{
  let r=$1*$1
  echo $r
}
#-------------------------------
var=123
func2 $var
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#---------------------------------------------------
# recursive function example
# calculation of factorial
#---------------------------------------------------
#!/bin/sh
#-------------------------------
factorial()
{
  if [ "$1" -gt "1" ]
  then
    i=`expr $1 - 1`
    j=`factorial $i`
    k=`expr $1 \* $j`
    echo $k
  else
    echo 1
  fi
}
#-------------------------------
read x
factorial $x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#---------------------------------------------------
# using of function library ????????
#---------------------------------------------------

file with name my.lb

func2()
{
  echo $1$1
}
func3()
{
  echo $1$1$1
}

shell program:

#!/bin/sh

./my.lb

var=123

func2 123

func3 123
1
2
3
4
5
6
#---------------------------------------------------
# floating point numbers
#---------------------------------------------------
#!/bin/sh

# does not support !!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#---------------------------------------------------
# simpiest array : declaration, element access and assignment
#---------------------------------------------------
#!/bin/bash

arr=(aa bb cc dd)

echo ${arr[0]} # curly bracket notation
echo ${arr[1]}
echo ${arr[2]}
echo ${arr[3]}

arr[2]="CCCCCCC"
echo ${arr[2]}
1
2
3
4
5
6
7
8
9
#---------------------------------------------------
# number of elements in array
#---------------------------------------------------
#!/bin/bash

arr=(aa bb cc dd)

n=${#arr[@]}
echo $n
1
2
3
4
5
6
7
8
9
10
11
12
#---------------------------------------------------
# array with filenames of current directory
#---------------------------------------------------
#!/bin/sh

arr=(*)  # * is list of all file and dir names

n=${#arr[@]}
echo "number of files and dirs "$n

echo ${arr[0]}
echo ${arr[1]}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#---------------------------------------------------
# print all array elements -not good
# works then no holes in indexes
#---------------------------------------------------
#!/bin/bash

arr=(aa bb cc dd ee ff gg)
n=${#arr[@]}
i=0
while test $i -lt $n
do
  echo ${arr[$i]}
  let i=$i+1
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#---------------------------------------------------
# dynamic expansion of array
# one array element in reality is couple (index, value)
#---------------------------------------------------
#!/bin/bash

arr=()

n=${#arr[@]}
echo "number of array elements "$n

arr[0]=a
n=${#arr[@]}
echo "number of array elements "$n

arr[1]=b
n=${#arr[@]}
echo "number of array elements "$n

arr[2]=c
n=${#arr[@]}
echo "number of array elements "$n

arr[10]=h
n=${#arr[@]}
echo "number of array elements "$n


echo ${arr[10]}

echo ${arr[4]} # empty string
echo ${arr[6]} # empty string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#---------------------------------------------------
# get all array and print it
#---------------------------------------------------
#!/bin/bash

arr=(aa bb cc dd ee ff gg)

echo ${arr[*]} # all array

echo ${arr[@]:0}   # aa bb cc dd ee ff gg

echo ${arr[@]:1}   # bb cc dd ee ff gg

echo ${arr[@]:2:3} # cc dd ee

for i in ${arr[*]}
do
  echo $i
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#---------------------------------------------------
# adding element to array
#---------------------------------------------------
#!/bin/bash

arr=(aa bb cc dd ee ff gg)

echo ${arr[*]}

arr=( "${arr[@]}" "newElem" ) # from right

echo ${arr[*]}

arr=( "newElem" "${arr[@]}" ) # from left

echo ${arr[*]}
1
2
3
4
5
6
7
8
9
10
11
12
#---------------------------------------------------
# move last element from array
#---------------------------------------------------
#!/bin/bash


arr=(aa bb cc dd ee ff gg)

echo ${arr[*]}

unset arr[${#arr[@]}-1] #  move last element
echo ${arr[*]}
1
2
3
4
5
6
7
8
9
10
11
12
#---------------------------------------------------
# copying of array
#---------------------------------------------------
#!/bin/bash

arr=(aa bb cc dd ee ff gg)

echo ${arr[*]}

arr2=( "${arr[@]}" )

echo ${arr2[*]}
1
2
3
4
5
6
7
8
9
10
11
#---------------------------------------------------
# get substring from string
#---------------------------------------------------
#!/bin/bash

echo "long string input=?"
read st

st2=${st:2:4}

echo $st2
1
2
3
4
5
6
7
8
9
10
11
#---------------------------------------------------
# substring replacement "abc" to "xyz"
#---------------------------------------------------
#!/bin/bash

echo "string input=?"
read str

st2=${str/abc/xyz} # only ones

echo $st2
1
2
3
4
5
6
7
8
9
10
11
#---------------------------------------------------
# search of character 'a' in a string
#---------------------------------------------------
#!/bin/sh

echo "string input=?"
read str

pos=`expr index $str a`

echo "position of the first 'a' = "$pos
1
2
3
4
5
6
7
8
9
#---------------------------------------------------
# string list counting
#---------------------------------------------------
#!/bin/sh

for i in aa bb cc dd ee ff gg hh
do
  echo $i
done
1
2
3
4
5
6
7
8
9
10
11
#---------------------------------------------------
# command line arguments
# separated by spaces
#---------------------------------------------------
#!/bin/sh

echo $0  # script file name

echo $1  # first argument
echo $2  # second argument
echo $3  # third argument
1
2
3
4
5
6
7
8
9
10
11
#---------------------------------------------------
# command line arguments without script name number
# all command line without script name
#---------------------------------------------------
#!/bin/sh

echo $#  # argument number

echo $*  # command line

echo $@
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#---------------------------------------------------
# get all files and dir names
#---------------------------------------------------
#!/bin/sh

echo *  # file and dir names of current dir

for i in *
do
  echo $i
done

echo ../* # file and dir names of parent dir

*/ just close comments
1
2
3
4
5
6
7
8
9
#---------------------------------------------------
# file search from root dir      ???????
# file name - parameter from command line
#---------------------------------------------------
#!/bin/sh

start=$HOME
date
find $start -name $1 -print
1
2
3
4
5
6
#---------------------------------------------------
# list of all files with extension .txt     !!!!!!
#---------------------------------------------------
#!/bin/sh

echo *.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#---------------------------------------------------
# combine a set of text files in one file use
# script >targetfile.lst , not txt-file !!!
#---------------------------------------------------
#!/bin/sh

lst=*.txt

for i in $lst
do
  echo
  echo "======================================"
  echo "File "$i
  echo "======================================"
  cat <$i
done
1
2
3
4
5
6
7
8
9
10
#---------------------------------------------------
# create new file and write string to it
# file name from command string - variable $1
#---------------------------------------------------
#!/bin/sh

echo "String=?"
read str

echo $str >$1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#---------------------------------------------------
# read textlines from console and add them to file
# file name from command string - variable $1
#---------------------------------------------------
#!/bin/sh

echo "Add strings=?"
str="1"

while [ $str ]
do
  read str
  echo $str >>$1
done
1
2
3
4
5
6
7
#---------------------------------------------------
# read first string from text file
#---------------------------------------------------
#!/bin/sh

read str <$1
echo $str
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#---------------------------------------------------
# text file reading
# script res.txt
#---------------------------------------------------
#!/bin/sh

str="1"

while [ $str ]
do
  read str
  echo $str
  echo $str
done
You can also navigate with left [or] right arrows next →