Quantcast
Channel: User Kamil Maciorowski - Super User
Viewing all articles
Browse latest Browse all 656

Answer by Kamil Maciorowski for ls -ltr on the terminal lists the files but says "No such file or directory" in a shell script

$
0
0

What am I doing wrong?

Several things that are rather common bugs when you are "new to Bash".

In echo \"$line\" the double-quotes are escaped, not special, they get to the output and $line is unquoted, which is by itself wrong. You actually see the quotes in the file. Then they get stored in $line (at the second read). And then this happens: quotes in variables are not special.

A basic fix is to replace echo \"$line\" with echo "$line". This won't make the script good and robust, but at least it will work in most cases.

There is more to improve:

  • There should be a shebang.

  • -name "*" in find is a test that always succeeds.

  • Do not use echo to print uncontrolled data.

  • Use IFS= read -r line also with the first read. Or better get rid of this read because…

  • Reading lines and storing in a file, only to read lines from the file to process further seems over-complicated. If you really want to process pathnames this way, pipe from grep directly to the second while.

  • You don't even need this grep. Append ! \( -name '*.git*' -prune \) to your first find and get rid of grep.

  • This line:

    find "$line" -type f -name "*"  #| wc -l <<<a | echo "$line :  $((a-1))"

    contains commented code that looks like a remnant of some voodoo scripting. I guess at some point this was not a comment, just weird code. In particular:

    • wc -l <<<a reads the here string (literally a) from its stdin and there is no point in piping anything to it, the here string will "win".
    • There is no point in piping to echo, it does not read its stdin anyway.

Some ideas about counting files: here.


Viewing all articles
Browse latest Browse all 656

Trending Articles