0
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
  • 1
    Use the -o (logical or) argument to find, perhaps? Thus, after the ; ending -exec, you'd say -o -name \*.bash -o -name \*.sh. Commented Oct 7, 2015 at 17:48

1 Answer 1

1

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 
2
  • 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 Commented Oct 14, 2015 at 19:57
  • @JaredTS486 Use egrep instead of grep -E Commented Oct 14, 2015 at 20:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.