Query regarding portable script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Query regarding portable script
# 1  
Old 07-26-2008
Query regarding portable script

Dear Experts,

I want to write a script which has to work on Solaris & Linux sytems.
The problem which i am facing is, there are commands whose options are different on both OS's. For example ping.

On Solaris i have to write:
ping $host 1
to check if the host is alive

On Linux i have to use following command
ping $host -c 1
One way to write generic script is
if [ `uname -s` == "SunOS" ]
then
OPT="1"
fi
if [ `uname -s` == "Linux" ]
then
OPT="-c 1"
fi

ping $host $OPT
The only problem with this approach is that for every set of options i have make OS specific check. Is there some better way of achieving same in shell scripts. In make i know sth like following works:

OPT.solaris="1"
OPT.linux="-c 1"
OPT=$(OPT.$(OSTYPE))

Is something like this possible in shell scripts also?
# 2  
Old 07-26-2008
You could use arrays opt[].

Code like that is a nightmare to maintain. Consider writing two include files, i.e., files that get sourced when your script starts, which file is sourced depends on the OS.

Each sourced file has aliases for you problem commands:
The include file for LINUX has this line in it
Code:
alias ping1="/home/me/pinglinux.sh"
# /home/me/pinglinux.sh has this in it
ping $1 -c $2

In your main script you have
Code:
ping1 $host $count   # or whatever

The alias then becomes a wrapper for a ping call.
# 3  
Old 07-26-2008
Depending on which shell you are using you could encapsulate the OS check into a function and set a global variable for the OS. In all further script parts you branch depending on that variable. The following is just a sketch:

Code:
#! /bin/ksh

function set_environment
{

case $(...some_check...) in
     solaris)
          OS="solaris"
          ;;

     linux)
          OS="linux"
          ;;

     *)
          OS="other"
          ;;

esac

}



function do_ping
{

target_host="$1"
return_value=0

case OS in
     solaris)
          ping $target_host 1
          return_value=$?
          ;;

     linux)
          ping -c 1 $target_host
          return_value=$?
          ;;

     *)
          print -u2 "ERROR: don't know how to ping on OS ${OS}"
          return_value=255
          ;;

esac

return $return_value
}

# ------------- main()

typeset -x OS=""

. set_environment

do_ping ...some_ip_address....
print - "ping returned $?"

exit 0

I hope this helps.

bakunin
# 4  
Old 07-26-2008
Hi Jim,
Thanks for the reply. Even i agree sourcing in can be good solution.
As I am not expert in scripting so request you to elaborate on how i can source a file in my main script.


-Dhiraj
# 5  
Old 07-28-2008
The . (dot) command reads in the contents of another file in the context of the current script.

Code:
. /path/to/include.sh

That's a dot, a space, and the name of the file to include.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tip: template for a safe and portable script

In an attempt to finally end this article I start this new thread. Here is a template for a safe and portable script. #!/bin/bash # /bin/bash exists on most still supported Unixes # # tr and date behave better with if ; then export LC_ALL=C; else export LANG=C; fi # # Unix optional packages... (2 Replies)
Discussion started by: MadeInGermany
2 Replies

2. Shell Programming and Scripting

Portable shell script advise

All, I have a need for a portable shell script for LInux and HPUX. The script has a simple need; Check for local files of a specific name (i.e. filename*), scp them to another system, and archive them to a folder. The script runs via cron. I first wrote the script in Linux (bash with gnu... (4 Replies)
Discussion started by: hburnswell
4 Replies

3. Shell Programming and Scripting

Argument to the script query

Hi, I have a script invest.sh which takes two inputs i.e the name of the properties file and the file permissions for eg: ./invest.sh app12.properties 664Now i wish to give a folder path /opt/app/properties instead of the properties file and the invest.sh script should lookout for all the... (3 Replies)
Discussion started by: mohtashims
3 Replies

4. Shell Programming and Scripting

Portable Shell Script - Determine Which Version of Binary is Installed?

I currently have a shell script that utilizes the "Date" binary - this application is slightly different on OS X (BSD General Commmand) and Linux systems (gnu date). In particular, the version on OS X requires the following to get a date 14 days in the future "date -v+14d -u +%Y-%m-%d" where gnu... (1 Reply)
Discussion started by: colinjohnson
1 Replies

5. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

6. Shell Programming and Scripting

Script query

Hi Admins, count=`ps -ef | grep LIST_8i | grep -v grep|wc -l` pid=`ps -ef|grep LIST_8i|grep -v grep|awk '{print $2}'` if then lsnrctl stop LIST_8i >> ${log} sleep 5 lsnrctl start LIST_8i >> ${log} else echo " " lsnrctl start LIST_8i >> ${log} fi 2>&1 The script restarts... (5 Replies)
Discussion started by: newaix
5 Replies

7. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

8. Shell Programming and Scripting

shell script query

hi all i have a shell script for connecting in sybase env what i need is i have around 10 servers , i need to connect to all servers and retrive the database inforamtion in the servers and display them any one of u have it request to share it asap ! "QUERY TO Connect to all servers... (1 Reply)
Discussion started by: mvsramarao
1 Replies

9. UNIX for Dummies Questions & Answers

Expect Script - if/else query

Hi, I am trying to write an expect script that contains an if/else statement based on a result ... but I'm having a bit of trouble. The code below shows my script - it all works fine but when I include the second expect function it ignores it and moves on ... can anyone help with this ...... (1 Reply)
Discussion started by: spikey
1 Replies

10. UNIX for Dummies Questions & Answers

Shell script query

Hi, I am stuck assigning a value to a variable. I am trying to assign a value to a variable but getting error.... IP_ADDR=grep 'I.P. Address' /install/cfgdist/`uname -n`.cfg | cut -d : -f 2| cut -d . -f 1-3| sed s/" "//g I am using this script to grep first three octets of an IP address... (4 Replies)
Discussion started by: max29583
4 Replies
Login or Register to Ask a Question