I am a newbie to unix and i have been trying to code a shell script for bash shell which reads the files in a folder using ls and writes them into an array variable. But, i am running into several errors while running the script. The script is given below:
It gives me the following error when i run the above code:
When i run the command :
individually on the command line, i get a list of all the files neatly.
Thanks for the reply. I could populate it by directly assigning the output of the ls -l to a variable but, the output of the ls symbol will be loaded into the 0 th position of the testarray , i.e, unix treats it as an normal variable but not as an array variable.
for the grep statement:
here is the error it is throwing:
Any help is highly appreciated.
-A
Quote:
Originally Posted by Chubler_XL
Try this grep -Ev '^\.$|\.\.$' for your grep statement.
You can also populate the array in one go with:
---------- Post updated at 08:50 AM ---------- Previous update was at 07:05 AM ----------
I tried out a new approach and something is going wrong. I am not sure what or why. Could anyone help me tweek the code and make it work?
Error that i am encountering:
script.sh: syntax error at line 12: `(' unexpected
The error is occuring when i am trying to assign the value of ls -l to the variable filename.
---------- Post updated at 09:49 AM ---------- Previous update was at 08:50 AM ----------
Hello All,
I came up finally with a way to assign the values of the filename to an array variable but, the problem is all the values are getting assigned to the first position of the array variable. This is because the count/num variable pointing to the location of the array is not incrementing.
Could anyone please help with this?Help is highly appreciated!!
Last edited by Franklin52; 04-24-2012 at 11:05 AM..
Reason: Please use code tags
I am so glad you dropped the ls. You are using bash. Learn it's great features. I am not quite sure what you are trying to accomplish though this is what I gather.
1. Find all files ending with ".sql" in current directory
2. Place them all in an array.
3. Count how many elements are in the array.
Last edited by neutronscott; 04-24-2012 at 12:13 PM..
Reason: stray code tag
Thanks for the reply. I realized ls was not a good command to go with. I replaced it. It works great. I figured how to increment as well. The problem now is i am not sure how to incorporate a for loop to print the values stored in the array.
I am trying to write all the filenames with .sql extention into an array and manipulate it likewise.
Any help with the printing of array is greatly appreciated.
Its just getting even more annoying when i am a newbie to this.
ok. `expr` is unnecessary. also, assignment is done without any spaces around the "=":
is the least invasive way to change your approach.
but the code i gave is much better. if you still need to loop over files then to do some processing:
edit: forgot to set nullglob, so that if no file exist the array will be empty instead of having literal '*.sql'
Last edited by neutronscott; 04-24-2012 at 02:15 PM..
Hello, I have a series of files in sub-directories that I want to loop through, process and name according to the input filename and the various parameters I'm using to process the files. I have a number of each, for example file names like AG005574, AG004788, AG003854 and parameter values like... (2 Replies)
hi,
i am writing a shell script in which i read a line in a variable.
FNAME="s1.txt s2.txt s3.txt s4.txt s5.txt"
i want to create a array and store single file names in a array..
so the array should contain
arr="s1.txt"
arr="s2.txt"
arr="s3.txt"
arr="s4.txt"
arr="s5.txt"
how to... (3 Replies)
I need to get the names of files which are starting with a string testfile. Also i want to create a XML file in the same location and write these file names into the XML.
Ex:
<path>
<dir>
<file>testfile1</file>
</dir>
<dir>
<file>testfile2</file>
</dir>... (4 Replies)
Hi,
I have a bash script that currently holds some data. I am trying to write all the contents to a file called temp.txt.
I am using
echo ${array} > temp.txt
The problem that I am experiencing is that the elements are being written horizontally in the file. I want them written... (5 Replies)
I have an array and two variables as below,
I need to check if $datevar is present in $filename.
If so, i need to replace $filename with the values in the array.
I need the output inside an ARRAY
How can this be done.
Any help will be appreciated. Thanks in advance. (2 Replies)
Hi,
I'd like to write a script which works on various files file1.jpg, file2.jpg ..... These files are splitted and their names are something like file_.XXX. I'd like to merge them and convert them at some moment again in file.XXX also
file1_1.jpg file1_2.jpg ... >file1.pdf
file2_1.txt... (2 Replies)
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
make 2 files. one to Input 10 numbers and print out the biggest number, and one to Write a script that can check... (1 Reply)
im in a basic unix class and our professor speaks broken engliash so i can never understand what exactly we are doing in class and i have no prior experience with unix. we were given an assignment to make 2 files. one to Input 10 numbers and print out the biggest number, and one to Write a script... (1 Reply)
NEWBIE ALERT!
Hi,
I'm 1 month into learning Perl and done reading "Minimal Perl" by Tim Maher (which I enjoyed enoumously). I'm not a programmer by profession but want to use Perl to automate various tasks at my job. I have a problem (obviously) and are looking for your much appreciated help.... (0 Replies)