0

I get how to read one file by using [-r] But how do I make a script that takes a multiple input of files and checks?

Lets say I type

./checkfile hi hello world

the script should return :

hi is readable 
hello is readable
world is not readable 
summary: 2 of 3 files are readable
rcs
  • 649
  • 1
  • 6
  • 11
JVAN
  • 65
  • 2
  • 6

2 Answers2

1
#! /bin/sh -
n=0
for file do
  if [ -r "$file" ]; then
    printf '"%s" is readable\n' "$file"
    n=$((n + 1))
  else
    printf '"%s" is not readable\n' "$file"
  fi
done
echo "$n out of $# files were readable"

[ -r file ] test whether the file is readable by the process invoking that [ command, so by you, the user running that script, typically using the access() system call.

It doesn't say anything about whether other users may be able to read it. It doesn't attempt to read it either. For instance, it won't be able to detect files that are un-readable because the underlying storage is defective.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • your `shebang` doesn't seem right – NarūnasK Mar 15 '17 at 17:26
  • @NarūnasK. That's the correct she-bang if you want it to be interpreted by /bin/sh. If one wanted to be pedantic, `#! /bin/sh` would be incorrect. – Stéphane Chazelas Mar 15 '17 at 19:16
  • @NarūnasK, see also [Why the "-" in the "#! /bin/sh -" she-bang?](//unix.stackexchange.com/q/351729) – Stéphane Chazelas Mar 15 '17 at 21:41
  • In fact my eyes caught a *space* between `#!` and `/bin/sh -` which after further [reading](http://unix.stackexchange.com/a/276845/111103) appeared to be optional and having space in `shebang` definitely is not an error, hence apologies for confusion. – NarūnasK Mar 16 '17 at 09:49
0

$@ is a special variable that stores all arguments (positional parameters) passed to the script in and array-like struct.

$1, $2, $3, ... are the positional parameters.
"$@" is an array-like construct of all positional parameters, {$1, $2, $3 ...}.

More about this in the bash reference manual

Bruno9779
  • 1,353
  • 6
  • 19