Write a shell script to test whether a given
- file is directory or not
- file has read permission or not
- file has write permission or not
- file has execute permission or not
Assume file name is given as command line argument.
Write a shell script to test whether a given
Assume file name is given as command line argument.
You need to use test or [ … ] syntax.
[ -d "$f" ] && echo "directory: yes" || echo "directory: no"
[ -f "$f" ] && echo "file: yes" || echo "file: no"
[ -r "$f" ] && echo "read permission: yes" || echo "read permission: no"
[ -w "$f" ] && echo "write permission: yes" || echo "write permission: no"
[ -x "$f" ] && echo "execute permission: yes" || echo "execute permission: no"
#!/bin/bash
f="$1"
[ "$f" == "" ] && { echo "$0 file"; exit 1; }
echo
echo "File \"$f\" has the following permission for user \"$USER\":"
echo
[ -d "$f" ] && echo "directory: yes" || echo "directory: no"
[ -f "$f" ] && echo "file: yes" || echo "file: no"
[ -r "$f" ] && echo "read permission: yes" || echo "read permission: no"
[ -w "$f" ] && echo "write permission: yes" || echo "write permission: no"
[ -x "$f" ] && echo "execute permission: yes" || echo "execute permission: no"
I prefer test
which is more explicit than [
.
yes, test and [ and [[.