Question: Script will receive any number of file names as arguments. Script should check whether every argument supplied is a file or directory. If directory report. If file, then name of the file plus number of lines present in it should be reported.
Below is my code,
#!/bin/sh
for i in $*; do
if [ -f $i ] ; then
c=`wc -l $i`
echo $i is a file and has $c line\(s\).
elif [ -d $i ] ; then
echo $i is a directory.
fi
done
Output:
shree@ubuntu:~/unixstuff/shells$ ./s317i file1 file2 s317h s317idir
file1 is a file and has 1 file1 line(s).
file2 is a file and has 2 file2 line(s).
s317h is a file and has 14 s317h line(s).
My question: Variable c's value are 1 file1, 2 file2, 14 s317h on every iteration. Whereas I'd want it to 1,2 and 14. Why does it contain the former values and not the latter one? Where am I wrong?
Note: s317i is my file name and file1 file2 s317h and s317idir are the command line arguments.
Kindly advice.