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?
# 15  
Old 10-13-2016
I dont know... When I write a script that runs on different OS which may not have the same version of software, path syntax etc... I test the OS and accordingly set the environment for my programs with the needed params... and it always works...
# 16  
Old 10-17-2016
Hi.

There have been discussions about static checkers for shell scripts. However, because of the flexibility of shell, there are times when the script really needs to be executed. For example:
Code:
ls=~/bin/my-ls
$ls /

Unless one processes the assignment, there is no way to tell if the command $ls will work, or even if it exists.

However, as an experiment and proof-of-concept, I wrote a perl script that, with many kludges, can process a number of the Bourne-shell-family statements to isolate commands that are not available. The perl is about 150 lines long.

An example shell script for input might be:
Code:
#!/usr/bin/env bash

# @(#) Example-2        Demonstrate sample statements for diagnosis.

LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

FILE=${1-data1}

p=$( basename $0 ) t1="$Revision: 1.14 $" v=${t1//[!0-9.]/}
[[ $# -gt 0 ]] && [[ "$1" =~ -version ]] &&  { echo "$p (local) $v" ; exit 0 ; }

[[ $# -le 0 ]] || echo1 Hi

cat $FILE | cat1 | cat2 ; cat3

v1="a|b\
|c|\
d"

./scramble1

~/scramble2

$HOME/scramble3

x=`cmd1`

y=$( cmd2 )

if [ stuff ] ; then other stuff; fi

if [ stuff0 ]
then
  other stuff
fi

if [[ modern stuff ]] ; then other stuff; fi

while [ repetitive exp ] ; do stuff ; done

while [[ more exps ]] ; do stuff ; done

if grep1 ; then stuff1 ; done

while gawk2 ; do stuff2 ; done

case $item in:
        a) a=1 ;;
        (b) case1 ;;
esac

db " End of script."

exit 0

and running the perl code across it yields:
Code:
$ ./p1 example-2
/home/drl/scramble3: not found
./scramble1: not found
basename is /usr/bin/basename
case1: not found
cat is /bin/cat
cat1: not found
cat2: not found
cat3: not found
cmd1: not found
cmd2: not found
db is a function defined in this script.
echo1: not found
exit is a special shell builtin
export is a special shell builtin
other: not found
pe is a function defined in this script.
printf is a shell builtin
stuff1: not found
stuff2: not found
/home/drl/scramble2: not found

For the adventurous, there is a far-more-complete, complex, shell parser found at: Shell::Parser - search.cpan.org

This is not a complete scanner of scripts. For example, note that single-line if/while are not processed, but some shifting around of the perl would allow that. Constructs like until, select, etc. are not processed; case within case is not recognized.

The basic idea is to use 2 passes. In pass 1 the code is broken apart to allow ease of recognition in pass 2 by writing to a scratch file, then in pass 2 the scratch file is processed, resulting in the warnings, and notes.

I have a long script that adapts itself to different platforms. This perl code correctly identified the sw_vers OSX command as not being found on Linux, as well as SuSE command zypper not being found on Debian.

Good luck ... cheers, drl
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