Script does not show the entire path

Asked

Viewed 69 times

1

Code:

for i in `find $1 -name $4 -type f` ; do  
path=`readlink -f $4`  
words=`cat "$i" | wc -w`  
echo "$path: $words palavras"  
done  

Prints:

/home/miglui/Desktop/SO/teste.txt: 14 palavras  
/home/miglui/Desktop/SO/teste.txt: 48 palavras  
/home/miglui/Desktop/SO/teste.txt: 29 palavras

Should Print:

/home/miglui/Desktop/SO/teste/1/teste.txt: 14 palavras  
/home/miglui/Desktop/SO/teste/2/teste.txt: 48 palavras  
/home/miglui/Desktop/SO/teste/teste.txt: 29 palavras

The source is not printing the entire file path. Where the error may be?

  • pq vc does not ``path=`pwd $4```?

  • @Lucas-Virgili Why you have to show the file name at the end of the path.

2 answers

1

When you call the following line of the script:

path=$(readlink -f $4)

You’re using the $4 argument. Although you did not put a call example for this script, I believe the $4 argument is the name of the file (example: test.txt). Only the file name will not serve. It has to be the file path found by find.

In case, you have to indicate the line:

path=$(readlink -f $i)

Since $i is the iteration variable that contains the full path of the file, it is the one you use correctly to count the amount of words with the toilet.

0

Tries

x=$"$(find -type f)"
for i in $x
    do echo -n "$i: "
    wc -w $i|cut -d " " -f1 -z
    echo -n " palavras"
    echo
done

Browser other questions tagged

You are not signed in. Login or sign up in order to post.