Sponsored Content
Top Forums Shell Programming and Scripting .sh file syntax checking script Post 302152510 by fabulous2 on Thursday 20th of December 2007 04:00:35 AM
Old 12-20-2007
Here is the latest revision of the script for anyone interested:

Code:
#!/bin/sh
set -e	# exit on first failed command
set -u	# exit if encounter never set variable


#--------------------------------------------------
# Programmer notes:
#
# See this forum for a discussion of this file: https://www.unix.com/shell-programming-scripting/46785-sh-file-syntax-checking.html
#
# Possible future enhancements:
#	--make this script file POSIX compatible (currently it is NOT due to the special find command options used below)
#	--there are many more options from the find command (e.g. how symbolic links are handled--currently they are not followed)
#	which might want to expose as command line arguments of this script.
#	On the other hand, offering these options will make it even harder to be POSIX compatible...
#--------------------------------------------------


#----------environment variables


# Initialize option variables to preclude inheriting values from the environment:
opt_h="false"	# default is do not print help
opt_p="./"	# default is the current working directory
opt_R="-maxdepth 1"	# default limits the search to just path itself (i.e. do not drill down into subdirectories)


#----------functions


printHelp() {
	echo "Usage: sh checkSyntax.sh [-h] [-p path] [-R]"
	echo
	echo "DESCRIPTION"
	echo "Checks the syntax of bourne (or compatible) shell script files." \
		" The target may be either a single shell script file," \
		" or every shell script file in a directory (this is the default behavior, with the current directory the target)," \
		" or every shell script file in an entire directory tree." \
		" A shell script file is considered to be any file with the extension .sh REGARDLESS OF ITS CONTENTS." \
		" If the user has used this extension for other file types, then the syntax check may fail." \
		" The syntax checking will be done by the shell that is executing this file (e.g. bash on a typical Linux system)," \
		" so all the .sh files in the search path must be syntax compatible with this shell." \
		" A good description of differences between bash and bourne shells, for instance is found here: http://www.faqs.org/docs/bashman/bashref_122.html."
	echo
	echo "OPTIONS"
	echo "-h prints help and then exits; no value should be specified; all other options are ignored"
	echo
	echo "-p if supplied, then requires a value that is either the path to a single .sh file or the path to a directory;" \
		" if omitted, then the current working directory will be searched"
	echo
	echo "-R if present and if the path to be searched is a directory, then searches subdirectories too; no value should be specified"
	echo
	echo "DEPENDENCIES"
	echo "This script assumes that a suitable version of the find command will be the first one found in the user's PATH." \
		" This find command must support the -maxdepth, -type, and -iname options." \
		" GNU find works, but other variants may not."
	echo
	echo "+++ BUGS"
	echo "--the code is broken if any element in a path being searched contains leading whitespace"
}


#----------main


# Parse command-line options:
while getopts 'hp:R' option
do
	case "$option" in
	"h")
		opt_h="true"
		;;
	"p")
		opt_p="$OPTARG"
		;;
	"R")
		opt_R=""
		;;
	?)
		# Note: if supply an invalid option, getopts should have already printed an error line by this point, so no need to duplicate it
		echo
		printHelp
		exit 1
		;;
	esac
done


# Print help and then exit if -h is a command-line option:
if [ $opt_h = "true" ]; then
	printHelp
	exit
fi


# Find all the .sh files and check them:
#for shFile in `find $opt_p $opt_R -type f -iname "*.sh"`	# to understand this line, execute "man find"; was inspired by this script: http://www.debianhelp.org/node/1167
#	Dropped the above line because it fails on path elements which contain whitespace; the solution below is discussed here: https://www.unix.com/shell-programming-scripting/27487-how-read-filenames-space-between.html
find $opt_p $opt_R -type f -iname "*.sh" | while read shFile
do
	( sh -n "$shFile" )	# sh -n will merely syntax check (never execute) shFile, and will print to stdout any errors found; encase in parentheses to execute in a subshell so that if a syntax error is found, which causes sh -n to have a non-zero exit code, then this parent shell does not see it; critical since it will stop executing (due to the set -e line at the top of the file) otherwise
done

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script syntax checking

Is it possible to check the script syntax with some sort of command...? Without running the script . I'm using Sun Solaris (3 Replies)
Discussion started by: bjornrud
3 Replies

2. Shell Programming and Scripting

Simple file checking script

Hi, I have a really, what I hope is, simple question. I'm looking for a simple way to see whether a file exists or not and then perform an action based on whether it exists or not. An example of what I tried is as follows: if then { echo "File mysql exists" ... (1 Reply)
Discussion started by: _Spare_Ribs_
1 Replies

3. Shell Programming and Scripting

Script for checking and reporting file sizes in a directory.

Hi, Need help for a Script for checking and reporting database file sizes in a directory. Request you to please give your valuable inputs. Thanks a lot in advance. Best Regards, Marconi (1 Reply)
Discussion started by: marconi
1 Replies

4. UNIX for Dummies Questions & Answers

Checking file sizes in script

Hi, I'm trying to check a filesize within a script and then excute a relevant action. An example is below: if then rm $filename rm $filename2 elif then rm $filename2 fi Basically if $filename2 has a filesize of 0 then I want both files to be removed, but... (6 Replies)
Discussion started by: chris01010
6 Replies

5. Shell Programming and Scripting

Checking ksh script syntax

To check a bash script syntax without executing it we use: bash -n scriptname What should be the equivalent command for checking a ksh script? (8 Replies)
Discussion started by: proactiveaditya
8 Replies

6. Shell Programming and Scripting

Script check for file, alert if not there, and continue checking until file arrives

All, Is there a way to keep checking for a file over and over again in the same script for an interval of time? Ie If { mail -user continue checking until file arrives file arrives tasks exit I don't want the script to run each time and email the user each time a file... (4 Replies)
Discussion started by: markdjones82
4 Replies

7. Shell Programming and Scripting

Help with script checking for a file in various servers

I am trying to write a script that checks whether or not, a file exists on multiple servers. My code / logic so far is: #!/usr/bin/ksh print "Enter File name to be checked" read MYFILE ssh server1 " cd /var/opt/logs ; if then ... (4 Replies)
Discussion started by: momin
4 Replies

8. Shell Programming and Scripting

File checking script need help

Hi, Gurus, I need a scripts to check specified file if it exists or not at certain time (say every month between 5th and 7th). if file exists do something otherwise do another thing. can anybody help this? Thanks in advance :wall: (3 Replies)
Discussion started by: ken002
3 Replies

9. Shell Programming and Scripting

Checking LB status.. stuck in script syntax of code

#!/bin/ksh #This script will check status of load balancer in AIX servers from hopbox #Steps to do as folows : #Login to server #netstat -ani | grep <IP> #check if the output contains either lo0 OR en0 #if the above condition matches, validation looks good #else, send an email with impacted... (7 Replies)
Discussion started by: vinil
7 Replies

10. Shell Programming and Scripting

Command script for checking a file existence

Hello, I have a directory where sometimes appear a certain file name - and I'd like to be notified by email when that happens... so what command or script I may use? e.g. if there's a file named "adam" in the directory named "dir1" then send a mail to "abc@abc.com".. it needs to permanently... (5 Replies)
Discussion started by: netrom
5 Replies
All times are GMT -4. The time now is 05:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy