Shell script to check file executable permission availability

SCRIPT:
#!/bin/bash
USAGE="usage : $0 {filename}"
CHECK_FILE=$1
if [ $# -lt 1 ]
then
 echo "$USAGE"
 exit 1
fi
if [ -x "$CHECK_FILE" ]
then
 echo "$CHECK_FILE has the executable permission"
else
 echo "$CHECK_FILE not has executable permission"
fi
OUTPUT-1:
test.sh has the executable permission

OUTPUT-2:

sujin_sr not has executable permission



Advanced script for same requirement:

#!/bin/bash
USAGE="usage : $0 {filename}"
CHECK_FILE=$1
#check for argument
if [[ $# -le 1 && ! -f $1 ]]
then
 echo "ERROR !!!"
 echo "$USAGE!"
 echo "Argument should be valid file!"
 exit 1
fi
#check file or not
if [ -x "$CHECK_FILE" ]
then
 echo "$CHECK_FILE has the executable permission"
else
 echo "$CHECK_FILE not has executable permission"
 echo "Do you want to give executable permission to $CHECK_FILE file"
 echo -n "{yes/no}  :  "
 read select
 if [[ $select = 'yes' ]]
 then
  chmod +x $CHECK_FILE
  echo "Executable permission given to the $CHECK_FILE file"
 fi
fi
Output-1:
test.sh not has executable permission
Do you want to give executable permission to test.sh file
{yes/no}  :  yes
Executable permission given to the test.sh file

Output-2:

bash test.sh test.sh 
test.sh has the executable permission


No comments:

Post a Comment