0

I have piece of code and want it to search for a string in a particular path. The script will take input strings from an input file using a while loop. Basically, I want to read that file line by line and try to find out whether the content of that line exists in the particular path or not.

#!/bin/bash

filename='/input.txt'
path=/

while read -r line
do
    res=`ls -l $path | grep $line`
    echo "$res"
done < "$filename"
6
  • 3
    idownvotedbecau.se/imageofcode Please paste in your code using the appropriate code markdown. You should also go to the Help Center to see how to create a Minimal, Complete and Verifiable Example. Commented May 9, 2018 at 18:05
  • 3
    Don't parse ls. Commented May 9, 2018 at 18:17
  • Why is most of the code commented out? Commented May 9, 2018 at 18:49
  • As the question stands, it is unclear exactly what problems you are facing. I suggest following chepner's advice and changing your file access strategy, if you are still facing issues, then edit your question with the relevant errors/code and a more specific explanation of what is blocking you Commented May 9, 2018 at 19:00
  • @WillBarnwell Hi, actually I have tried all the ways like using if command and after that I actually want to know what is the actual output so I have commwnted for some. Commented May 10, 2018 at 2:27

1 Answer 1

1

Instead of ls, use find:

find $file -maxdepth 1

If you need to grep for the full paths, ensure that you are passing the absolute/canonical path to find:

find `readlink -f $file` -maxdepth 1
Sign up to request clarification or add additional context in comments.

Comments

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.