How to pre-check scrutinize all my shell scripts?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pre-check scrutinize all my shell scripts?
# 8  
Old 10-12-2016
Power

Let me state something that i missed stating clearly.

I am not looking for a fix i.e the corrected commands and their locations. No !!

I m looking only to report the failing commands that fail due to the "command not found" or due to "illegal arguments".

I just need to report all failing commands in the script without executing the script on the server.

That's my requirement.

Last edited by mohtashims; 10-12-2016 at 05:34 PM..
# 9  
Old 10-12-2016
You can do that with proper error checking. It's a part of programming, the script can't do it for you.

Code:
die() {
        echo "$@" >&2
        exit 1
}

find ... || die "find did not work"

# etc

These 2 Users Gave Thanks to Corona688 For This Post:
# 10  
Old 10-13-2016
Quote:
Originally Posted by mohtashims
I am not looking for a fix i.e the corrected commands and their locations. No !!
OK, now i am a bit baffled. In my experience the best error handling is the one where you dn't have to handle anything at all because you avoid the error altogether. Only the next best is to report the error properly.

Quote:
Originally Posted by mohtashims
I m looking only to report the failing commands that fail due to the "command not found" or due to "illegal arguments".
This is quite easy: any responsible script programming includes checking the return codes of all the commands (at least the vital ones). This return code will be non-zero for:

a) - failing commands, regardless of why they fail, but there will be different error codes for the error condition
a1) - unknown argument/option
a2) - not enough rights
a3) - whatever else the error might be

b) - unknown commands

In case of b) the shell itself will return error 127, as you can easily test with a known and an unknown command:

Code:
# ls
[... some output...]
# echo $?
0
# /foo/bar/baz
/bin/ksh: /foo/bar/baz:  not found
# echo $?
127

You should be able to build an error handling function around that.

I hope this helps.

bakunin
# 11  
Old 10-13-2016
Power

So, all of you are asking me to write a separate script to report the failing commands.

Which means i will have to look for all command in 1000+ lines of unix shell script to figure out all the commands i use in there like:

Code:
ifconfig
ggrep
nawk
openssl
gzip

etc...

However, i wanted to know if it is somehow possible that my scripts are automatically check for all commands without executing the script & without me having to provide all commands in the check.

So, if any new script is introduced i should be able to validate all the command in that script without executing the script & without manually traversing line by line looking for unique commands in the scripts.

If it is not possible, then i will check for each command like some have suggested on this thread.
# 12  
Old 10-13-2016
You might try one of the script compilers. Searching 'bash compiler' gets lots of hits.
# 13  
Old 10-13-2016
Quote:
Originally Posted by mohtashims
So, all of you are asking me to write a separate script to report the failing commands.

Which means i will have to look for all command in 1000+ lines of unix shell script to figure out all the commands i use in there like:
No, but you might think a bit and find a more "computerised" way of achieving this goal. You can, for instance, write a small parser, which filters out all the shell keywords and other language constructs so that only commands are being left over, which you can then feed into a while-loop for instance to process as described below.

You can probably (but with less runtime-security) forego the parser and reduce it to a matter of sequential greps to arrive at the same list. Note, though, that this will be a more error-prone (altough more easily to achieve) alternative, because parsing adds context to the processed code which filtering doesn't (to add some theoretical background: the difference between Chomsky-Type 3 and Chomsky-Type 2 grammatics coming in here).


Quote:
Originally Posted by mohtashims
However, i wanted to know if it is somehow possible that my scripts are automatically check for all commands without executing the script & without me having to provide all commands in the check.
What exactly do you mean by "automatically"? If i remember correctly (Don Cragun may help out here) /usr/bin/dowhateverineed was not part of the POSIX standard last time i checked, so: no, you won't get that automatically. You will have to do work to achieve results like the rest of us.

Quote:
Originally Posted by mohtashims
So, if any new script is introduced i should be able to validate all the command in that script without executing the script & without manually traversing line by line looking for unique commands in the scripts.
Here is an idea: implement, what i proposed before, put that into a script, pass to this script the name of a another script you want to check, let the script go over the process of extracting commands and then, for every command found, execute a test if this command exists at the target system. This takes already care of the "command not found"-problem.

Here is another idea, which might sound a bit outlandish: STICK TO THE POSIX STANDARD! There are a lot of POSIX-certified platforms and even non-certified platforms will for most parts adhere to the standard. So, if you use only what is in this standard you will at most times be on the safe side even without complicated tests. If you want to use every obscure platform-specific speciality you are doomed to ultimately fail anyway, having tests or not. So, how about changing your working habits instead of wanting to change the world?

Frankly, i work in environments where i have to deal with up to a dozen different platforms (different OSes, different versions, etc.) and my scripts usually work even in these environments without problems (that is: problems coming from this diversity - my scripts are not flawless at all, but i don't have your problems). And 1000 lines of shell code is a typical size of one of my scripts, of which i have (and use) many dozens. My pity for you to have to manage 1000 lines of code is somewhat limited.

I hope this helps.

bakunin
# 14  
Old 10-13-2016
Quote:
Originally Posted by mohtashims
So, all of you are asking me to write a separate script to report the failing commands.
No, to write your original one properly. If commands are failing left and right but it doesn't quit, you neglected to check a lot of return values.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to check sub scripts execution?

I have a shell script which is used to get the input and have another shell script (a sub script) at the end of this shell script which is used to upload the inputs in the Oracle database. I can check the execution status of the parent script using sh -x script.sh. but this command doesn't show the... (1 Reply)
Discussion started by: srilaxman
1 Replies

2. Shell Programming and Scripting

Can somebody help me check over my shell scripts??

I been having a lot of trouble trying to start up a 3rd party application in Solaris 7 but it seams that its missing entry's when trying to run the files so maybe the start shells scripts have errors and maybe thatr is what is causing the issues I have added two links to the shells can anyone check... (5 Replies)
Discussion started by: Wpgn
5 Replies

3. Shell Programming and Scripting

Check existence of a number of files and call other scripts

Hi, I am new to unix scripting and am jus getting to learn about it.. I need to know on how to check for the existence of a number of files in a path..i.e the files are ftp'ed from several other servers, should check if all the files have arrived, if not wait till they arrive..can i use a flag... (5 Replies)
Discussion started by: yohasini
5 Replies

4. Shell Programming and Scripting

check exit status of bg scripts

HI All, I am running one shell script, in that script i am calling 4 scripts in the background. abc.ksh & efg.ksh & xky.ksh & mno.ksh & please let me know, how could i find the success and failure of each script. i Cannot use $?, because i want to run all the scripts in parellel. ... (2 Replies)
Discussion started by: javeed7
2 Replies

5. Linux

Call scripts in Rpm's: %pre %post sectino

Hi, In the rpm SPEC file there is %pre section for preinstall scripts. We can write any think in the "sh" format here. I want to call a script here. How can i do this? Thanks (2 Replies)
Discussion started by: vibhor_agarwali
2 Replies

6. Shell Programming and Scripting

1 script or multiple scripts?? - check files, run jobs

Question for anyone that might be able to help: My objective is to eheck if a file (a source file) exists in a directory. If it does then, I'd like to call an application (Informatica ETL file...not necessary to know) to run a program which extracts data and loads it into multiple targets. ... (6 Replies)
Discussion started by: jnanasakti
6 Replies

7. Shell Programming and Scripting

Check if script run by a user directly or via other scripts

Hi, i have a script 'a.sh' that should be called only by certain scripts like b.sh, c.sh Inside a.sh, how can i determine 1) if this script was run directly from command prompt (or scheduler) 2) if called via other scripts? Is there an easy way to get parent process name (not just pid),... (2 Replies)
Discussion started by: ysrinu
2 Replies

8. Shell Programming and Scripting

my scripts does not check if directory exists

Hello: Can someone please help me figure out what is wrong here, my script does not move on to the "else" part even though there is no .ssh directory on my remote server: $more putkey.sh #!/bin/ksh for server in `cat list` do if ; then cat $HOME/.ssh/id_rsa.pub |ssh $server ' cat >>... (4 Replies)
Discussion started by: Sara-sh
4 Replies

9. UNIX for Dummies Questions & Answers

How to check the unix scripts currently running

I have a Unix box abcd, where I have script1, script2 and script3 running. I have to write a 4th script script4 which would check my box(abcd) and kill all running scripts. How can I do that? (3 Replies)
Discussion started by: Sibasish
3 Replies

10. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies
Login or Register to Ask a Question