Post

Bash Note

[toc]


bash Note


1
2
3
4
5
6
7
8
9
10
Number of elements: ${#ARRAY[@]}
echo ${ARRAY[@]}

stdin
# standard input. It takes text as input.
stdout
# standard output. The text output of a command is stored in the stdout stream.
stderr
# standard error. Whenever a command faces an error, the error message is stored in this stream.


Hello World Bash Shell Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
--- check bash interpreter location
$ which bash
/bin/bash

--- create file called hello_world.sh.
#!/bin/bash
# declare STRING variable
STRING="Hello World"
#print variable on a screen
echo $STRING


--- make the file executable:
$ chmod +x hello_world.sh
$ ./hello_world.sh

Simple Backup bash shell script

1
2
3
4
5
6
7
8
9
#!/bin/bash
tar -czf myhome_directory.tar.gz /home/linuxconfig

--- make the file executable:
$ ./backup.sh
tar: Removing leading '/' from member names

$ ls myhome_directory.tar.gz
myhome_directory.tar.gz

Variables

  1. declare simple bash variable and print it on the screen (stdout) with echo command.
1
2
3
4
5
6
7
--- create file called hello_world.sh.
#!/bin/bash
STRING="HELLO WORLD!!!"
echo $STRING

--- make the file executable:
$ ./hello_world.sh
  1. Bash backup Script with bash Variables
1
2
3
4
--- backup script:
#!/bin/bash
OF=myhome_directory_$(date +%Y%m%d).tar.gz
tar -czf $OF /home/linuxconfig

Global vs. Local variables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash

#Define bash global variable
#global variable can be used anywhere in this bash script
VAR="global variable"
function bash {
    #Define bash local variable
    #This variable is local to bash function only
    local VAR="local variable"
    echo $VAR
}
echo $VAR
bash
echo $VAR


--- make the file executable:
$ ./variable.sh
# global variable
# local variable
# global variable

Passing arguments to the bash script

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
#!/bin/bash
# 1.
# use predefined variables to access passed arguments
# echo arguments to the shell
echo $1 $2 $3 ' -> echo $1 $2 $3'

# 2.
# store arguments from bash command line in special array
args=("$@")
echo ${args[0]} ${args[1]} ${args[2]} ' -> args=("$@"); echo ${args[0]} ${args[1]} ${args[2]}'

# 3.
# use $@ to print out all arguments at once
echo $@ ' -> echo $@'

# 4.
# use $# variable to print out
# number of arguments passed to the bash script
echo Number of arguments passed: $# ' -> echo Number of arguments passed: $#'


--- make the file executable:
$ ./arguments.sh Bash Scripting Tutorial
# Bash Scripting Tutorial -> echo $1 $2 $3
# Bash Scripting Tutorial -> args=("$@"); echo ${args[0]} ${args[1]} ${args[2]}
# Bash Scripting Tutorial -> echo $@
# Number of arguments passed: 3 -> echo Number of arguments passed: $#

Executing shell commands with bash

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
# use backticks " ` ` " to execute shell command
echo `uname -o`
# executing bash command without backticks, just print out
echo uname -o

--- make the file executable:
$ ./test.sh
# $ uname -o
# GNU/Linux
# uname -o

Reading User Input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
echo -e "Hi, please type the word: \c "
read  word # user input
echo "The word you entered is: $word"

echo -e "Can you please enter two words? "
read word1 word2
echo "Here is your input: \" $word1 \" \" $word2 \" "

echo -e "How do you feel about bash scripting? "
# read command now stores a reply into the default built-in variable $REPLY
read
echo "You said $REPLY, I'm glad to hear that! "

echo -e "What are your favorite colours ? "
# -a makes read command to read into an array
read -a colours
echo "My favorite colours are also ${colours[0]}, ${colours[1]} and ${colours[2]}:-)"

Bash Trap Command

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
# bash trap command
trap bashtrap INT
# bash clear screen command, clear and then run
clear;

# bash trap function is executed when CTRL-C is pressed:
# bash prints message => Executing bash trap subrutine !
bashtrap(){
    echo "CTRL+C Detected !...executing bash trap !"
}

# for loop from 1/10 to 10/10
for a in `seq 1 10`; do
    echo "$a/10 to Exit."
    sleep 1;
done

echo "Exit Bash Trap Example!!!"

Arrays

  1. Declare simple bash array
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
# Declare array with 4 elements
ARRAY=( 'Debian Linux' 'Redhat Linux' Ubuntu Linux )
# get number of elements in the array
ELEMENTS=${#ARRAY[@]}

# echo each element in array
# for loop
for (( i=0; i<$ELEMENTS; i++)); do
    echo ${ARRAY[${i}]}
done
  1. Read file into bash array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
# Declare array
declare -a ARRAY

# Link filedescriptor 10 with stdin
exec 10<&0
# stdin replaced with a file supplied as a first argument
exec < $1

let count=0

while read LINE; do
    ARRAY[$count]=$LINE
    ((count++))
done

echo Number of elements: ${#ARRAY[@]}
# echo array's content
echo ${ARRAY[@]}

# restore stdin from filedescriptor 10
# and close filedescriptor 10
exec 0<&10 10<&-
  1. Bash script execution with an output:
1
2
3
4
5
6
7
8
9
linuxconfig.org $ cat bash.txt
Bash
Scripting
Tutorial
Guide

linuxconfig.org $ ./bash-script.sh bash.txt
Number of elements: 4
Bash Scripting Tutorial Guide

Bash if / else / fi statements

Simple Bash if/else statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# note the spacing inside the [ and ] brackets!
# Without the spaces, it won't work!

#!/bin/bash
directory="./BashScripting"

# bash check if directory exists
if [ -d $directory ]; then
	echo "Directory exists"
else
	echo "Directory does not exists"
fi


--- make the file executable:
$ ./if.sh
# Directory does not exists
$ mkdir BashScripting
$ ./if.sh
# Directory exists

Nested if/else

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
33
34
35
#!/bin/bash

# Declare variable choice and assign value 4
choice=4
# Print to stdout
echo "1. Bash"
echo "2. Scripting"
echo "3. Tutorial"
echo -n "Please choose a word [1,2 or 3]? "
# Loop while the variable choice is equal 4
# bash while loop
while [ $choice -eq 4 ]; do

# read user input
read choice
# bash nested if/else
if [ $choice -eq 1 ] ; then
        echo "You have chosen word: Bash"
else
        if [ $choice -eq 2 ] ; then
                 echo "You have chosen word: Scripting"
        else
                if [ $choice -eq 3 ] ; then
                        echo "You have chosen word: Tutorial"
                else
                        echo "Please make a choice between 1-3 !"
                        echo "1. Bash"
                        echo "2. Scripting"
                        echo "3. Tutorial"
                        echo -n "Please choose a word [1,2 or 3]? "
                        choice=4
                fi
        fi
fi
done

Bash Comparisons

Arithmetic Comparisons

..
-lt<
-gt>
-le<=
-ge>=
-eq==
-ne!=
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
# declare integers
NUM1=2
NUM2=2
if [ $NUM1 -eq $NUM2 ]; then
	echo "Both Values are equal"
elif [ $NUM1 -gt $NUM2 ]; then
	echo "NUM1 is greater then NUM2"
else
	echo "NUM2 is greater then NUM1"
fi

String Comparisons

..
=equal
!=not equal
<less then
>greater then
-n s1string s1 is not empty
-z s1string s1 is empty
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
#Declare string S1
S1="Bash"
#Declare string S2
S2="Scripting"

if [ $S1 = $S2 ]; then
	echo "Both Strings are equal"
else
	echo "Strings are NOT equal"
fi

Bash File Testing

..
-b filenameBlock special file
-c filenameSpecial character file
-d directorynameCheck for directory existence
-e filenameCheck for file existence
-f filenameCheck for regular file existence not a directory
-G filenameCheck if file exists and is owned by effective group ID.
-g filenametrue if file exists and is set-group-id.
-k filenameSticky bit
-L filenameSymbolic link
-O filenameTrue if file exists and is owned by the effective user id.
-r filenameCheck if file is a readable
-S filenameCheck if file is socket
-s filenameCheck if file is nonzero size
-u filenameCheck if file set-ser-id bit is set
-w filenameCheck if file is writable
-x filenameCheck if file is executable
1
2
3
4
5
6
7
8
#!/bin/bash
file="./file"

if [ -e $file ]; then
	echo "File exists"
else
	echo "File does not exists"
fi

Similarly for example we can use while loop to check if file does not exists. This script will sleep until file does exists. Note bash negator “!” which negates the -e option.

1
2
3
4
5
6
#!/bin/bash

while [ ! -e myfile ]; do
# Sleep until file does exists/is created
sleep 1
done

Loops

Bash for loop

  1. echo every file name under /var/
    1
    2
    3
    4
    5
    
    #!/bin/bash
    # bash for loop
    for f in $( ls /var/ ); do
     echo $f
    done
    
  2. Run for loop from bash shell command line:
1
$ for f in $( ls /var/ ); do echo $f; done

Bash while loop

1
2
3
4
5
6
7
#!/bin/bash
COUNT=6
# bash while loop
while [ $COUNT -gt 0 ]; do
	echo Value of count is: $COUNT
	let COUNT=COUNT-1
done

Bash until loop

1
2
3
4
5
6
7
#!/bin/bash
COUNT=0
# bash until loop
until [ $COUNT -gt 5 ]; do
        echo Value of count is: $COUNT
        let COUNT=COUNT+1
done

Control bash loop with

Here is a example of while loop controlled by standard input.

Until the redirection chain from STDOUT to STDIN to the read command exists the while loop continues.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
# This bash script will locate and replace spaces in the filenames
DIR="."
# Controlling a loop with bash read command by redirecting STDOUT as a STDIN to while loop
# find will not truncate filenames containing spaces
find $DIR -type f | while read file; do
# using POSIX class [:space:] to find space in the filename
if [[ "$file" = *[[:space:]]* ]]; then
# substitute space with "_" character and consequently rename the file
mv "$file" `echo $file | tr ' ' '_'`
fi;
# end of while loop
done
Bash script to replace spaces in the filenames with _

Bash Functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
!/bin/bash
# BASH FUNCTIONS CAN BE DECLARED IN ANY ORDER
function function_B {
        echo Function B.
}
function function_A {
        echo $1
}
function function_D {
        echo Function D.
}
function function_C {
        echo $1
}
# FUNCTION CALLS
# Pass parameter to function A
function_A "Function A."
function_B
# Pass parameter to function C
function_C "Function C."
function_D

Bash Select

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
PS3='Choose one word: '

# bash select
select word in "linux" "bash" "scripting" "tutorial"
do
  echo "The word you have selected is: $word"
# Break, otherwise endless loop
  break
done

exit 0

Case statement conditional

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
echo "What is your preferred programming / scripting language"
echo "1) bash"
echo "2) perl"
echo "3) python"
echo "4) c++"
echo "5) I do not know !"
read case;
#simple case bash structure
# note in this case $case is variable and does not have to
# be named case this is just an example
case $case in
    1) echo "You selected bash";;
    2) echo "You selected perl";;
    3) echo "You selected python";;
    4) echo "You selected c++";;
    5) exit
esac

Bash quotes and quotations

Quotations and quotes are important part of bash and bash scripting. Here are some bash quotes and quotations basics.


Escaping Meta characters

Before we start with quotes and quotations we should know something about escaping meta characters. Escaping will suppress a special meaning of meta characters and therefore meta characters will be read by bash literally. To do this we need to use backslash "\" character. Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

#Declare bash string variable
BASH_VAR="Bash Script"

# echo variable BASH_VAR
echo $BASH_VAR

#when meta character such us "$" is escaped with "\" it will be read literally
echo $BASH_VAR

# backslash has also special meaning and it can be suppressed with yet another "\"
echo "\"

Single quotes

Single quotes in bash will suppress special meaning of every meta characters. Therefore meta characters will be read literally. It is not possible to use another single quote within two single quotes not even if the single quote is escaped by backslash.

1
2
3
4
5
6
7
8
9
#!/bin/bash
#Declare bash string variable
BASH_VAR="Bash Script"

# echo variable BASH_VAR
echo $BASH_VAR

# meta characters special meaning in bash is suppressed when  using single quotes
echo '$BASH_VAR  "$BASH_VAR"'

Double Quotes

Double quotes in bash will suppress special meaning of every meta characters except “$”, “" and “`”. Any other meta characters will be read literally. It is also possible to use single quote within double quotes. If we need to use double quotes within double quotes bash can read them literally when escaping them with “". Example:

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash

#Declare bash string variable
BASH_VAR="Bash Script"

# echo variable BASH_VAR
echo $BASH_VAR

# meta characters and its special meaning in bash is
# suppressed when using double quotes except "$", "\" and "`"
echo "It's $BASH_VAR  and \"$BASH_VAR\" using backticks: `date`"

Bash quoting with ANSI-C style

There is also another type of quoting and that is ANSI-C. In this type of quoting characters escaped with “" will gain special meaning according to the ANSI-C standard.

ANSI-C.
\aalert (bell)
\bbackspace
\ean escape character
\fform feed
\nnewline
\rcarriage return
\thorizontal tab
\vvertical tab
\backslash
`single quote
\nnnoctal value of characters ( see [https://www.asciitable.com/ ASCII table] )
\xnnhexadecimal value of characters ( see [https://www.asciitable.com/ ASCII table] )

The syntax fo ansi-c bash quoting is: $’’ . Here is an example:

1
2
3
4
5
#!/bin/bash
# used \n as a new line, \x40 is hex value for @ and  is octal value for .
echo $'web: www.linuxconfig.org\nemail: web\x40linuxconfigorg'
# web: www.linuxconfig.org
# email: web@linuxconfigorg

Arithmetic Operations


Bash Addition Calculator Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash

let RESULT1=$1+$2

echo $1+$2=$RESULT1 ' -> # let RESULT1=$1+$2'

declare -i RESULT2
RESULT2=$1+$2

echo $1+$2=$RESULT2 ' -> # declare -i RESULT2; RESULT2=$1+$2'
echo $1+$2=$(($1 + $2)) ' -> # $(($1 + $2))'


--- make the file executable:
$ ./calc.sh 88 12
88+12=100 -> # let RESULT1=$1+$2
88+12=100 -> # declare -i RESULT2; RESULT2=$1+$2
88+12=100 -> # $(($1 + $2))


Bash Arithmetic

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash

echo '### let ###'
# bash addition
let ADDITION=3+5
echo "3 + 5 =" $ADDITION

# bash subtraction
let SUBTRACTION=7-8
echo "7 - 8 =" $SUBTRACTION

# bash multiplication
let MULTIPLICATION=5*8
echo "5 * 8 =" $MULTIPLICATION

# bash division
let DIVISION=4/2
echo "4 / 2 =" $DIVISION

# bash modulus
let MODULUS=9%4
echo "9 % 4 =" $MODULUS

# bash power of two
let POWEROFTWO=2**2
echo "2 ^ 2 =" $POWEROFTWO

echo '### Bash Arithmetic Expansion ###'
# There are two formats for arithmetic expansion: $[ expression ]
# and $(( expression #)) its your choice which you use

echo 4 + 5 = $((4 + 5))
echo 7 - 7 = $[ 7 - 7 ]
echo 4 x 6 = $((3 * 2))
echo 6 / 3 = $((6 / 3))
echo 8 % 7 = $((8 % 7))
echo 2 ^ 8 = $[ 2 ** 8 ]


echo '### Declare ###'
echo -e "Please enter two numbers \c"
# read user input
read num1 num2
declare -i result
result=$num1+$num2
echo "Result is:$result "

# bash convert binary number 10001
result=2#10001
echo $result

# bash convert octal number 16
result=8#16
echo $result

# bash convert hex number 0xE6A
result=16#E6A
echo $result

Round floating point number

%.0f $floating_point_number

1
2
3
4
5
6
7
8
#!/bin/bash
# get floating point number
floating_point_number=3.3446
echo $floating_point_number
# round floating point number with bash
for bash_rounded_number in $(printf %.0f $floating_point_number); do
echo "Rounded number with bash:" $bash_rounded_number
done

Bash floating point calculations

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
# Simple linux bash calculator
echo "Enter input:"
read userinput
echo "Result with 2 digits after decimal point:"
echo "scale=2; ${userinput}" | bc
echo "Result with 10 digits after decimal point:"
echo "scale=10; ${userinput}" | bc
echo "Result as rounded integer:"
echo $userinput | bc
Bash floating point calculations

Redirections


STDOUT from bash script to STDERR

#!/bin/bash
echo "Redirect this STDOUT to STDERR" 1>&2
# To prove that STDOUT is redirected to STDERR we can redirect script's output to file:

STDERR from bash script to STDOUT

1
2
3
#!/bin/bash
cat $1 2>&1
To prove that STDERR is redirected to STDOUT we can redirect script's output to file:

stdout to screen

The simple way to redirect a standard output ( stdout ) is to simply use any command, because by default stdout is automatically redirected to screen. First create a file “file1”:

1
2
3
# $ touch file1
$ ls file1
file1

As you can see from the example above execution of ls command produces STDOUT which by default is redirected to screen.


stdout to file

The override the default behavior of STDOUT we can use “>” to redirect this output to file:

1
2
3
$ ls file1 > STDOUT
# $ cat STDOUT
# file1

stderr to file

By default STDERR is displayed on the screen:

1
2
3
4
5
6
7
8
9
10
11
12
$ ls
file1  STDOUT
$ ls file2
ls: cannot access file2: No such file or directory

redirect the standard error ( stderr ) to a file and stdout to a screen as default.
STDOUT is displayed on the screen, however STDERR is redirected to a file called STDERR:

$ ls file1 file2 2> STDERR
file1
$ cat STDERR
ls: cannot access file2: No such file or directory

stdout to stderr

It is also possible to redirect STDOUT and STDERR to the same file. In the next example we will redirect STDOUT to the same descriptor as STDERR. Both STDOUT and STDERR will be redirected to file “STDERR_STDOUT”.

1
2
3
4
5
6
7
8
9
$ ls
file1  STDERR  STDOUT

$ ls file1 file2 2> STDERR_STDOUT 1>&2

$ cat STDERR_STDOUT
ls: cannot access file2: No such file or directory
file1
# File STDERR_STDOUT now contains STDOUT and STDERR.

stderr to stdout

redirecting STDERR to the same descriptor as SDTOUT:

1
2
3
4
5
6
7
8
$ ls
file1  STDERR  STDOUT

$ ls file1 file2 > STDERR_STDOUT 2>&1

$ cat STDERR_STDOUT
ls: cannot access file2: No such file or directory
file1

stderr and stdout to file

Previous two examples redirected both STDOUT and STDERR to a file. Another way to achieve the same effect is illustrated below:

1
2
3
4
5
6
7
8
9
$ ls
file1  STDERR  STDOUT

$ ls file1 file2 &> STDERR_STDOUT
$ ls file1 file2 >& STDERR_STDOUT

$ cat STDERR_STDOUT
# ls: cannot access file2: No such file or directory
# file1

ref:

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.