0% found this document useful (0 votes)
24 views17 pages

S08 Shell Scripting Extras Arrays Slides

Uploaded by

Surya T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views17 pages

S08 Shell Scripting Extras Arrays Slides

Uploaded by

Surya T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Sarfraaz Ahmed Linux Course

Basic LinuX &


Shell ScriptinG
— By : Sarfraaz Ahmed

Linux® is the registered trademark of Linus Torvalds in the U.S. and other countries.
Linux Course

Arrays

Sarfraaz Ahmed
Linux Course

Strings as Arrays
1 #!/usr/bin/env bash
2
3 print_words() {
4 for item in "$@" $ ./slogan.sh
Input 5 do Aim
Field 6 echo $item Big
Separater : 7 done Can
IFS 8 } Do
9
10 work="Aim Big Can Do"
11 print_words $work

Each word in a string is considered as an element of an array

Sarfraaz Ahmed

Linux Course

Strings as Arrays
1 #!/usr/bin/env bash
2
3 print_words() {
4 for item in "$@"
5 do
6 echo $item $ ./slogan.sh
7 done IFS is : "
8 } "
IFS 9 Arun,Ben,Carol,Dan
de nes word 10 line="Arun,Ben,Carol,Dan" IFS is : ","
boundaries 11 Arun
12 ( Ben
for splitting 13 echo "IFS is : \"$IFS\"" Carol
14 print_words $line Dan
15
16 IFS=","
17 echo "IFS is : \"$IFS\""
18 print_words $line
19 )

Sarfraaz Ahmed

fi

Linux Course

Strings as Arrays
1 #!/usr/bin/env bash
2
Insert “We”
. . . . .
20
between
21 work="Aim Big Can Do" “Big” and “Can”
22 echo $work
23
24 sub="We"
26 idx=0 $ ./slogan.sh
Much 27 total=${#work} Aim Big Can Do
28 Aim
easier to do this, Aim Big We
29 for item in $work
if we use 30 do Aim Big We Can
Shell Arrays 31 new_work="$new_work $item” Aim Big We Can Do
32 if [ $idx -eq 1 ] Aim Big We Can Do
33 then
34 new_work="$new_work $sub”
35 fi
36 echo $new_work
37 idx=$((idx + 1))
38 done
39 echo $new_work

Sarfraaz Ahmed

Linux Course

Arrays
• A data structure that represents a collection of
items

• Items can be of the same type or different types

• Items are referenced through their index

• Index starts from 0

• The position of each item in an array conveys a


meaning

Sarfraaz Ahmed

Linux Course

Arrays
Declaring an Array

metros=(Delhi Mumbai Chennai Kolkatta)

cities=(“New York” “London” “Paris”)

declare -a scores=(89 93 85 97)

work_string=“Aim Big Can Do”


work_array=($work_string)

colors[0]=“Red”
colors[1]=“Green”
colors[2]=“Blue”

Sarfraaz Ahmed

Linux Course

Arrays
Printing elements of an Array

metros=(Delhi Mumbai Chennai Kolkatta)

$ echo ${metros[0]}
Delhi
$ echo ${metros[1]}
Mumbai
$ idx=3
$ echo ${metros[$idx]}
Kolkatta
$ echo ${metros}
Delhi

Sarfraaz Ahmed

Linux Course

Arrays
Printing an entire Array

cities=(“New York” “London” “Paris”)

Entire array is
expanded to a
$ echo “${cities[*]}” single word
Always enclose New York London Paris
$
within
$ echo “${cities[@]}”
“double quotes” New York London Paris
Each element is
expanded as a
separate word

Sarfraaz Ahmed

Linux Course

Arrays
Printing an entire Array

cities=(“New York” “London” “Paris”)

61 for item in “${cities[*]}”


62 do
63 echo $item
64 done
Entire array is
expanded to a
single word
$ ./print.sh
New York London Paris

Sarfraaz Ahmed

Linux Course

Arrays
Printing an entire Array Each element is
expanded as a
separate word

cities=(“New York” “London” “Paris”)

61 for item in “${cities[*]}” 61 for item in “${cities[@]}”


62 do 62 do
63 echo $item 63 echo $item
64 done 64 done

$ ./print.sh
$ ./print.sh New York
New York London Paris London
Paris

Sarfraaz Ahmed

Linux Course

Arrays
Printing an entire Array

str=“Aim Big Can Do"

55 for item in ”$str”


56 do
57 echo $item
58 done

$ ./print.sh
Aim
Big
Can
Do

Each word in a string is considered as an element of an array

Sarfraaz Ahmed

Linux Course

Array Length
cities=(“New York” “London” “Paris”)

Length of str=“${cities[1]}”
London
element of array echo $str
at index 1
echo ${#str} 6
echo ${#cities[1]} 6

Number of echo ${#cities[@]} 3


elements in the echo ${#cities[*]} 3
array
echo ${#cities} 8
echo ${#cities[0]} 8
Length of
element of array
at index 0

Sarfraaz Ahmed

Linux Course

Array Operations
Concatenating Arrays

metros=(Delhi Mumbai Chennai Kolkatta)

cities=(“New York” “London” “Paris”)

capitals=(“${metros[@]}” “${cities[@]}”)

Adding items to an Array

capitals=(“${capitals[@]}” “Tokyo”)

Sarfraaz Ahmed
Linux Course

Slice of an Array
metros=(Delhi Mumbai Chennai Kolkatta)
3 elements
from index 1
$ echo ${metros[@]:1:3}

Mumbai Chennai Kolkatta


2 elements
from index 0
$ echo ${metros[@]:0:2}

Delhi Mumbai
All elements
from index 2
$ echo ${metros[@]:2}

Chennai Kolkatta

Sarfraaz Ahmed
Linux Course

Insert an Array Element


work=(Aim Big Can Do)

Insert “We” between “Big” and “Can”

$ idx=2
$ sub=“We”
$ new_work=(${work[@]:0:$idx} $sub ${work[@]:$idx})

$ new_work=( Aim Big We Can Do )

$ echo ${new_work[@]}
Aim Big We Can Do

Sarfraaz Ahmed

Linux Course

THANK YOU

Sarfraaz Ahmed

You might also like