Reverse elements of an Array in Linux

In this blog, sharing code to reverse the array elements. arr=(1 2 3 4) ## Array of 4 Elementsarr_2=() ## Another Array of zero Elements or you can say blank Arraylen=${#arr[@]} ## It gives the length of an Array arrwhile [ $len -gt “0” ]dolen=$(( $len – 1))arr_2+=(${arr[len]})doneecho ${arr_2[*]} ##It will print (4 3 2 1)

Append elements of an Array to another Array

In this blog, sharing code of appending elements of one Array to another. arr=(1 2 3 4) ## Array of 4 Elementsarr_2=(5 10 12 1) ## Another Array of 4 Elementsi=0len=${#arr_2[@]} ## It gives the length of an Array arr_2while [ $i -lt $len ]doarr+=(${arr_2[i]}) ## Appending elements of arr_2 into array arr.i=$(( $i + 1))doneecho ${arr[*]}

Sum of all Array Elements in Linux

In this Blog, Sharing the code to add all elements of an Array using shell scripting. a =(1 2 3 4) ## Array of 4 Elementsi=0len=${#a[@]} ## It gives the length of an Arraysum=0 ## Setting sum variable to value 0while [ $i -lt $len ]dosum=$((${a[i]}+$sum))i=$(( $i + 1))doneecho $sum

Search Element In An Array in Linux

In this blog, Sharing the code to Search an Element in an Array using Shell scripting. a =(1 2 3 4) ## Array of 4 Elementsi=0x=2 ##x is the element which need to be searched in an Arraylen=${#a[@]} ## It gives the length of an Arraylen1=$(( $len – 1 )) ##len1 is used to display if element is not present in an array.while [ $i -lt $len ]doif [[ ${a[i]} -eq $x ]]thenecho $x is presentbreakfiif [[ $i -eq $len1 ]]then echo $x is not presentfii=$(( $i + 1))done

Perform Arithmetic Operations On Two Files

In this Blog , we will cover how to apply sum ,multiply or subtract operator on two files each having one field and same number of records. Consider the data in each file as below. Below is the Shell script which will multiply row by row from above two files and put the output in another file. arr1=()for line in cat linux.txt | tail -n+2doarr1+=(“$line”)donearr1_1=()for line in cat linux_1.txt | tail -n+2doarr1_1+=(“$line”)doneLenOfArr= ${#arr1[@]}touch newfile.txtecho “COL1” > newfile.txta=0while [ $a -le $((LenOfArr-1)) ]; do let ans=${arr1[a]}*${arr1_1[a]}; echo $ans >> newfile.txt; a=$((…