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=$HOMEcd$homedir
1
2
3
4
5
6
7
8
9
//===================================================================================
// how to print shell variable
//===================================================================================echo PS1
echo$PS1echo$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
//===================================================================================#---------------------------------------------------# 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/shecho'hello'echo"hello"echo hello
1
2
3
4
5
6
7
8
#---------------------------------------------------# declaration and printing of string variable#---------------------------------------------------#!/bin/shx='Wonderful new World'echo$xecho x # just string 'x'
1
2
3
4
5
#---------------------------------------------------# call of other programs#---------------------------------------------------#!/bin/shls
1
2
3
4
5
6
7
8
9
10
#---------------------------------------------------# indirect call of other programs#---------------------------------------------------#!/bin/shx='pwd'$xx='ls -l'$x
1
2
3
4
5
6
7
8
9
#---------------------------------------------------# indirect call with indirect parameters#---------------------------------------------------#!/bin/shu='-l'x='ls '$x$u
1
2
3
4
5
6
#---------------------------------------------------# print current shell name ???#---------------------------------------------------#!/bin/shecho$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/shv1="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/shv1="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/shdate=`date`echo the date is $date
1
2
3
4
5
6
7
8
#---------------------------------------------------# escaping#---------------------------------------------------#!/bin/shmsg=`echo Your Current Directory is \`pwd\``echo$msg
1
2
3
4
5
6
7
8
9
#---------------------------------------------------# reading of text line from keyboard#---------------------------------------------------#!/bin/shread x
echo$xecho$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/shecho-n"Input text line=? "read x
echo$xecho$x
1
2
3
4
5
6
7
8
#---------------------------------------------------# syntax: many commands in one line !!!!#---------------------------------------------------#!/bin/shecho"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/bashvar=12345
let var=$var+1 # let is importantecho$varv=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/bashecho'number=?';read x
let y=$x+$x;echo'x+x='$ylet y=$x*$x;echo'square='$ylet y=$x/3 ;echo'x/3='$ylet 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/sha=123
b=12
c=`expr$a + $b`# additionecho$cc=`expr$a\*$b`# multiplicationecho$cc=`expr$a / $b`# divisionecho$cc=`expr$a % $b`# residualecho$c
1
2
3
4
5
6
7
8
9
10
11
12
#---------------------------------------------------# very simple 'if' #---------------------------------------------------#!/bin/shecho'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/shecho'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/shecho'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/shecho-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/bashx=0
while[$x-lt 10 ]do
echo$xlet x=$x+1
done
#---------------------------------------------------# divisors of integer number#---------------------------------------------------#!/bin/bashecho-n'number=?'read x
i=2 # possible divisork=1
let n=$x/2 # top limit for divisorwhile[$i-le$n]do
let k=$x%$i# residualif[$k-eq 0 ]then
echo-n"Divisor= "echo$iif
let i=$i+1
done
1
2
3
4
5
6
7
8
9
#---------------------------------------------------# simple use of for ... in ...#---------------------------------------------------#!/bin/shfor i in"abc""xyz" 1 2 99
do
echo$idone
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/bashecho-n"number=?"read n
s=0 # here sumfor((i=1; i <=n ; i++))do
let s=$s+$idone
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/shecho"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/shwhile[ 1 ]do
read x
echo$xif[$x-eq 0 ]# in $x must be number!then
echo"script done ..."exit 0
fi
done
#---------------------------------------------------# strings concatenation#---------------------------------------------------#!/bin/shecho"Input string=?"read str1
echo"Input second string=?"read str2
s3=$str1$str2# it works!echo$s3s4=${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/shecho"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/shecho"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/shvar="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 nameecho$1# first parameterecho$2# second parameterecho"We leave function..."exit 0
}#---------------------------------
func 123 "abc"
#---------------------------------------------------# 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/basharr=(aa bb cc dd)echo${arr[0]}# curly bracket notationecho${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/basharr=(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/sharr=(*)# * is list of all file and dir namesn=${#arr[@]}echo"number of files and dirs "$necho${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/basharr=(aa bb cc dd ee ff gg)n=${#arr[@]}i=0
while test$i-lt$ndo
echo${arr[$i]}let i=$i+1
done
#---------------------------------------------------# dynamic expansion of array# one array element in reality is couple (index, value)#---------------------------------------------------#!/bin/basharr=()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 "$necho${arr[10]}echo${arr[4]}# empty stringecho${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/basharr=(aa bb cc dd ee ff gg)echo${arr[*]}# all arrayecho${arr[@]:0}# aa bb cc dd ee ff ggecho${arr[@]:1}# bb cc dd ee ff ggecho${arr[@]:2:3}# cc dd eefor i in${arr[*]}do
echo$idone
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#---------------------------------------------------# adding element to array#---------------------------------------------------#!/bin/basharr=(aa bb cc dd ee ff gg)echo${arr[*]}arr=("${arr[@]}""newElem")# from rightecho${arr[*]}arr=("newElem""${arr[@]}")# from leftecho${arr[*]}
1
2
3
4
5
6
7
8
9
10
11
12
#---------------------------------------------------# move last element from array#---------------------------------------------------#!/bin/basharr=(aa bb cc dd ee ff gg)echo${arr[*]}unset arr[${#arr[@]}-1]# move last elementecho${arr[*]}
1
2
3
4
5
6
7
8
9
10
11
12
#---------------------------------------------------# copying of array#---------------------------------------------------#!/bin/basharr=(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/bashecho"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/bashecho"string input=?"read str
st2=${str/abc/xyz}# only onesecho$st2
1
2
3
4
5
6
7
8
9
10
11
#---------------------------------------------------# search of character 'a' in a string#---------------------------------------------------#!/bin/shecho"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/shfor i in aa bb cc dd ee ff gg hh
do
echo$idone
1
2
3
4
5
6
7
8
9
10
11
#---------------------------------------------------# command line arguments# separated by spaces#---------------------------------------------------#!/bin/shecho$0# script file nameecho$1# first argumentecho$2# second argumentecho$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/shecho$# # argument numberecho$*# command lineecho$@
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#---------------------------------------------------# get all files and dir names#---------------------------------------------------#!/bin/shecho*# file and dir names of current dirfor i in*do
echo$idone
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/shstart=$HOMEdate
find $start-name$1-print
1
2
3
4
5
6
#---------------------------------------------------# list of all files with extension .txt !!!!!!#---------------------------------------------------#!/bin/shecho*.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/shlst=*.txt
for i in$lstdo
echo
echo"======================================"echo"File "$iecho"======================================"cat <$idone
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/shecho"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/shecho"Add strings=?"str="1"while[$str]do
read str
echo$str>>$1done
1
2
3
4
5
6
7
#---------------------------------------------------# read first string from text file#---------------------------------------------------#!/bin/shread str <$1echo$str
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#---------------------------------------------------# text file reading# script res.txt#---------------------------------------------------#!/bin/shstr="1"while[$str]do
read str
echo$strecho$strdone
You can also navigate with left [or] right arrows next →