| 1 | #!/bin/bash |
| 2 | # script-detector.sh: Detects scripts within a directory. |
| 3 | |
| 4 | TESTCHARS=2 # Test first 2 characters. |
| 5 | SHABANG='#!' # Scripts begin with a "sha-bang." |
| 6 | |
| 7 | for file in * # Traverse all the files in current directory. |
| 8 | do |
| 9 | if [[ `head -c$TESTCHARS "$file"` = "$SHABANG" ]] |
| 10 | # head -c2 #! |
| 11 | # The '-c' option to "head" outputs a specified |
| 12 | #+ number of characters, rather than lines (the default). |
| 13 | then |
| 14 | echo "File \"$file\" is a script." |
| 15 | else |
| 16 | echo "File \"$file\" is *not* a script." |
| 17 | fi |
| 18 | done |
| 19 | |
| 20 | exit 0 |
| 21 | |
| 22 | # Exercises: |
| 23 | # --------- |
| 24 | # 1) Modify this script to take as an optional argument |
| 25 | #+ the directory to scan for scripts |
| 26 | #+ (rather than just the current working directory). |
| 27 | # |
| 28 | # 2) As it stands, this script gives "false positives" for |
| 29 | #+ Perl, awk, and other scripting language scripts. |
| 30 | # Correct this. |