find . -perm -u+x,g+x,o+x -type f -exec grep -i -l -H -E '#!/bin/bash|#!/bin/sh' {} \; 2>/dev/null 1>/$user_scripts

I am tasked with finding any script that could exist for N users in 4 folders. I will be using ssh and executing the command above to generate a list of files for later use. Some scripts with the file extension .sh and .bash which do not contain #!/bin/bash|#!/bin/sh are not found.

I need to improve upon this but I am struggling to find something that works in the Solaris server which I have no admin rights for (No special software / libraries). If possible I would prefer not to need to execute another script that would needlessly appear in each list for each user.

Basically I need some kind of "If(find or grep)" in the same line. Is this possible?

  • 1
    Use the -o (logical or) argument to find, perhaps? Thus, after the ; ending -exec, you'd say -o -name \*.bash -o -name \*.sh. – Tom Hunt Oct 7 '15 at 17:48

You can do this by OR-ing different parts of your find command. Consider splitting your selection criteria into three parts: necessary requirements for all files (permissions, etc), and then EITHER named files OR those with an appropriate header.

This can be expressed with find like this (split onto multiple lines here for readability only):

find . -perm -u+x,g+x,o+x -type f \(
    -name '*.sh' -o
    -name '*.bash' -o
    -exec grep -il -E '^#!/bin/bash|^#!/bin/sh' {} \;
\) 2>/dev/null >/$user_scripts
  • This works well however there are underlying complications with my command. I cannot use -E in my version of Solaris and I need to constrain the search to find only scripts the current user can execute. Nevertheless this answer pointed me in the right direction, thanks! @roaima – JaredTS486 Oct 14 '15 at 19:57
  • @JaredTS486 Use egrep instead of grep -E – roaima Oct 14 '15 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.