ali kapucu blog
Penetration Tester, Security & Network Design Engineer
Friday, April 17, 2015
Saturday, May 17, 2014
Using Array in Shell Scripting
We cannot imagine a programming language without the concept of arrays. It doesn’t matter how they are implemented among various languages. Instead arrays help us in consolidating data, similar or different, under one symbolic name.
Here as we are concerned about shell scripting, this article will help you in playing around with some shell scripts which make use of this concept of arrays.
Another convenient way of initializing an entire array is by using the pair of parenthesis as shown below.
To traverse through the array elements we can also use for loop.
Here as we are concerned about shell scripting, this article will help you in playing around with some shell scripts which make use of this concept of arrays.
Array Initialization and Usage
With newer versions of bash, it supports one-dimensional arrays. An array can be explicitly declared by the declare shell-builtin.But it is not necessary to declare array variables as above. We can insert individual elements to array directly as follows.declare -a var
where ‘XX’ denotes the array index. To dereference array elements use the curly bracket syntax, i.e.var[XX]=<value>
Note: Array indexing always start with 0.${var[XX]}
Another convenient way of initializing an entire array is by using the pair of parenthesis as shown below.
There is yet another way of assigning values to arrays. This way of initialization is a sub-category of the previously explained method.var=( element1 element2 element3 . . . elementN )
We can also read/assign values to array during the execution time using the read shell-builtin.array=( [XX]=<value> [XX]=<value> . . . )
Now upon executing the above statement inside a script, it waits for some input. We need to provide the array elements separated by space (and not carriage return). After entering the values press enter to terminate.read -a array
To traverse through the array elements we can also use for loop.
The following script summarizes the contents of this particular section.for i in “${array[@]}” do #access each element as $i. . . done
#!/bin/bash array1[0]=one array1[1]=1 echo ${array1[0]} echo ${array1[1]} array2=( one two three ) echo ${array2[0]} echo ${array2[2]} array3=( [9]=nine [11]=11 ) echo ${array3[9]} echo ${array3[11]} read -a array4 for i in "${array4[@]}" do echo $i done exit 0
Various Operations on Arrays
Many of the standard string operations work on arrays . Look at the following sample script which implements some operations on arrays (including string operations).Following is the output produced on executing the above script.#!/bin/bash array=( apple bat cat dog elephant frog ) #print first element echo ${array[0]} echo ${array:0} #display all elements echo ${array[@]} echo ${array[@]:0} #display all elements except first one echo ${array[@]:1} #display elements in a range echo ${array[@]:1:4} #length of first element echo ${#array[0]} echo ${#array} #number of elements echo ${#array[*]} echo ${#array[@]} #replacing substring echo ${array[@]//a/A} exit 0
I think there is no significance in explaining the above script in detail as it is self-explanatory. If necessary I will dedicate one part in this series exclusively on string manipulations.apple apple apple bat cat dog elephant frog apple bat cat dog elephant frog bat cat dog elephant frog bat cat dog elephant 5 5 6 6 Apple bAt cAt dog elephAnt frog
Thursday, March 7, 2013
Change network interface name on ubuntu
for the record, i would like to remind myself how to change network interface name on ubuntu.
and change configuration file as well

sudo nano /etc/udev/rules.d/70-persistent-net.rules
By default, it assigns the name eth0,eth1 etc to the NIC(s):
and change configuration file as well
Subscribe to:
Posts (Atom)