venkatrg - Firstly I commend you on your script as it is quite in depth. Second, if you use [code][/code] tags around your code it will maintain formatting and be a
lot easier to read.
As vikas has pointed out there are easier ways, but I would like to help you with what you have presented.
I will start from the top:
1. You refer to $1 all the way through the script. Are you aware that when the script is called that $1 is the first item on the command line after the script name and prior to a space but when you call
one of your own functions it is the first item after the function name prior to a space? I ask as it is very confusing from a reading point of view to know which $1 is being referenced.
2. Let me know what you think might happen if the following were the only lines in your code:
Code:
#Create the log file in the path
cd "$1"
cd "../frags"
#Delete all the files/folers in the ./frag directory
rm -rf *
Now we call the script but make a little typo in our haste:
Code:
./script / folder
# ^ this is a space between slash and word folder (typo)
Think about what might happen here??
3. Echo not necessary here:
Code:
IFS=$(echo -en "\n\b")
# could just be
IFS=$'\n\b'
I am also curious how backspace (\b) will be a separator??
4. In processFile function, why the need to set var=$line? Is there a reason you could not simply use $line in the places where you have $var?
5. No need to mix up testing options:
Code:
if [[ "$var" =~ "| 1 line" ]] && [ $firstline = "TRUE" ] ; then
#becomes
if [[ "$var" =~ "| 1 line" && $firstline = "TRUE" ]] ; then
6. You open file descriptor '3':
In both functions, but it is never actually closed. Line to close would be:
7. Escape (\) not required here:
8. The following compound if statement inside the while loop for function readlogfile has me confused:
Code:
while read -r line
do
#echo $line
#Remove the first line in the trunklog.txt file
if [ $first = "TRUE" ]; then
first="FALSE"
elif [ $first = "FALSE" ]; then
if [[ ! "$line" =~ ---------* ]]; then
echo "$line" >> "temp.txt"
else
processFile "temp.txt"
fi
fi
done
So if we start from the top of this snippet:
a. We using a while loop to read from the past in log file
b. The use of if then elif is not required as there are only 2 options for $first so this could become a simple if / else construct:
Code:
if [ $first = "TRUE" ]; then
first="FALSE"
else
if [[ ! "$line" =~ ---------* ]]; then
echo "$line" >> "temp.txt"
else
processFile "temp.txt"
fi
fi
c. Once in the else we then check if line contains dashes. If it does we call the function processFile. My issue here is that if the dashes are in the first line we look at then we will call function on a file that does not exist. I realise that the previous if is probably coping with this, but it might be an idea to test that the file exists prior to calling the function
9. When using =~ you are doing a regular expression comparison (of sorts) and so placing * at the end of the line is not required:
Code:
if [[ ! "$line" =~ ---------* ]]; then
# same result as
if [[ ! "$line" =~ --------- ]]; then
Once the match is made you do not care what comes after
I hope you do not take any of the above as negative. It is solely meant to aide you in improving what you have
Something to think about ... generally you will create functions for tasks that you repeat several times in code, but most of your tasks are fairly linear so it may
be just as easy to have most of this as a continuous code piece (just a thought)
Look forward to seeing how you go.