store files in a directory in an array
Hi all,
I am a newbie to shell scripting. I want to store the files of the directory in an array and then access these files from the array using for loop. I want to try the readdir command and i have written a bit of code. #!/bin/bash cd /home/tanmay/Desktop/BET/T1 dir=opendir files=readdir dir; Currently the output of this code is that,it successfully displays all the files on the terminal.But i am not able to store it in an array.Can you please help me in storing the files(the files variable) in an array? Also can you suggest some online material/tutorials where i can read about this shell scripting. Thanks |
Sincerely, I've never heard about the opendir and readdir commands, but provided they are available and work as expected, your script should be:
Code:
#!/bin/bash http://tldp.org/LDP/Bash-Beginners-Guide/html/ http://linuxcommand.org/tlcl.php (see the online version) Hope this helps. |
Do you actually need them in an array, or do you just need to loop over the files?
The simplest way (in my opinion) to loop over all of the files in a directory is: Code:
for i in *; do |
Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
I have never seen opendir/readdir either, and they certainly don't exist in bash. There are C library functions with those names, but I can't find them as stand-alone commands anywhere on my system or in the apt repositories. Could you explain what they are and where you learned about them? In any case, the OP code does not actually work at all. The only reason you're getting the output you do is due to this line: Code:
files=readdir dir; As for colucix's suggestion here: Code:
files=( $(readdir "$dir") ) http://mywiki.wooledge.org/Arguments http://mywiki.wooledge.org/WordSplitting http://mywiki.wooledge.org/Quotes The proper way to access files is generally with simple globbing. globbing filename expansion happens at the end of the parsing order, after word-splitting, and so isn't subject to that weakness. You can use it to populate an array, or directly in a loop, as in suicidaleggroll's example. Code:
cd directory |
Thanks for the help..For the opendir() command i found it on http://linux.about.com/library/cmd/blcmdl3_opendir.htm
and also i can see its manual in my bash when i write man opendir in my bash. Also, David's code give me the list of all the files. How can i access each and every files in the directory? Something like if i store all the filenames in an array named file and then access each file as file(1), file(2) etc. |
Quote:
Quote:
|
If you want to learn how to script, don't just flail around trying stuff at random and asking us how-to questions, find a real tutorial and start studying for yourself.
Here are a few useful bash scripting references: http://mywiki.wooledge.org/BashGuide http://mywiki.wooledge.org/BashFAQ http://mywiki.wooledge.org/BashPitfalls http://www.linuxcommand.org/index.php http://tldp.org/LDP/Bash-Beginners-G...tml/index.html http://www.tldp.org/LDP/abs/html/index.html http://www.gnu.org/software/bash/manual/bashref.html http://wiki.bash-hackers.org/start http://ss64.com/bash/ I recommend starting with the BashGuide, or perhaps linuxcommand. They'll walk you through the basic concepts. |
All times are GMT -5. The time now is 05:54 AM. |