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:#!
# 8  
Old 04-20-2015
Quote:
Originally Posted by wisecracker
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?
You don't.

Is the following file (scf) a shell script?:
Code:
$ ls -l scf
-------r--  1 dwc  staff  35 Apr 20 01:27 scf
$ cat scf
supercalifragilisticexpialidocious

It can be for someone whose user name is not dwc, does not have a primary group name or supplementary group name staff, and has an executable file named supercalifragilisticexpialidocious somewhere in their search path.

Last edited by Don Cragun; 04-20-2015 at 05:54 AM..
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 04-20-2015
Although not the more efficient, your first attempt was pretty close. The main issue is your case line was ignored interpreted as a comment, starting with a "#".
I also removed '-name "*"' which is useless being always true.

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

Answering to your last question, I would start with the "file" command. There is no simple heuristic to sort out shell scripts from other text files so most non shebanged scripts will show up as "ASCII text" with this command:

Code:
find . -type f -exec file {} + | grep text

# 10  
Old 04-20-2015
There might be versions of find where -name "*" is everything but .*.
If this is intended then \! -name ".*" would be more clear.
# 11  
Old 04-20-2015
*deleted by author*

Last edited by wbport; 04-20-2015 at 05:49 PM.. Reason: Didn't read OP question carefully enough
# 12  
Old 04-20-2015
Quote:
Originally Posted by MadeInGermany
There might be versions of find where -name "*" is everything but .*.
If this is intended then \! -name ".*" would be more clear.
According to the standards, -name "*" should match every path being evaluated. But, note that the entries . and .. are not evaluated unless they were named as operands on the command line.
# 13  
Old 04-20-2015
Quote:
Originally Posted by wisecracker
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?
You really can't.

All a user needs to do is source a file anyway. In bash:

Code:
bash 4.1$ . TextFile

That'll run through commands in TextFile just as if it were executable and started with "#!/bin/bash".

Not sure what the point of finding scripts is anyway, since all they do is the same thing the user can do with a command line.

And what about files .bashrc or .profile?
# 14  
Old 04-21-2015
Quote:
Originally Posted by wisecracker
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?
Check that the file is a text file that is executable?
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