Originally Posted by
vaibhavs17
Actual problem: I need to write a shell script which can give me difference character by character not by line (using comm)
Personally, I would rather eat glass than use bash for this but here is a starting point:
Code:
#!/bin/bash
i=0
while read line; do
file[$i]=$line;
i=$((i+1));
done < "test.sh"
i=0
while [ $i -lt ${#file[*]} ]; do
j=0
while [ $j -lt ${#file[$i]} ]; do
echo ${file[i]:$j:1};
j=$((j+1));
done;
i=$((i+1));
done;
Doesn't that look like fun? This stores "test.sh" in an array of lines, then prints each line one character at a time, like:
#
!
/
b
i
n
/
b
a
s
h
You cannot have multi-dimensional arrays in bash, but if an array element is a string you can deal with the individual characters in a string. For more on arrays:
Arrays
Remember: bash is whitespace sensitive!!!! Also, if you run this script on itself notice what happens with the asterisk on line 10 -- demonstrating shell scripts can be dangerous in unpredictable ways (this one isn't dangerous).