Bash For Loop Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash For Loop Help
# 1  
Old 04-07-2013
Wrench Bash For Loop Help

This is a short program I wrote to search through a directory and move files containing the keyword to a folder.
Code:
#!/bin/bash

echo 'What is the directory?'
read DIR
echo -e '\n'

echo 'What is the keyword?'
read KEY

echo -e '\n'

cd $DIR
rmdir 'relevant_files'
mkdir 'relevant_files'

for entry in 'grep -ilr $KEY $DIR'; do
    echo $entry
    mv $entry 'relevant_files'
done

The main problem I'm having with is the for loop.
Code:
for entry in 'grep -il $KEY $DIR'; do
    echo $entry
    mv $entry 'relevant_files'
done

My question is, how would I make the for loop entries equal to each file grep comes up with in the directory?
# 2  
Old 04-07-2013
No need to use grep. Use wildcards * or ? to perform expansion referred as globbing:
Code:
for entry in ${KEY}* ; do
    echo "$entry"
    mv "$entry" relevant_files/
done

# 3  
Old 04-07-2013
Quote:
Originally Posted by Yoda
No need to use grep. Use wildcards * or ? to perform expansion referred as globbing:
Code:
for entry in ${KEY}* ; do
    echo "$entry"
    mv "$entry" relevant_files/
done

I'm not searching for a filename, I'm searching for text within the file. Sorry for explaining poorly.
# 4  
Old 04-07-2013
Quote:
Originally Posted by zenyoul
I'm not searching for a filename, I'm searching for text within the file. Sorry for explaining poorly.
Code:
for file in *
do
        if grep "$KEY" "$file" 1> /dev/null 2> /dev/null
        then
                echo "$file"
                mv "$file" relevant_files/
        fi
done

This User Gave Thanks to Yoda For This Post:
# 5  
Old 04-08-2013
Quote:
Originally Posted by Yoda
Code:
for file in *
do
        if grep "$KEY" "$file" 1> /dev/null 2> /dev/null
        then
                echo "$file"
                mv "$file" relevant_files/
        fi
done

I usually prefer:
Code:
        if grep -q "$KEY" "$file"

instead of:
Code:
        if grep "$KEY" "$file" 1> /dev/null 2> /dev/null

because I want to see diagnostic messages if one or more of the files being processed can't be opened for reading, or have been renamed or deleted by something else while this script is also processing files in this directory.
# 6  
Old 04-08-2013
You want to mv all files that contain a certain key, you want to do it recursively, and you want to know the filenames moved? Try
Code:
 mv -v  $(grep -ril "$KEY" *) relevant_files/

Nor for loop nor echo required.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on for loop in bash

Hi, In the code "for loop" has been used to search for files (command line arguments) in directories and then produce the result to the standard output. However, I want when no files are named on the command line, it should read a list of files from standard input and it should use the command... (7 Replies)
Discussion started by: Ra26k
7 Replies

2. Shell Programming and Scripting

Speed up bash loop?

I am running the below bash loop on all the files of a specific type (highlighted in bold) in a directory. There are 4 awk commands that use the input files to search another and look for a match. The input files range from 27 - 259 and are a list of names. The file that is searched is... (11 Replies)
Discussion started by: cmccabe
11 Replies

3. Shell Programming and Scripting

Bash Shell loop - Help !

Dear all Linux lover, I am a new learner to Bash Shell script and I would like to writing a script to to repeat my script. This mean I would like to have multiple same of result after running the .sh. ####### TIMES_NO=0 echo -n "Please enter the number for times to repeat ?" read... (10 Replies)
Discussion started by: Rocky888
10 Replies

4. UNIX for Dummies Questions & Answers

Help with 3 variable bash loop

Hi all! I think someone might be able to solve my problem pretty easily. I am trying to run a bash loop with 3 variables. I know how to do: for var1 in `cat list1`; do for var2 in `cat list2`; do for var3 in `cat list3`; command var1 var2 > var3; done; done; done However, this will run all... (4 Replies)
Discussion started by: torchij
4 Replies

5. Shell Programming and Scripting

If loop in bash

Hello, I have a script that runs a series of commands. Halfway through the script, I want it to check whether everything is going alright: if it is, to proceed with the script, if it isn't to repeat the last step until it gets it right. My code so far looks like this, simplified a bit: ... (3 Replies)
Discussion started by: Leo_Boon
3 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. Shell Programming and Scripting

Problem with for loop in bash

I'm trying to do a script where I want to see if all users home directories are only writable by owner. However, in my script I do not know how to implement the for loop so that all directories are checked. In mine, I am only checking the permissions for the first directory found. I do know that a... (3 Replies)
Discussion started by: detatchedd
3 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

10. Shell Programming and Scripting

Simple loop in Bash

Hi, I want to do a simple loop where I have one column of text in a file and I want the loop to read each line of the file and do a simple command. The text file will be something like this: hostname1 hostname2 hostname3 hostname4 I am using Bash and have already come up with this to... (1 Reply)
Discussion started by: BrewDudeBob
1 Replies
Login or Register to Ask a Question