binaries PATH check in a ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting binaries PATH check in a ksh script
# 1  
Old 05-31-2011
binaries PATH check in a ksh script

The situation is a data center with around 800 servers.
Each server has installed one of these unix-like OS: rhel, sunos, aix, hp-ux.

And we have to make scripts general enough to being able to be executed over all these servers.

Furthermore, sometimes the scripts will be executed as a normal user and sometimes they will be executed as root.

The problem is:

sometimes the path for the binaries used into the script are in one place, and sometimes they are in another place. So the variety of different places the binaries used into the script are placed is big.

So, to make the scripts "bomb-proof" we're using this strategy:

First line of each script is containing a:

Code:
PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:/var/tmp:/etc:/home/ouruser:/export/home/ouruser...

In general, we're assigning to the PATH variable the content it had ($PATH) plus the content of all the PATH variables of the user root of each type of OS plus some "usually known places for rare binaries temporary used like inqraid in /var/tmp or in /home/ouruser" on some servers.

After doing this PATH assignment, we're checking for each command used in the script if it is in the path, checking it with the
Code:
which

command. If one of the binaries used in the script is not in our "MEGAPATH" we exit with a message saying the binary X needed to executed the script is not in the PATH.

My question is:

Is this the better way to ensure the different binaries we should use in a script are in the PATH before continuing with the execution of the script? I'm open to different suggestions. Thank you!
# 2  
Old 05-31-2011
An idea

Assuming the paths are based on the system and all systems have the same shell, you could find out the system at the start, then set the PATH accordingly.
Assuming ksh93 on Sun (code is untested but you get the idea):
Code:
#!/usr/dt/bin/dtksh
## The above shell should be present on all machines. In this example,
## ksh93.

# Define constants for system-specific search PATHs
typeset -r SUNOSPATH=<sun specific path>
typeset -r RHELOSPATH=<RHEL specific path>
typeset -r AIXOSPATH=<AIX specific path>

# Define constants for sytem/root-specific search paths.
typeset -r SUNROOTPATH=<SUN root specific path>
typeset -r RHELROOTPATH=<RHEL root specific path>
typeset -r AIXROOTPATH=<AIX root specific path>

# define variables.
newpath=""
userpath=""

# Find out what system I am on.
typeset -r SYSTEM=$(uname)

# Find out what user I am running as.
typeset -r USER=$(logname)

# set newpath to the system-specific one.
case $SYSTEM in
  'SunOS') newpath=${SUNOSPATH}
           ;;
   'RHEL') newpath=${RHELOSPATH}
           ;;
    'AIX') newpath=${AIXOSPATH}
           ;;
        *) # Always expect the unexpected!
           print "$0: Unknown system [$SYSTEM] encountered"
           exit 1
           ;;
esac

# if we are root, add that path too.
if [[ $USER == "root" ]]; then
  case $SYSTEM in
    'SunOS') newpath=${newpath}:${SUNROOTPATH}
             ;;
     'RHEL') newpath=${newpath}:${RHELROOTPATH}
             ;;
      'AIX') newpath=${newpath}:${AIXROOTPATH}
             ;;
  esac
fi

# Add the system/user-specific path to the current environment's PATH
export PATH="${PATH}:${newpath}"

# test
print "System: $SYSTEM"
print "User: $USER"
print "Path: $PATH"

exit 0

Pros: Generic.
If paths change, you only have to set them in one place, at the top of the script.

Caveats: if the command you need to run exists in multiple locations, make sure you are running the right one. This would be controlled by the order in which you build the PATH.

I got a little carried away, but it was an interesting lunchtime distraction. :-)

Gary
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh script to check if certain process is running

I have to kill the process "test" for a maintenance I do but want the script to check when it comes back up. I can get what I want when I run this while loop: while true;do ps -ef | grep test | grep -v grep | sed -e 's/^*//';sleep 60;done but I want the script to do it for me and as soon as... (6 Replies)
Discussion started by: seekryts15
6 Replies

2. Shell Programming and Scripting

Script in KSH to check if a process its up or down.

Hi all! Im working on a simple script in KSH (just started) to check if a process its up or down, but im kind of lost with the following error. Script: #!/usr/bin/ksh psup=$(ps -ef | grep sftp | egrep -v grep | wc -l) if ($psup > 0); then echo "Process SFTP running" else ... (6 Replies)
Discussion started by: ARSport
6 Replies

3. UNIX for Advanced & Expert Users

Shell script to check the SAN dual path.

Hello, I would like to create a shell script which would check whether the hosts have SAN dual path or not. If yes, then I should get a success message, else should get a list of hosts on which SAN dual path is not working. Thanks.. (4 Replies)
Discussion started by: mahive
4 Replies

4. Shell Programming and Scripting

Please help I want script to check filename, size and date in specify path.

Please help, I want script to check filename, size and date in specify path. I want output as: DATE: YYYYMMDD HH:MM ------------------------------------------------ fileA,filesize,yyyy mm dd HH:MM fileA,filesize,yyyy mm dd HH:MM fileA,filesize,yyyy mm dd HH:MM fileA,filesize,yyyy mm dd... (1 Reply)
Discussion started by: akeji
1 Replies

5. Shell Programming and Scripting

Script to compare binaries

Hi All, I am very new to scripting and wanted to write a script for comparison of folder including binary files like ear's and jars. Following is the job for which I need it: 1. Compare two folders recursively 2. Compare the binaries like ear's but exclude files like MANIFEST files 3.... (1 Reply)
Discussion started by: maverick.topgun
1 Replies

6. Shell Programming and Scripting

How do I write a ksh script that will check if all ftp files are received?

I am trying to code a ksh script that will check to see if all 26 incoming ftp files have been received before proceeding to the next function, which is to rename each file. Here is the pseudo-code of what I am trying to do: <<STEP_1>> IF all ALS files have been transmitted then... (2 Replies)
Discussion started by: doug145
2 Replies

7. UNIX for Dummies Questions & Answers

How to check path exist or not in UNIX shell script

UNIX Shell Script I'm in /home/suneel dirctory in that directory need to check for a path for example com/src/resources If Path exists need to copy the files from one directory If path not exist need to create the folders and copy the files UNIX shell script help required (3 Replies)
Discussion started by: suneelc
3 Replies

8. UNIX for Advanced & Expert Users

how to check the actual path instead of link path

Hi I have a path link /test/df/link1/actual/file1 here link1 is actually a softlink link1= a/b i need to print the ACTUAL FULL path instead of a linked path is there any direct command to print the actual path of any linked path eg showPhyscialPath /test/df/link1/actual/file1 and it... (4 Replies)
Discussion started by: reldb
4 Replies

9. Shell Programming and Scripting

Full path of executing script in ksh?

Hello all, Here's the scenario: I've got a script, let's call it script1. This script invokes another script, which we'll call set_env, via the dot "." command, like so: File: #!/bin/ksh # region_id=DEV . set_env ${region_id} and so on. Script set_env sets up an... (2 Replies)
Discussion started by: BriceBu
2 Replies

10. Shell Programming and Scripting

Determining absolute PATH within KSH script

Hey everyone, I'd like to determine the absolute path of where a script resides from within the same script. For example, I have a script - /tmp/myscript. Regardless of how that script is call - '/tmp/myscript', 'cd /tmp;./myscript', '. /tmp/myscript' - I'd like to be able to determine - from... (5 Replies)
Discussion started by: gsatch
5 Replies
Login or Register to Ask a Question