0

Here is my script, as simple as possible:

#!/bin/bash

for file in /home/pi/watch-dir/*.torrent; do
   echo "$file"
   sleep 2
done

Base on this answer How to iterate over files in a directory with Bash? I believe the for-in should see /home/pi/watch-dir/*.torrent as a list.

Inside the watch-dir directory:

pi@raspberry:~/watch_dir $ ls -al
total 68
drwxrwx---  2 pi pi  4096 Sep 15 18:59 .
drwxr-xr-x 12 pi pi  4096 Sep 15 19:03 ..
-rwxrwx---  1 pi pi 19065 Sep 15 18:59 asdf.torrent
-rwxrwx---  1 pi pi 19065 Sep 15 18:59 qwer.torrent
-rwxrwx---  1 pi pi 19065 Sep 15 18:59 zxcv.torrent

And this is the only-one-line-output after run the scipt, I really don't know what I am doing wrong...

pi@raspberry:~ $ ./test.sh
/home/pi/watch-dir/*.torrent
3

You've got watch-dir in your loop and watch_dir is the folder!

- != _
2
  • 1
    The loop 'watch-dir' contains a minus character. The actual folder 'watch_dir' contains an underscore character. – CoderMike Sep 15 '17 at 12:30
  • Accepted answer. Silly me :(( – Huy.PhamNhu Sep 15 '17 at 12:31
0

This is obviously nothing related to Raspberry Pi but to Linux and Bash scripting.

The for-in operator operates on a space separated (line separated? You can change it with IFS=<your separator>) lists.

IFS=" "
for x in alice bob cindy d e f; do
  echo $x
done

will output

alice
bob
cindy
...

There is no globbing or thatsoever. So instead of your glob you will have to get a listing of the actual files. Something like $(find . -type f -name "*.torrent")

So you should be able to achieve your intent with

#!/bin/bash

for file in $(find /home/pi/watch-dir -type f -name "*.torrent"); do
   echo "$file"
   sleep 2
done

$(ls *.torrent) should also work if you run the script from within the same directory or change into the dirctory within your script.

1
  • Base on this answer link I think the for-in should see /home/pi/watch-dir/*.torrent as a list right? – Huy.PhamNhu Sep 15 '17 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.