for $word in $line returns filenames in the current directory unexpectedly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for $word in $line returns filenames in the current directory unexpectedly
# 1  
Old 01-03-2011
for $word in $line returns filenames in the current directory unexpectedly

I am writing a script below, which has 2 loops. The outer one reads file sufffixed with a number and inner inside which loop through each line of the file and display each space delimited string. However, i find that the string printed out in the inner loop includes not only the delimited string in the file, but also the filename in the same directories.
Code:
#!/bin/bash
process_file="/home/eclipse/misc/crontab.all"
rm $process_file
for ((i =5;i<=5;i++))
do
        host="edev$i"
        filename="/home/eclipse/misc/$host.crontab"
        ssh eclipse@$host crontab -l|grep start|grep -v \# > $filename
        echo $filename
        echo "Processes on $host:" >> crontab.all
        while read line;
        do
                echo "line=$line"
                STR_ARRAY=(`echo $line | tr "," "\n"`)
                for y in "${STR_ARRAY[@]}"
                        do
                        echo "> [$y]"
                done
        done < $filename
done

The result is the same if i replaced the inner loop by:
Code:
for word in $line
do
    echo $word
done


Anyone can help? Thanks.


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples!

Last edited by Franklin52; 01-03-2011 at 04:38 AM..
# 2  
Old 01-03-2011
how is your crontab files? can you give a sample ?
and desired output?
# 3  
Old 01-03-2011
The line
Quote:
Originally Posted by martie
Code:
                STR_ARRAY=(`echo $line | tr "," "\n"`)

is causing your troubles. From crontab, a crontab entry is of the form:
Code:
minutes hours days months days-of-week command...

and most entries have at least one "*". So when the above line is evaluated, the "*" from the entry is treated as a wildcard, which expands to all the files in the current directory. You should use, at a minimum:
Code:
STR_ARRAY=(`echo "$line" | tr "," "\n"`)

Or you may want to change the while read lineto something like:
Code:
while read minutes hours days months dows command; do

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

2. Solaris

Tilde prefix returns invalid home directory.

I am trying to find the home directory of users on a UNIX (Solaris/AIX) box using echo ~usernameThis does return the home directory for all valid users. For some reason this command also outputs home directory which are non-existent for few users who seem not to have logon access to that... (31 Replies)
Discussion started by: thinkster
31 Replies

3. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

4. Shell Programming and Scripting

Printing filenames in my current directory

Can someone give me a tip on writing a script that, for each file in the working directory, prints the filename, the # of lines, and the # of words to stdout? (2 Replies)
Discussion started by: flash123
2 Replies

5. Shell Programming and Scripting

rm -rf ab returns find: `./ab': No such file or directory

Hi Gurus. This is driving me a bit batty. I now if must be a simple matter but I cant find anything that references it. I have a housekeeping script that searches for some huge dump directories then removes them using rm -rf. find ./ -name 'ab' -exec rm -rf {} \; This works but always... (7 Replies)
Discussion started by: rinser
7 Replies

6. UNIX for Dummies Questions & Answers

delete last word of each line a directory

I want to delete the last word of each line in all the files in one directory but dont know what I am doing wrong FILES="data/*" for X in $FILES do name=$(basename $X) sed s/'\w*$'// $X > no-last/${name} done Can you please help me :wall: (8 Replies)
Discussion started by: A-V
8 Replies

7. Shell Programming and Scripting

Word Count (wc -w) returns a non-integer value

Hi, Why is it I cannot compare the return value of 'wc -w' to an integer? Please see code below: a="x y z" b=`echo ${a} | wc -w` if ; then echo "less than 4" fi when I try to run error always happen (: integer expression expected) - error is on the if statement Please... (1 Reply)
Discussion started by: h0ujun
1 Replies

8. Shell Programming and Scripting

Getting filenames from a directory in single line

Hi All, I have a requirement where I need to get the all file names present in a particular directory in to a single separated by space, I have files in /home/RAAM/work directory as test1.xls test2.xls test3.xls I need to get all filenames from that directory in single line like ... (6 Replies)
Discussion started by: Raamc
6 Replies

9. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

10. Shell Programming and Scripting

Finding files in current directory when 100,000's files in current directory

Hi All I was wondering what is the most efficient way to find files in the current directory(that may contain 100,000's files), that meets a certain specified file type and of a certain age. I have experimented with the find command in unix but it also searches all sub directories. I have... (2 Replies)
Discussion started by: kewong007
2 Replies
Login or Register to Ask a Question