doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Reading Filenames into an array

Thu Mar 21, 2019 12:23 pm

I have this script from a site that i thought would work, that I altered to suit, but I am getting an error '-bash: ./: Is a directory'

Code: Select all

#!/bin/bash
SOURCE_DIR=/home/pi/CCTV/RearOfHouse/TestDirs/
files=(
   "$SOURCE_DIR"/*.jpg
   "$SOURCE_DIR"/*.mp4
   "$SOURCE_DIR"/**/*
)

printf '%s\n' "${files[@]}" # i.e. path/to/source/filename.jpg

printf '%s\n' "${files[@]##*/}" # i.e. filename.mp4

#ERROR+ -bash: ./: Is a directory
Still learning the basics of coding here, can anyone help please?
Thanks
WD
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

User avatar
topguy
Posts: 6886
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Reading Filenames into an array

Thu Mar 21, 2019 12:33 pm

And the error doesnt report any specific line number ?

Do the error go away if you remove:

Code: Select all

"$SOURCE_DIR"/**/*

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: Reading Filenames into an array

Thu Mar 21, 2019 12:44 pm

topguy wrote:
Thu Mar 21, 2019 12:33 pm
And the error doesnt report any specific line number ?

Do the error go away if you remove:

Code: Select all

"$SOURCE_DIR"/**/*
No, I've tried commening out that line and I still get the same error?
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

User avatar
B.Goode
Posts: 10941
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Reading Filenames into an array

Thu Mar 21, 2019 2:18 pm

As an overall approach, as with conventional programming languages, lots of intermediate snapshots of the state of your variables might help? I think that in the bash shell the echo command might be the tool to use.


But, unverified, here is something to check out -


You have

Code: Select all

SOURCE_DIR=/home/pi/CCTV/RearOfHouse/TestDirs/
So the directory path terminates with "/"

Then you prepend it to a file path

Code: Select all

"$SOURCE_DIR"/*.jpg
Question: does that result in an unexpected "//" in the resulting string. Is that valid in the context where you try to use that result?

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: Reading Filenames into an array

Thu Mar 21, 2019 2:30 pm

B.Goode wrote:
Thu Mar 21, 2019 2:18 pm
As an overall approach, as with conventional programming languages, lots of intermediate snapshots of the state of your variables might help? I think that in the bash shell the echo command might be the tool to use.


But, unverified, here is something to check out -


You have

Code: Select all

SOURCE_DIR=/home/pi/CCTV/RearOfHouse/TestDirs/
So the directory path terminates with "/"

Then you prepend it to a file path

Code: Select all

"$SOURCE_DIR"/*.jpg
Question: does that result in an unexpected "//" in the resulting string. Is that valid in the context where you try to use that result?
sorry, same result using your suggested code
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

User avatar
B.Goode
Posts: 10941
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Reading Filenames into an array

Thu Mar 21, 2019 2:35 pm

sorry, same result using your suggested code

I didn't suggest any code...


If you have an error, share the code and the full text of any resulting error message.

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: Reading Filenames into an array

Thu Mar 21, 2019 2:59 pm

B.Goode wrote:
Thu Mar 21, 2019 2:35 pm
sorry, same result using your suggested code

I didn't suggest any code...


If you have an error, share the code and the full text of any resulting error message.

Code: Select all

###########################=============================##################
###########################CODE1
#!/bin/bash
SOURCE_DIR=/home/pi/CCTV/RearOfHouse/TestDirs *.jpg
files=(
   "$SOURCE_DIR"/*.jpg
   "$SOURCE_DIR"/*.mp4
#   "$SOURCE_DIR"/**/*
)

##You can then use printf to see the contents of the array including paths:
printf '%s\n' "${files[@]}" # i.e. path/to/source/filename.jpg

##
#Or using parameter substitution to exclude the pathnames:
printf '%s\n' "${files[@]##*/}" # i.e. filename.mp4


#######################=========================########################
The only error I get when running the code is:-

-bash: ./: Is a directory

Regards
WD
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

User avatar
B.Goode
Posts: 10941
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Reading Filenames into an array

Thu Mar 21, 2019 3:10 pm

The only error I get when running the code is:-

-bash: ./: Is a directory
What do you type at the shell (command line) prompt to run your shell script?

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: Reading Filenames into an array

Thu Mar 21, 2019 3:29 pm

B.Goode wrote:
Thu Mar 21, 2019 3:10 pm
The only error I get when running the code is:-

-bash: ./: Is a directory
What do you type at the shell (command line) prompt to run your shell script?
I put in :-

./ filename

from the same directory the file I created (above) exists in and then press enter
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

User avatar
B.Goode
Posts: 10941
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Reading Filenames into an array

Thu Mar 21, 2019 3:33 pm

No space!

Code: Select all

./filename
The script may need to be made executable first:

Code: Select all

chmod +x filename

User avatar
topguy
Posts: 6886
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Reading Filenames into an array

Thu Mar 21, 2019 3:48 pm

Code: Select all

pi@raspberrypi:~ $ ./ gdgfdf
-bash: ./: Is a directory
You really didnt see the connection ??

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: Reading Filenames into an array

Thu Mar 21, 2019 3:51 pm

B.Goode wrote:
Thu Mar 21, 2019 3:33 pm
No space!

Code: Select all

./filename
The script may need to be made executable first:

Code: Select all

chmod +x filename
Thanks, space taken out and file made executable, output now as expected: (except it doesn't like me declaring the type of file to look for)-

./CreateSecurityDirs: line 4: *.jpg: command not found
/*.jpg
/*.mp4
*.jpg
*.mp4
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

User avatar
B.Goode
Posts: 10941
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Reading Filenames into an array

Thu Mar 21, 2019 4:03 pm

I see the same!


Code: Select all

pi@RPi3BplusOffice:~ $ FRED=/home/pi  *.jpg                    
    -bash: *.jpg: command not found
pi@RPi3BplusOffice:~ $
What do you hope to achieve by the "*.jpg" part of that command - it looks superfluous, as proven by the possibility correct output that follows?

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: Reading Filenames into an array

Thu Mar 21, 2019 5:13 pm

B.Goode wrote:
Thu Mar 21, 2019 4:03 pm
I see the same!


Code: Select all

pi@RPi3BplusOffice:~ $ FRED=/home/pi  *.jpg                    
    -bash: *.jpg: command not found
pi@RPi3BplusOffice:~ $
What do you hope to achieve by the "*.jpg" part of that command - it looks superfluous, as proven by the possibility correct output that follows?
Sorry, I added the .jpg thinking it would pull out only those, my next task is to see if I can pull out a section/clip of the file, i.e. starting at point 3 and pull out the next 3 chars.
Many thanks for everyone's help.
WD
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

User avatar
B.Goode
Posts: 10941
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Reading Filenames into an array

Thu Mar 21, 2019 5:23 pm

You might find something like this helpful to understand the syntax of bash array indexing...

https://www.thegeekstuff.com/2010/06/ba ... y-tutorial


Nos da - Dewi. (David - also a proud and busy taid!)

Return to “Other programming languages”