Linux shell programming : array operations
bogotobogo.com site search:
Array operations - iterating array
Let's start with the simplest array indexing:
#!/bin/bash arr=(10 5 300 4000 22000) len=${#arr[@]} for ((i=0;i<$len;i++)) do # index element of the array length of the element echo "$i ${arr[$i]} ${#arr[$i]}" done
Output:
0 10 2 1 5 1 2 300 3 3 4000 4 4 22000 5
Here is a more comprehensive array shell code:
#!/bin/bash stars=(Andromeda Circunus Lupus Sagitta Virgo) for item in "${stars[@]}" do echo $item done # first element echo ${stars[0]} echo ${stars:0} # all elements echo ${stars[@]} echo ${stars[@]:0} # all elements but not the first one echo ${stars[@]:1} # elements in a range echo ${stars[@]:1:3} # length of the first element echo ${#stars[0]} echo ${#stars} # number of elements echo ${#stars[*]} echo ${#stars[@]} # replacing substring echo ${stars[@]//t/T}
The output looks like this:
$ ./op.sh Andromeda Circunus Lupus Sagitta Virgo Andromeda Andromeda Andromeda Circunus Lupus Sagitta Virgo Andromeda Circunus Lupus Sagitta Virgo Circunus Lupus Sagitta Virgo Circunus Lupus Sagitta 9 9 5 5 Andromeda Circunus Lupus SagiTTa Virgo
Array operations - Reverse a string
To reverse a string, we can do with a command rev
:
$ echo abcde | rev
To do the same thing in shell script, we need to know how to extract a single character from a string:
#!/bin/bash s="abcde" len=${#s} echo "1st string at index = 0, ${s:0:1}" echo "last string at index = 0, ${s:len-1:1}"
Note that the substring syntax looks like this:
## syntax ## ${parameter:offset:length}
Output:
1st string at index = 0, a last string at index = 0, e
Now, let's run a loop - extract a character from the end:
#!/bin/bash s="abcde" len=${#s} for ((i=len-1;i>=0;i--)) do echo "index=$i, ${s:$i:1}" done
Output:
index=4, e index=3, d index=2, c index=1, b index=0, a
It's time to reverse the string, constructing it one by one from the end:
#!/bin/bash s="abcde" len=${#s} for ((i=len-1;i>=0;i--)) do rev="$rev${s:$i:1}" echo "index=$i, rev=$rev" done
Output:
index=4, rev=e index=3, rev=ed index=2, rev=edc index=1, rev=edcb index=0, rev=edcba
Array operations - iterating array by index
In this section, we'll loop through via array index:
#!/bin/bash stars=(Andromeda Circunus Lupus Sagitta Virgo) for (( i=0; i<${#stars[@]}; i++ )) do echo ${stars[i]} done echo printf "%s\n" "${stars[@]}"
Output:
Andromeda Circunus Lupus Sagitta Virgo Andromeda Circunus Lupus Sagitta Virgo
Linux - system, cmds & shell
- Linux Tips - links, vmstats, rsync
- Linux Tips 2 - ctrl a, curl r, tail -f, umask
- Linux - bash I
- Linux - bash II
- Linux - Uncompressing 7z file
- Linux - sed I (substitution: sed 's///', sed -i)
- Linux - sed II (file spacing, numbering, text conversion and substitution)
- Linux - sed III (selective printing of certain lines, selective definition of certain lines)
- Linux - 7 File types : Regular, Directory, Block file, Character device file, Pipe file, Symbolic link file, and Socket file
- Linux shell programming - introduction
- Linux shell programming - variables and functions (readonly, unset, and functions)
- Linux shell programming - special shell variables
- Linux shell programming : arrays - three different ways of declaring arrays & looping with $*/$@
- Linux shell programming : operations on array
- Linux shell programming : variables & commands substitution
- Linux shell programming : metacharacters & quotes
- Linux shell programming : input/output redirection & here document
- Linux shell programming : loop control - for, while, break, and break n
- Linux shell programming : string
- Linux shell programming : for-loop
- Linux shell programming : if/elif/else/fi
- Linux shell programming : Test
- Managing User Account - useradd, usermod, and userdel
- Linux Secure Shell (SSH) I : key generation, private key and public key
- Linux Secure Shell (SSH) II : ssh-agent & scp
- Linux Secure Shell (SSH) III : SSH Tunnel as Proxy - Dynamic Port Forwarding (SOCKS Proxy)
- Linux Secure Shell (SSH) IV : Local port forwarding (outgoing ssh tunnel)
- Linux Secure Shell (SSH) V : Reverse SSH Tunnel (remote port forwarding / incoming ssh tunnel) /)
- Linux Processes and Signals
- Linux Drivers 1
- tcpdump
- Linux Debugging using gdb
- Embedded Systems Programming I - Introduction
- Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu/Fedora
- LXC (Linux Container) Install and Run
- Linux IPTables
- Hadoop - 1. Setting up on Ubuntu for Single-Node Cluster
- Hadoop - 2. Runing on Ubuntu for Single-Node Cluster
- ownCloud 7 install
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox I
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox II
- Windows 8 guest on Mac OSX host using VirtualBox I
- Ubuntu Package Management System (apt-get vs dpkg)
- RPM Packaging
- How to Make a Self-Signed SSL Certificate
- Linux Q & A
- DevOps / Sys Admin questions
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization