Sponsored Content
Top Forums Shell Programming and Scripting .sh file syntax checking script Post 302152334 by reborg on Wednesday 19th of December 2007 03:49:10 PM
Old 12-19-2007
Quote:
Originally Posted by fabulous2
Personally, I think that it should always be an error to use an uninitialized variable.
You are entitled to thinks so. I disagree but that is a question of opinion, by all means do that. I just said it was unnecessary in this case.

Quote:
If I had written sh, it would not even be an option.
Sorry I didn't follow you here.

Quote:
First, why would I ever want to use the full path to a command? That makes it totally unportable! The whole purpose of something like the PATH env var is to get around issues like this.
The purpose of PATH is to tell the shell in which directories and in which order to look for commands. A properly written script would not normally depend on login environment to know that. There are some exceptions, but I really don't see a reason for having one here. The script will currently break if the user has a different command (or different non-pompatible version of a command) with the same name as one of the ones you use in their PATH before the one you want; or has a command aliased to behave in a way you don't expect which would be worse. If you really want to use PATH, include it in the script, don't depend on the user to set it.

Quote:
Second, you did not complete your thought: what is the or clause?
I did, but you didn't receive it.

What I said was, what is preferable if you don't want to use full paths directly use a variable, which I always do by the way, and it is easier to port also.

for eample:
Code:
FIND=/usr/bin/find
...
$FIND $opt_p $opt_R -type f

Also doing it in that way if the command you are using has a verbose option so for example 'tar' you could do:
Code:
TAR="/usr/bin/tar v"

and turn on verbose output for all instances of the command in the script while debugging.

Then to port it is a simple matter of updating the variable if required, or adding a clause for each OS you expect to run on for any non-shared paths.

Quote:
It does not support any of the functionality that I really want this function to have, like recursive finding in a directory tree and case insensitivity.
I'm guessing you mean non-recursive, since it recurses by default:
Code:
find <dir> \( ! -name <dir> -prune \)

Case insenitivity you can do with grep -i or more simply :

Code:
-name "*.[sS][hH]"

Quote:
When I did a "man find" just now on a linux box, it did note that many of the non-POSIX options that I am using are not unique to GNU's find but are implemented on other systems too, somaybe it is mostly portable...
Maybe...Solaris and HPUX for example do not.

Quote:
Rant mode on: there is nothing more annoying than the lazy unix programmer habit of responding to an input error with the correct usage.
It has little or nothing to do with laziness. It is more effort for the programmer to write the message, and saves the user a lot of time if they forget the order of the arguments, there are a lot of characters in a man page and some people use slow connections. Most users I have dealt with much prefer to run a commmand with no arguments or -h and get a usage message. Maybe the -h options solves it from either POV. I am not saying don't print the message that says what is wrong, just display the generic form as a reminder too or tell the user how to do so, this kind of thing:
Code:
# check.sh -B
Illegal option -B
    Try `check.sh -h' for more information


Quote:
Do people routinely write korn shell, c shell, etc script files and use the same .sh file extension for all of them, instead of using something sensible like .ksh, .csh, etc?
Yes.

Quote:
I do not even know if other shells support the -n syntax check option, so I would just as soon ignore all script files except those purporting to be bourne shell scripts.
bourne derived shells such as ksh and bash do.

Quote:
Would it be a better idea to abandon searching for *.sh files, and instead look at every normal file and read its first line and only assume that it is a bourne shell script if the first line starts with "#!/bin/sh"?
You could if you wanted to. Something like this inserted in the find would do it:
Code:
find ..... -exec awk ' FNR==1 && /\/sh$/ { print FILENAME; quit}' \; | .....

 

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 07:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy