0

do you know why in this command, $x is visible

ls -d */ $1 | while read x; do echo $x; done

return the sub directories in the current directory.

However, when I put in another command, $x seems to have no value

ls -d */ $1 | while read x; do ls -lat /order | grep $x; done

return nothing

6
  • Well, the second loop lists the files in /order directory, which is not self-evidently related to */ or $1. Did you mean to use something else in place of /order? Commented Feb 24, 2016 at 20:14
  • My intention is to use the result from the ls -d */, say 123, and ls -lat /order | grep 123 Commented Feb 24, 2016 at 20:19
  • 2
    Is there a file /order/123? Or a file in /order with 123 in any of the characteristics shown by ls -lat? (length including digits 123, mainly). If not, the grep won't find anything. Commented Feb 24, 2016 at 20:21
  • Oh I see it now. the outcome of ls -d */ contains the / character, so it is 123/. Of course when I grep for 123/ it won't find anything Commented Feb 24, 2016 at 20:23
  • 1
    Each pipe | separates commands that are run in their own separate subshells. You cannot split a loop across two separate processes. Commented Feb 24, 2016 at 20:51

1 Answer 1

1

there is a / at the end of output before the pipe, you should filter it.

ls -d */ $1 | while read xx; do ls -l /order |grep `echo ${xx}|sed  's/.$//'`  ; done
Sign up to request clarification or add additional context in comments.

1 Comment

ls -d */ $1 | tr / ' ' | while read x; do ls -l /order | grep $x; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.