assingn a variable a filename and then reading it in


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers assingn a variable a filename and then reading it in
# 1  
Old 07-23-2009
assingn a variable a filename and then reading it in

Im trying to set a filename to a variable and then read the file in using the variable but im getting a syntax error. any ideas?

#!/bin/bash



function scanFile()
{
while read $1
do
echo $filename
done
}

file1=report.log


scanFile() $file1
# 2  
Old 07-23-2009
Leave off the () when calling the function, its only meant for when you're declaring it, and is optional even then.

read does not work that way. read NAME does not read from the file NAME, it reads into the variable $NAME. It usually reads from standard input, redirection is used if you want to change that. You'd want to do this:

Code:
function scanFile()
{
        # Local variable, incase you have FILENAME as a global variable somewhere else
        local FILENAME
        while read FILENAME
        do
                echo "$FILENAME"
        done < "$1"

        # Because BASH breaks the loop before processing the last input in some circumstances
        [ ! -z "$FILENAME" ] && echo "$FILENAME"        
}

# 3  
Old 07-23-2009
do i need to set file1=report.log outside the variable still.

report.log is in the same directory i will be running my script from.

can i not set report.log to a variable and then use that variable in the loop?
# 4  
Old 07-23-2009
Quote:
Originally Posted by magnia
do i need to set file1=report.log outside the variable still.
What does "outside the variable" mean?
Quote:
can i not set report.log to a variable and then use that variable in the loop?
You mean, just have the function get a name from a global variable instead of a parameter? Yes, you could, just replace "$1" with "$file1", but its generally a bad idea to use or modify global variables inside a function; If you ever forget which things it modifies or uses, you could be in for big headaches later when it starts doing things you didn't expect for no apparent reason.

I do sometimes make exceptions for log files myself, but I'm not certain how you're going to be using this.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Open Source

Splitting files using awk and reading filename value from input data

I have a process that requires me to read data from huge log files and find the most recent entry on a per-user basis. The number of users may fluctuate wildly month to month, so I can't code for it with names or a set number of variables to capture the data, and the files are large so I don't... (7 Replies)
Discussion started by: rbatte1
7 Replies

2. Shell Programming and Scripting

reading filename in nested directories

Hi all, I have some directory structure as $ls -ltr drwxr-xr-x 1 mscprod us_msc 512 May 10 08:34 650 drwxr-xr-x 1 mscprod us_msc 512 May 10 08:51 652 drwxr-xr-x 1 mscprod us_msc 512 May 10 08:51 640 drwxr-xr-x 1 mscprod us_msc512 May 10 09:13 654 $ cd 650/ $ls fileabc.root and within... (7 Replies)
Discussion started by: emily
7 Replies

3. Shell Programming and Scripting

Need help in reading a filename

Hi Guys, I have the following input. test_junk_file__20120210092009.txt latest_file__20120210092009.txt side_load_junk_file__20120210092009.txt I need to exclude the timestamp part from the file. so, the output should be as follows. test_junk_file.txt latest_file.txt... (7 Replies)
Discussion started by: mac4rfree
7 Replies

4. Homework & Coursework Questions

Matlab help! Reading in a file with a variable filename

1. The problem statement, all variables and given/known data: I want to read in a file, and plot the data in matlab. However, I do not like hardwiring filenames into my codes, so I always give the user the option to specify what the filename is. I am pretty inexperienced with matlab, so I have no... (0 Replies)
Discussion started by: ds7202
0 Replies

5. UNIX for Dummies Questions & Answers

Reading filename from directory

I am using the following code to read filename from the directory: for i in ` ls $inputDir | grep $partialName*.csv` do echo $i done But the echo is giving me the following: ls | grep cm_ctx*.csv instead of the full filename "cm_ctx_2009_07_15_17_18.csv" Any ideas anyone? I... (2 Replies)
Discussion started by: khanvader
2 Replies

6. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

7. Shell Programming and Scripting

variable used as filename

Hello, i'm fairly new to scripting, so please bear with me (I did try looking this up first, i figured it had to have been asked already). #!/bin/bash fileName=`date | sed -n 's/ /_/g p' | sed -n 's/^/Backup_/p' | sed -n 's/$/\.tar/p'`; #THIS SETS BACKUP_DATE echo $fileName #TEST OF VALUE ... (4 Replies)
Discussion started by: jzacsh
4 Replies

8. UNIX for Dummies Questions & Answers

get the latest file by reading the date in the filename.

Hi, I grep for a pattern in a list of files. "grep -i -l $pattern *.datx*" it may give me n number of files. say for eg, it gives me 2 files. lock_eicu_20071228_00000000.dat_20071228_05343100 lock_eicu_20080501_00000000.dat_20080501_05343900 out of these 2 files I need to get the... (7 Replies)
Discussion started by: prsshini
7 Replies

9. Shell Programming and Scripting

mv Filename variable to another filename

Anyone who can assist : I am trying to pass the group vairiable to a filename: rpt_tsavegrp=/export/legato/scripts/$group_savegrp_rpt.$dat It will not pass to variable. Anyone have any ideas what I am doing wrong here. Thanks # This script sends email that save group completed.... (3 Replies)
Discussion started by: gzs553
3 Replies

10. Shell Programming and Scripting

Reading a file using sh with spaces in filename

Hi I am trouble parsing through a file with spaces in the filename. I need to grab "supportIDPS/SCM/windows_install/file groups/dds.fgl" and then do a md5sum on it. I am using sh. Any help is appreciated. Here is an example of the input file: 7eedbc9f7902bf4c1878d9e571addf9a ... (4 Replies)
Discussion started by: jekl5
4 Replies
Login or Register to Ask a Question