LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how can I improve this algorithm in bash (https://www.linuxquestions.org/questions/programming-9/how-can-i-improve-this-algorithm-in-bash-4175498899/)

mia_tech 03-20-2014 04:46 PM

how can I improve this algorithm in bash
 
basically I have a directory with files, and I want to output current file, previous file, next file. So here's my algorithm

Code:

for ((i=0; i<$total; i++)); do
 thisfile=${files[i]}
 if [ $i -eq 0 ]; then
  previousfile=""
 elif [ $i -eq $((total-1)) ]; then
  nextfile=""
 else
  previousfile=${files[$((i-1))]}
  nextfile=${files[$((i+1))]}
 fi
 echo $previousfile $thisfile $nextfile
done


colucix 03-20-2014 05:13 PM

Maybe you can avoid superfluous assignments:
Code:

for ((i=0; i<$total; i++))
do
  [[ $i == 0 ]] || echo -n "${files[$((i-1))]} "
  echo ${files[i]} ${files[$((i+1))]}
done


grail 03-20-2014 08:33 PM

If you are going to use (()) for the for statement, why not use them for the rest of your arithmetic expressions?
Code:

for ((i=0; i < total; i++)); do
 thisfile=${files[i]}
 if (( i == 0 )); then
  previousfile=""
 elif (( i == total - 1 )); then
  nextfile=""
 else
  previousfile=${files[i-1]}
  nextfile=${files[i+1]}
 fi
 echo $previousfile $thisfile $nextfile
done

I have highlighted in red all the changes I made.

I am curious to the output when i = 0, ie nextfile is not calculated, so strictly speaking, unless total is 1, this should
have a value.

Lastly, when the output shows a single file name (or any for that matter), how do you know which position this name is in?
This perhaps picky, but if the script is not just for you (or you run it in a months time and forget) it may
be tricky for a user to tell exactly what they are seeing without any labels ... just a thought ;)

mia_tech 03-21-2014 12:30 AM

Quote:

Originally Posted by grail (Post 5138420)
If you are going to use (()) for the for statement, why not use them for the rest of your arithmetic expressions?
Code:

for ((i=0; i < total; i++)); do
 thisfile=${files[i]}
 if (( i == 0 )); then
  previousfile=""
 elif (( i == total - 1 )); then
  nextfile=""
 else
  previousfile=${files[i-1]}
  nextfile=${files[i+1]}
 fi
 echo $previousfile $thisfile $nextfile
done

I have highlighted in red all the changes I made.

I am curious to the output when i = 0, ie nextfile is not calculated, so strictly speaking, unless total is 1, this should
have a value.

Lastly, when the output shows a single file name (or any for that matter), how do you know which position this name is in?
This perhaps picky, but if the script is not just for you (or you run it in a months time and forget) it may
be tricky for a user to tell exactly what they are seeing without any labels ... just a thought ;)

yeah, that's basically the same way I have it. I did it to make an array of pictures and create webpages with links to next and previous page.
So if you look at my script above, instead of echo to stdout I would call another function that has some html code and create a page for every picture with links to previous and next. Hope it makes sence.

grail 03-21-2014 01:31 AM

Well whilst this may be an improvement on the current code, I think colucix has presented the best option, ie use the array you have created which removes most if not all the if / else requirements


All times are GMT -5. The time now is 12:54 AM.