linux – Determine the end of the loop with a shell script

Question: Question:

I have a shell that lists the filenames in a folder every 5 seconds as shown below.

for file in `ls /home/test/`; do
    echo "${file}"
    sleep 5

I don't want to run sleep 5 only at the end of the loop because I don't have to wait 5 seconds after displaying the last filename, how do I write it?

Answer: Answer:

I'm a entangler, but I try to sleep for 5 seconds before evaluating echo.
If you don't sleep the first time, you'll exit the loop after the last one ends.

for file in `ls .`; do
  if [ $count -ne  0 ]; then
    sleep 5
  echo ${file}
  ((count++ ))
Scroll to Top