Page 1 of 1
Reading Filenames into an array
Posted: Thu Mar 21, 2019 12:23 pm
by doubleudee1
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
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 12:33 pm
by topguy
And the error doesnt report any specific line number ?
Do the error go away if you remove:
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 12:44 pm
by doubleudee1
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:
No, I've tried commening out that line and I still get the same error?
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 2:18 pm
by B.Goode
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
Question: does that result in an unexpected "//" in the resulting string. Is that valid in the context where you try to use that result?
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 2:30 pm
by doubleudee1
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
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
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 2:35 pm
by B.Goode
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.
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 2:59 pm
by doubleudee1
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
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 3:10 pm
by B.Goode
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?
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 3:29 pm
by doubleudee1
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
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 3:33 pm
by B.Goode
No space!
The script may need to be made executable first:
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 3:48 pm
by topguy
Code: Select all
pi@raspberrypi:~ $ ./ gdgfdf
-bash: ./: Is a directory
You really didnt see the connection ??
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 3:51 pm
by doubleudee1
B.Goode wrote: ↑Thu Mar 21, 2019 3:33 pm
No space!
The script may need to be made executable first:
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
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 4:03 pm
by B.Goode
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?
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 5:13 pm
by doubleudee1
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
Re: Reading Filenames into an array
Posted: Thu Mar 21, 2019 5:23 pm
by B.Goode
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!)