How can I write a shell script file_readable which:
-
accepts some number of names as arguments,
-
checks each name to see if it is a regular file and readable, and
-
outputs a count of the number of such files.
For example:
$ sh file_readable /etc/fstab /etc/ssh/ssh_host_rsa_key /etc/does-not-exist
1
Of these, only /etc/fstab is likely to both exist and be readable.
So far I have put this together but it does not work correctly – can anybody help me please ?:
#!/bin/sh
for filename in "$@"
do
if test -f "$filename"
then echo | wc -l
else echo $?
fi
done
If file exists and is a regular you print number of lines in empty string plus “\n”, which is equal
1always. Sound not quite usable, isn’t it?All you need is incrementing some counter and printing it in the end.