While loop, input from find command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers While loop, input from find command
# 1  
Old 08-14-2014
While loop, input from find command

Hello nix Experts,

I am a *nix rookie and have run into this issue, can some one help me here and let me know what I am doing wrong.

Code:
/home/user1> while read n
> do
> echo $n
> done < <(find . -type f -ctime -1 | grep abc)

I am getting the below error:
-sh: syntax error near unexpected token `<'

When I do a shell check to see what Shell I am using by default, i get this:
Code:
/home/user1>which shell
/usr/bin/which: no shell in (/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/opt/ibm/director/bin)

Any suggestion is greatly appreciated.
# 2  
Old 08-14-2014
Try echo $SHELL to find your shell. Or just ps. "which shell" looks for a command named shell, which isn't much help.

How about this, with -name to avoid the grep. This checks in the filename. -path would check in the file path instead.

Code:
find . -type f -ctime -1 -name '*abc*' | while IFS="" read -r FILE
do
        echo "$FILE"
done



The IFS avoids having read split on anything, and the -r prevents it from trying to evaluate backslashes. It's a good habit when you want read to give you literal, unchanged input...

One caveat: Variables you set inside the "while" won't be set outside the loop. The | puts the loop into a separate, independent subshell.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-14-2014
Quote:
Originally Posted by Corona688
One caveat: Variables you set inside the "while" won't be set outside the loop. The | puts the loop into a separate, independent subshell.
Depends on the shell.. Smilie
Code:
$
$ cat kshlooptest
#! /bin/ksh
LAST="nothing"
find /etc -name 'w*' 2> /dev/null | while IFS="" read name ; do
        LAST=$name
done
echo outside of while loop LAST = $LAST
exit 0
$
$
$ ./kshlooptest
outside of while loop LAST = /etc/gconf/schemas/window-list.schemas
$

# 4  
Old 08-14-2014
Quote:
Originally Posted by Perderabo
Depends on the shell.. Smilie
Yes, I know. ksh yes, absolutely-anything-other-than-ksh no.

This cuts both ways incidentally. It's possible for ksh to break a script from variables behind a pipe having a different scope than specified by POSIX.
# 5  
Old 08-14-2014
Thanks for the reply guys, that was helpful
# 6  
Old 08-14-2014
The current standard says:
Quote:
A subshell environment shall be created as a duplicate of the shell environment, except that signal traps that are not being ignored shall be set to the default action. Changes made to the subshell environment shall not affect the shell environment. Command substitution, commands that are grouped with parentheses, and asynchronous lists shall be executed in a subshell environment. Additionally, each command of a multi-command pipeline is in a subshell environment; as an extension, however, any or all commands in a pipeline may be executed in the current environment. All other commands shall be executed in the current shell environment.
So, variable assignments in a while loop in a pipeline are allowed, but not required, to be visible in the current shell execution environment after the pipeline terminates.

The last element of a pipeline is run in the current shell execution environment in ksh.
The last element of a pipeline is run in a subshell environment in bash.
On this point, both are OK according to the standard.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

HELP - loop a curl command with different variables from input file

Hi guys! Kind of new to bash scripting and now I'm stuck. I need to curl with these variables: "{ \"nodename\": \"$1\", \"ipaddress\": \"$2\", \"poolname\": \"$3\", \"port\": \"$4\", \"loadbalancer\" : \"$5\" }" and my input_file.txt contains server001 10.10.10.01 serverpool1 80... (4 Replies)
Discussion started by: yort
4 Replies

2. UNIX for Dummies Questions & Answers

find command in for loop

i have executed the following command in terminal find /Users/vasu -name "*.txt" -print and i am getting the result /Users/vasu/file1.txt /Users/vasu/file2.txt /Users/vasu/file3.txtbut while i was trying to execute the same in the script it is not working,I tried with below logic in... (2 Replies)
Discussion started by: vmachava
2 Replies

3. Shell Programming and Scripting

How to redirect a input of find command into a text file

Hello friends, I want a command to print the reult files from find command into a text file.:) Iam looking from forum memebers. PLZ help me.ASAP Thanks in Advance, Siva Ranganath CH (5 Replies)
Discussion started by: sivaranga001
5 Replies

4. Shell Programming and Scripting

Requesting input and for loop

Test - Test (1 Reply)
Discussion started by: Amit Sura
1 Replies

5. UNIX for Advanced & Expert Users

Help-prompt for path and take this as input in find command

HI , I am trying to wite a script that will prompt me saying " what is path that you want to find ?". once i specify the path, the script should put this path in the find command mentioned below and execute the script: find <path> -ctime +200 -type f -exec ls -l {} \; for example : ... (7 Replies)
Discussion started by: bsandeep_80
7 Replies

6. UNIX for Dummies Questions & Answers

problem with output of find command being input to basename command...

Hi, I am triying to make sure that there exists only one file with the pattern abc* in path /path/. This directory is having many huge files. If there is only one file then I have to take its complete name only to use furter in my script. I am planning to do like this: if ; then... (2 Replies)
Discussion started by: new_learner
2 Replies

7. Shell Programming and Scripting

read command (input) inside the while loop

Hi, 'read' command is not working inside the while loop, How can I solve this? Rgds, Sharif. (2 Replies)
Discussion started by: sharif
2 Replies

8. Shell Programming and Scripting

find command in while loop - how to get control when no files found?

I have the following statement in script: find ${LANDING_FILE_DIR}${BTIME_FILENAME_PATTERN2} -print | while read file; do ... done When there are no files located by the find comand it returns: "find: bad status-- /home/rnitcher/test/....." to the command line How do I get control in... (3 Replies)
Discussion started by: mavsman
3 Replies

9. Shell Programming and Scripting

How to combine "find" command in for each loop (tcsh)

Hello I was wandering if I can combine find command in side for each loop in unix the main propose is to change some thing in files from several types and not all of them is this possible ? (on liner script? ) tnx for the helppers (3 Replies)
Discussion started by: umen
3 Replies

10. UNIX for Dummies Questions & Answers

find files and using them as input arguements for another command

I need to do the following: 1) find files in certain directories that have todays date stamp 2) use these files as input arguements into another command (1 Reply)
Discussion started by: bobbygrep
1 Replies
Login or Register to Ask a Question