Locating all the scripts which start with:#!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Locating all the scripts which start with:#!
# 1  
Old 04-19-2015
Locating all the scripts which start with:#!

Hi,
I need to find all the executable shell scripts under /home dirctory and its sub directories.
I would like to print the path to the files , which include in the header of the file: #!
(Actually its the first line of the file).

I have tried :

Code:
find . -name "*" -type f  -exec sh -c '
    case "$(head -n 1 "$1")" in
      #!*)exit0;;
    esac
exit 1
' sh {} \; -print

Its return nothing.
Please advise
Thanks

Last edited by Don Cragun; 04-19-2015 at 06:14 PM.. Reason: Add CODE tags.
# 2  
Old 04-19-2015
Quick and dirty longhand for the home directory only as a starter using OSX 10.7.5, default bash terminal:-
Code:
#!/bin/bash
> /tmp/listing
> /tmp/exe_scripts.txt
ls -l ~/*.sh > /tmp/listing
while read line
do
	if [ "${line:3:1}" = "x" ]
	then
		echo "$line" >> /tmp/exe_scripts.txt
	fi
done < /tmp/listing
cat /tmp/exe_scripts.txt

Results:-
Code:
Last login: Sun Apr 19 22:29:11 on ttys000
AMIGA:barrywalker~> cd ~/Desktop/Code/ShellAMIGA:barrywalker~/Desktop/Code/Shell> chmod 755 exescript.sh
AMIGA:barrywalker~/Desktop/Code/Shell> ./exescript.sh
-rwxr-xr-x  1 barrywalker  staff  128656 22 Nov 09:36 /Users/barrywalker/03080.sh
-rwxr-xr-x  1 barrywalker  staff  128990  6 Dec 22:18 /Users/barrywalker/03083.sh
-rwxr-xr-x  1 barrywalker  staff  131697  7 Dec 19:48 /Users/barrywalker/03090.sh
-rwxr-xr-x  1 barrywalker  staff  134772 29 Dec 21:05 /Users/barrywalker/03100.sh
-rwxr-xr-x  1 barrywalker  staff  135072 30 Jan 20:30 /Users/barrywalker/03120.sh
-rwxr-xr-x  1 barrywalker  staff  140746  3 Apr 11:32 /Users/barrywalker/03140.sh
-rwxr-xr-x  1 barrywalker  staff  143511 11 Apr 20:37 /Users/barrywalker/AudioScope.sh
AMIGA:barrywalker~/Desktop/Code/Shell> _

# 3  
Old 04-19-2015
Try:
Code:
find . -type f -exec grep -n '^#!' {} + | awk -F: '$2==1{print $1}'

# 4  
Old 04-19-2015
This does a recursive look, but does NOT display the directories, OS etc as before:-
Code:
#!/bin/bash
> /tmp/listing
> /tmp/exe_scripts.txt
cd ~
ls -lR > /tmp/listing
while read line
do
	if [ "${line:3:1}" = "x" ] && [ "${line:$((${#line}-3)):3}" = ".sh" ]
	then
		echo "$line" >> /tmp/exe_scripts.txt
	fi
done < /tmp/listing
cat /tmp/exe_scripts.txt

Results:-
Code:
Last login: Sun Apr 19 22:43:41 on ttys000
AMIGA:barrywalker~> cd ~/Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./exescript.sh
ls: com.solidstatenetworks.host.savedState: Permission denied
-rwxr-xr-x   1 barrywalker  staff  128656 22 Nov 09:36 03080.sh
-rwxr-xr-x   1 barrywalker  staff  128990  6 Dec 22:18 03083.sh
-rwxr-xr-x   1 barrywalker  staff  131697  7 Dec 19:48 03090.sh
-rwxr-xr-x   1 barrywalker  staff  134772 29 Dec 21:05 03100.sh
-rwxr-xr-x   1 barrywalker  staff  135072 30 Jan 20:30 03120.sh
-rwxr-xr-x   1 barrywalker  staff  140746  3 Apr 11:32 03140.sh
-rwxr-xr-x   1 barrywalker  staff  143511 11 Apr 20:37 AudioScope.sh
{SNIP}
-rwxr--r--   1 barrywalker  staff   63938 26 Apr  2014 02250.sh
-rwxr--r--   1 barrywalker  staff   93359 29 Jun  2014 02500.sh
-rwxr--r--   1 barrywalker  staff  106544 29 Jun  2014 02550.sh
-rwxr--r--   1 barrywalker  staff  108239 29 Jun  2014 03000.sh
-rwxr--r--@  1 barrywalker  staff  108463  9 Nov 10:19 03010.sh
-rwxr--r--@  1 barrywalker  staff  110523  9 Nov 10:20 03020.sh
-rwxr-xr-x   1 barrywalker  staff  115347  9 Nov 18:46 03040.sh
-rwxr--r--   1 barrywalker  staff  128656 22 Nov 09:36 03080.sh
-rwxr--r--   1 barrywalker  staff  131697  7 Dec 19:48 03090.sh
-rwxr--r--   1 barrywalker  staff  134772 29 Dec 21:05 03100.sh
-rwxr--r--   1 barrywalker  staff  135072 30 Jan 20:30 03120.sh
-rwxr--r--@  1 barrywalker  staff  135072 30 Jan 20:14 AudioScope.sh
-rwxr--r--  1 barrywalker  staff   63938 26 Apr  2014 AudioScope.sh
-rwxr--r--  1 barrywalker  staff   52320  5 Jan  2014 AudioScope_02-01-2014.sh
-rwxr--r--  1 barrywalker  staff    4333 26 Mar  2014 Function_Generator.sh
AMIGA:barrywalker~/Desktop/Code/Shell> _

# 5  
Old 04-19-2015
Perhaps this might be a little quicker, and certainly supports files with spaces in their name:

Code:
find . -type f -print0 | xargs -0 awk '/^#!/&&FNR==1{print FILENAME}{next}'

Or if you have GNU awk use nextfile:

Code:
find . -type f -print0 | xargs -0 gawk '/^#!/&&FNR==1{print FILENAME}{nextfile}'

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 04-20-2015
Quote:
Originally Posted by Chubler_XL
Perhaps this might be a little quicker, and certainly supports files with spaces in their name:

Code:
find . -type f -print0 | xargs -0 awk '/^#!/&&FNR==1{print FILENAME}{next}'

Or if you have GNU awk use nextfile:

Code:
find . -type f -print0 | xargs -0 gawk '/^#!/&&FNR==1{print FILENAME}{next file}'

You're correct in noting that grep and awk aren't both needed; the pattern matching I was doing with grep can just as well be done inside awk.

Note that my earlier suggestion didn't have any problem with spaces (or tabs) in filenames, but it wouldn't work with filenames containing one or more colon characters.

Note also that we haven't been told what OS the submitter is using (and the -0 option in xargs and the -print0 primary in find are not available on all systems (and are extensions not included in the POSIX standards).

The awk nextfile function is also an extension to the POSIX standards, but isn't just available in gawk. The following should be portable to any Linux or UNIX system:
Code:
find . -type f -exec awk 'FNR == 1 && /^#!/{print FILENAME}' +

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk. If awk on your system has a nextfile function, the following will run considerably faster:
Code:
find . -type f -exec awk 'FNR == 1{if($0 ~ /^#!/) print FILENAME; nextfile}' +

# 7  
Old 04-20-2015
Hmm, trust me not to read the OP's post fully. Apologies.

But I am curious though, much like my attempt assumes a '.sh' extension what about a shell script that does not have a shebang as the first line?

How do you cater for such circumstances?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

[HA] Red Hat 7, pacemaker and start/stop scripts

Hi there, I am wondering if I could add start/stop ksh scripts provided by 3rd party to cluster... I read that script must be ocf/lsb compliant, however, in AIX I can just set up two separate scripts for starting and stopping application. Can similar be done under RH Linux cluster? Cheers, c (1 Reply)
Discussion started by: cyjan
1 Replies

2. Red Hat

Need Kick Start Post and Preinstallation Scripts

Hi All Rhel Admin Need a Small Help please Give me a Preinstallation Script and postinstallation script for Kickstart Preinstallation script Just Need to Partition 1 TB HDD using LVM Except Boot /boot = 500 swap = 16 GB / = 850 Gb 8e And need to format it in ext4 ... (1 Reply)
Discussion started by: babinlonston
1 Replies

3. Shell Programming and Scripting

Start scripts if it doesn't run on other node

Hello community, I created a script to simply query DB and then analize data. The environment where the script will works is two RedHat machines that access both to an external database. My script runs from the first crontab node. But what about if the first node goes down? What I need is copy... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

4. UNIX for Advanced & Expert Users

Start multiple scripts from the same session

hello, i have an AIX 6.1 server which has Informix 11.5 Database Engine. When i want to export some databases from the instance for backup or for any other use i can do it with dbexport (informix command). The problem is that when i run this command or when i run script which run this command,... (2 Replies)
Discussion started by: omonoiatis9
2 Replies

5. Shell Programming and Scripting

Shell performance problem with locating scripts

I am currently trying out MKS Toolkit C Shell, and I've no problems with it until I try add directories to PATH that are located on a network drive. When I do that, the shell performance slows down significantly and no longer runs fast. In fact, it takes seconds for something that should take... (1 Reply)
Discussion started by: vas28r13
1 Replies

6. Shell Programming and Scripting

Oracle DB Start shutdown scripts

Hi, We have a requirement wherein we do not want to share the Oracle DB sys and system passwords to be shared with the support desk. But they will be responsible for starting/shuting down the Database. Is it possible to write a shell script which will read the sys and system passwords from a... (0 Replies)
Discussion started by: narayanv
0 Replies

7. Shell Programming and Scripting

Start all scripts in a directory

Good morning. I have a tricky one here for me. Hope somebody can help. I am looking for a way to take a single script in a directory and use it to fire all scripts within a subdirectory. For example. Lets say I have the following in /lcl/prd/apps file1.sh file2.sh file3.sh file4.sh... (2 Replies)
Discussion started by: LRoberts
2 Replies

8. Shell Programming and Scripting

Stopping Start/Stop scripts in reverse order

#Define the Start/Stop/Status Scripts to include SSS_SCRIPTS=( prog1 prog2 prog3 etc...... ) #Start the scripts StartScripts() { for SSS in ${SSS_SCRIPTS} do ./$SSS start done } #Stop the Scripts StopScripts() { for SSS in ${SSS_SCRIPTS} do ./$SSS stop #<---I... (1 Reply)
Discussion started by: madasafish
1 Replies

9. Shell Programming and Scripting

Start and stop of an application thru shell scripts.

Hi, I just learnt the shell scripting and got working on that right now. I have one problem. Here i am having a java application that needs to be start and stop using two shell scripts, i.e., starting the java application using one shell script and stopping the application using another... (1 Reply)
Discussion started by: sadha
1 Replies
Login or Register to Ask a Question