DesperateProgrammer
DesperateProgrammer

Reputation: 1

Shell no such file or directory

While trying to run a for loop in shell I encountered a problem.

for i in `cat $file`
do
`cut -d ',' -f 2- $i`
done

I tried to cut the lines from the second column and output them, however it gave me an error: (content of the file) no such file or directory

Upvotes: 0

Views: 372

Answers (1)

jhnc
jhnc

Reputation: 16762

First, you try to execute the output of the cut command.

Consider:

$ echo hello >file
$ cat file
hello
$ a=`cat file`
$ echo "$a"
hello
$ `echo "$a"`
-bash: hello: not found
$

Perhaps you just wanted to display the output of cut:

for i in `cat "$file"`
do
    cut -d , -f 2- $i
done

Second, you pass cut an argument that is expected to be a filename.

You read data from $file and use it as a filename. Is that data actually a filename?

Consider:

$ echo a,b,c,d >file
$ cat file
a,b,c,d
$ a=`cat file`
$ echo "$a"
a,b,c,d
$ cut -d , -f 2- file
b,c,d
$ cut -d , -f 2- "$a"
cut: a,b,c,d: No such file or directory

Perhaps you wanted:

cut -d , -f 2- "$file"

Thirdly, your for loop splits the data in "$file" on whitespace, not by line.

Consider:

$ echo 'a   b,c d' >file
$ cat file
a   b,c d
$ for i in `cat file`; do echo "[$i]"; done
[a]
[b,c]
[d]
$

Perhaps you wanted to read individual lines?

while read i; do
    : ...
done <file

Upvotes: 1

Related Questions