1

I have a text file that contains a list of folder names, one per line, that I want to access in a directory. I intend to use the script as follows:

main.sh folderlist.txt foldername/

Here is the code:

#!/bin/bash
DIRFILE=$1;
INPUTBASEDIR=$2;
while read dir; do
    echo "$INPUTBASEDIR$dir"
    ls -l "$INPUTBASEDIR$dir"
done<"$DIRFILE"

When echoing the concatenated path, it outputs the correct and existing path. However, the ls line spits out : No such file or directory. What must I do to have the second line properly output the files under that directory?

5
  • 1
    Script works for me, maybe the path doesn't exist, have you checked for any weird characters or misplaced capitals in your file ? Can you give an example of a directory. If you aren't using the full path it will try to access it from your current directory and may not find it . Commented Feb 12, 2015 at 10:02
  • 1
    possible duplicate of Bash - :No such file or directory Commented Feb 12, 2015 at 10:17
  • @tripleee Not at all! The reasons for "No such file ..." in the two posts are very different. The other one is not even trying to access files. Commented Feb 14, 2015 at 23:16
  • @Abhay Actually, discussion at stackoverflow.com/questions/28472424/… resolved this. If you can find a better duplicate where the answer is "DOS carriage returns", please do mark that as the duplicate instead. We get tons of these, so it's not at all unlikely that there is a better duplicate. Commented Feb 15, 2015 at 6:31
  • @tripleee Wow. Yes, certainly. Commented Feb 15, 2015 at 6:46

1 Answer 1

1

When echoing the concatenated path, it outputs the correct and existing path.

One cannot be sure it is "correct and existing path". Command echo simply outputs the argument string. Whether the file/directory exists or not does not matter.

Only ls looks for the file. So it appears to me that there are some entries in folderlist.txt which are not in foldername.

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.