-1

My script just runs producing no output. If I enter no argument it does the error checking correctly but if I enter an appropriate file for the argument it just runs with no output and no errors. The goal of the script is to check if a file is executable or not then ask the user if they want to change the permissions if they are not set.

Here is the code

 #!/bin/bash

#making sure and argument is entered

if [ $# -ne 1 ]
then
  echo "Invalid arguments. Must pass 1 argument"
  echo "Usage: $0 <filename>"
  exit 1
fi

#filename argument variable
fname=$1

#storing file to verify its a executable
is_executable=`file $fname`

#getting permissions
lsOut=`ls -l $fname|awk '{print $1}'`

ownerPerm=`echo ${lsOut:1:3}`
groupPerm=`echo ${lsOut:4:3}`
otherPerm=`echo ${lsOut:7:3}`

# checking if the file is a executable
if [[ "$is_executable" =~ .*"executable".* ]]
then
  if [[ $ownerPerm != *"x"* ]]
  then
    choice=""
    echo "Should the execute bit be set for Owner(yes/no)? "
    read choice
    if [ $choice == "yes" ]
    then
      chmod u+x $fname
    fi
  fi

  if [[ $groupPerm != *"x"* ]]
  then
    choice=""
    echo "Should the execute bit be set for Group(yes/no)? "
    read choice
    if [ $choice == "yes" ]
    then
      chmod g+x $fname
    fi
  fi

  if [[ $otherPerm != *"x"* ]]
  then
    choice=""
    echo "Should the execute bit be set for Other(yes/no)? "
    read choice
    if [ $choice == "yes" ]
    then
      chmod o+x $fname
    fi
  fi
else
  if [[ $ownerPerm =~ .*"x".* ]]
  then
    chmod u-x $fname
    echo "Execute permission removed from owner for the file $fname"
  fi

  if [[ $groupPerm =~ .*"x".* ]]
  then
    chmod g-x $fname
    echo "Execute permission removed from group for the file $fname"
  fi
  if [[ $otherPerm =~ .*"x".* ]]
  then
    chmod o-x $fname
    echo "Execute permission removed from other for the file $fname"
  fi
fi
Walter A
  • 19,067
  • 2
  • 23
  • 43
Xibbas
  • 1
  • 1
  • 2
    It seems to work for me. Try [running it with `bash -x`](https://stackoverflow.com/questions/951336/how-to-debug-a-bash-script) and see what's happening as it executes. Also, run it past [shellcheck.net](https://www.shellcheck.net) and fix the issues it points out. I'd also recommend simplifying the tests (e.g. either `if [[ $ownerPerm = *"x"* ]]` or `if [[ $ownerPerm =~ "x" ]]` instead of `if [[ $ownerPerm =~ .*"x".* ]]`). If you can't solve it, show us a [minimal, reproducable example](https://stackoverflow.com/help/minimal-reproducible-example). – Gordon Davisson Nov 10 '19 at 20:39
  • I edited the question: some indenting. – Walter A Nov 10 '19 at 20:43
  • 1
    There is a direct way to check if a file is executable by you: `[[ -x $file ]]`. Using this would simplify your script a lot. – Benjamin W. Nov 10 '19 at 20:44
  • 2
    Use `stat` to get the permissions rather than parsing the output of `ls`. – chepner Nov 10 '19 at 20:45
  • Can you show the input? When `file` returns `executable` and the mode is drwxrwxrwx, what output do you expect? – Walter A Nov 10 '19 at 20:46
  • I figured out was was wrong. Me being an idiot incorrectly set up the file permissions for the tests I was trying to run. The bash -x command allowed me to figure it out. Thank you all who helped, I learned about some useful new tools and tips. – Xibbas Nov 10 '19 at 21:09

1 Answers1

-1

I figured out was was wrong. Me being an idiot incorrectly set up the file permissions for the tests I was trying to run. The bash -x command allowed me to figure it out.

Xibbas
  • 1
  • 1
  • 1
    For the benefit of other people with similar problems please provide the permissions code that fixed it. – agc Nov 11 '19 at 00:00