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?
# 1  
Old 10-12-2016
Power How to pre-check scrutinize all my shell scripts?

I have a few shell scripts.

I need to run them on Flavors of Unix and Linux OS.

The problem occurs that a set of commands in the script fail on one Operating System or the other.

Below are a list of few commands that did not work on all operating systems i tested specifically becoz they are either located at different paths as they are not set in the user profile that i login with or those commands do not even exist on the System i run the script.

Code:
find
openssl
nawk
grep

I wish to test my shell scripts on any Operating System and figure out what command or arguments to the commands are failing.

This can be done basically by grepping for "command not found" or " invalid arguments" strings in the output of the shell script run.

This is my requirement. Can you tell me if it actually able to find all the failing commands for "command not found" or " invalid arguments without executing the script.

Something like a dummy runs that executes the script only for me to collect the output without actually executing the commands on the OS.

Sort of a pseudo run rather than actual run of the scripts.

Let me know if & how is it possible.

Will be a great help. Thank you.

Last edited by mohtashims; 10-12-2016 at 12:18 PM..
# 2  
Old 10-12-2016
If you can't even find find, you've got a horrible login profile, or are running from cron.
# 3  
Old 10-12-2016
Power

Quote:
Originally Posted by Corona688
If you can't even find find, you've got a horrible login profile, or are running from cron.
I m not dealing with crontab.

find command works and i can find find but the argument passed with find may not work on some systems. So i have to give the path to the feasible find among the many find command installed on that system.

By the way, like i asked in the OP is there a way to pseudo run my script rather than actual run ? or any trick up your sleeve ?

Last edited by mohtashims; 10-12-2016 at 02:03 PM..
# 4  
Old 10-12-2016
Your explanation of find makes that especially difficult: find just existing isn't good enough, you actually need to know that it's a specific version. That's not the sort of thing you can discover without running it to see if it works.

I don't know what to suggest besides thorough error-checking. You could have variables like

AWK="mawk" and run them like $AWK -F: '{ code }' ... to make it easier to adapt your script to different systems.

You could also rewrite certain things more portably to avoid needing system-specific commands, i.e. using plain awk instead of GNU awk, etc.
# 5  
Old 10-12-2016
You can check paths by hand, I suppose.

Code:
OLDIFS="$IFS" ; IFS=":"

for CMD in openssl find nawk grep
do
        F=""
        for P in $PATH # Depends on IFS splitting, do not quote
        do
                [ -e "$P/$X" ] && F=1
                [ -z "$F" ] || break
        done

        if [ -z "$F" ]
        then
                echo "Required command $X not found in path" >&2
                exit 1
        fi
done

IFS="$OLDIFS"

Again, this does not check versions of commands, just their existence.

A "dry run" as you put it would only check syntax errors. Only running them will detect whether they're actually doing what you expect them to do.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 10-12-2016
You can create symlinks in a directory in the path pointing to the right command version.
You need to restrict your script to the minimal subset of commands/options common to ALL systems it runs on.
# 7  
Old 10-12-2016
Most systems offer a way to set a basic (and basically working, if the system is set up sensibly) environment: in AIX (and other UNIXes) this is /etc/environment, in Linux this is /etc/profile, etc..

I have built on that and created a standard environment file for all of my scripts which gives me a constant environment by being sourced in in all my scripts. It is called f_env and looks like this (excerpt):



Code:
unset ENV                                   # clear the environment
#---------------------------------------------------- set basic environment

typeset -x OS=$(uname -a | cut -d' ' -f1)   # find out the OS
                                            # read in standard environment
case "$OS" in
     AIX)
          . /etc/environment
          ;;

     Linux)
          . /etc/profile
          ;;

     *)
          . /etc/environment
          ;;
esac
                                            # set default TERM variable
case "$OS" in
     AIX)
          TERMDEF="$(termdef)"
          ;;

     Linux)
          TERMDEF="$TERM"
          ;;

      *)
          TERMDEF="$TERM"
          ;;

esac
typeset -x TERM=${TERMDEF:-'wyse60'}        # set default TERM variable
typeset -x LANG=C                           # default language environment
typeset -x EDITOR=vi                        # what else ? ;-))
typeset -x VISUAL=$EDITOR

typeset -x PATH="/usr/bin"                 # set the path
           PATH="$PATH:/bin"
           PATH="$PATH:/etc"
           PATH="$PATH:/usr/sbin"
           PATH="$PATH:/usr/ucb"
           PATH="$PATH:/sbin"
           PATH="$PATH:/usr/bin/X11"
           PATH="$PATH:/usr/local/bin"      # tools, home for scripts
           PATH="$PATH:/usr/local/sbin"     # -"-

if [ -z "$DEVELOP" ] ; then
     typeset -x FPATH="/usr/local/lib/ksh"  # set fnc path for fnc lib
     FPATH="$FPATH:/usr/local/bin"
     FPATH="$FPATH:/usr/local/sbin"
else
     typeset -x FPATH=~/lib        # for lib development
fi

[....]

Most of my scripts start this way:

Code:
#! /bin/ksh
# --------------------------------------------- main()
if [ "$DEVELOP" != "" ] ; then
     . /usr/local/lib/ksh/f_env
else
     . ~/lib/f_env
fi
[....]

Notice that the variable DEVELOP is examined both in the script and the environment-file. The reason is i have this standard-environment together with a lot of shell-functions packaged into a "library" and always on the same place on every system (/usr/local/lib/ksh). Still i want to be able to test with newly written (or changed) library functions without messing with the public part. For this i have a variable "DEVELOP" set in my profile on my development system. When this variable is set FPATH is set to ~/lib and all the external shell functions are loaded from there instead.

This way i can test with my private copy of the library until i am ready to release the next version.

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
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