Skip to content
Advertisement

Running a script in terminal using Linux

Im trying to run this script in the terminal but its not working and says permission denied. scriptEmail is filename.

% find . -type d -exec ./scriptEmail {} ;

scriptEmail is written as follows:

# !/bin/bash
# Mail Script
find gang-l -type f -name "*" -exec sh -c ' file = "$0" java RemoveHeaders "$file" > processed/$file ' {} ';'

My read write permission

-rwxr-xr-x

Advertisement

Answer

As for permissions:

  • Check that your shebang is at the very top of your file, and that it starts exactly with #!; # ! will not work.
  • Check that your files are given execute permissions; chmod 750 scriptEmail will do.
  • Check that your file uses UNIX newlines — with DOS newlines, your shebang may have a hidden character making it point to an interpreter which doesn’t actually exist.
  • Check that the directory your file is stored in is in a directory where executable scripts are allowed (not mounted with the noexec flag, or in a SELinux context disallowing execution).

If your mount point is noexec or your ability to create executable scripts is blocked by SELinux or similar, then use find . -type d -exec bash ./scriptEmail {} ; to explicitly specify an interpreter rather than attempting to execute your script.


Second: Since you’re executing your script with find already — and using that to recurse through directories — you don’t need a second find inside (which would have you potentially operating on processed/dirA/dirB/file as well as processed/dirB/file and processed/file — with errors for all of these where the directory doesn’t exist).

#!/bin/sh
cd "$1" || exit             # if we can't cd to directory given in argument, exit.
mkdir -p processed || exit  # if we can't create our output directory, exit.
for f in *; do              # ...iterate through all directory contents...
  [ -f "$f" ] || continue   # ...if they aren't files, skip them...
  java RemoveHeaders "$f" >processed/"$f" # run the processing for one item
done
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement