Sponsored Content
Top Forums Shell Programming and Scripting Portable Shell Script - Determine Which Version of Binary is Installed? Post 302710789 by colinjohnson on Friday 5th of October 2012 01:00:49 AM
Old 10-05-2012
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 date requires "date -d "+ 14 days" -u +%Y-%m-%d".

I desire to make my script portable - so I believe that I'll need to utilize a test to determine which version of the date application is installed and thus, what command to run. I've come up with the following two methods, but am looking for advice:

Use "File" tool:
Code:
date_binary_full_path=`which date`
date_binary=`file -b $date_binary_full_path`
if [[ $date_binary ~= "Mach-O 64-bit executable x86_64" ]]
    then date_command="date -v+14d -u +%Y-%m-%d"
elif  [[ $date_binary == "ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped" ]]
	date_command="date -d \"+ 14 days\" -u +%Y-%m-%d"
else
	date_command="date -d \"+ 14 days\" -u +%Y-%m-%d"
fi
echo $date_command

Use "date" --version switches:
Code:
if [[ `date --version 2>&1 | grep -c "date (GNU coreutils)"` > 1 ]]
	then date_command="date -d \"+ 14 days\" -u +%Y-%m-%d"
else
	$="date -v+14d -u +%Y-%m-%d"
fi
echo $date_command

 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Determine Unix Version

Is there a trick to determine the exact version from Unix that one is working with? I would have expected to see it after logon, but all I get are some Copyright-messages... I know it's some HP-UX, but I would like to know the version-number Tnx in advance! Dave (2 Replies)
Discussion started by: davegeysemans
2 Replies

2. UNIX for Dummies Questions & Answers

Cmd to determine my OS version?

I'm looking for a generic (i.e. would be resident in most/all unix flavors) internal command for determining my OS and version. When I telnet to a box here @ work, I get none of that info and can't remember the equivalent of the DOS ver command; I know I used to know this! Thanks. (2 Replies)
Discussion started by: brian.wilson
2 Replies

3. Shell Programming and Scripting

ftp - determine ascii or binary file

Hello, How to i determine via ftp commandline if files on ftp server is ascii or binary files. Like every other comon windows ftp program does it automatically. regards Thomas (5 Replies)
Discussion started by: congo
5 Replies

4. UNIX for Dummies Questions & Answers

Tru64 - how can you determine the package where a file or binary belongs to?

Dear linux-unix users, I hope that one of you will be able to help me. How can I determine to which package a file or binary belongs to. With solaris/linux package managers you can easely determine this but i cant find the way how to do this on tru64. Any help would greatly be appreciated.... (0 Replies)
Discussion started by: Crazy_lenny
0 Replies

5. Linux

A portable bootable version?

What I have been doing for some time now is installing linux on my tech machine at work, plugging in devices and transferring data with dd_rescue. What I need now is a version of linux that I can install on a laptop sata hard drive and be able to plug it into any machine to transfer data off of... (1 Reply)
Discussion started by: Methal
1 Replies

6. Solaris

How to determine, whether Sun WorkShop is installed on Solaris worksation?

How to determine, whether Sun WorkShop is installed on Solaris worksation? And if It's installed, how to start one? (2 Replies)
Discussion started by: wolfgang
2 Replies

7. Solaris

T5220 How to determine physical memory installed?

Hi, looking to upgrade memory on a pair of T5220's from 32GB to 64GB. Cannot determine current DIMM size and slots used. i.e. not sure if Qty 16 x 2GB or Qty 8 x 4GB. If there are no empty slots, i need to go with higher density DIMMs and retire exsisting the 2GB prtdiag follows. #... (3 Replies)
Discussion started by: edrew
3 Replies

8. 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

9. UNIX for Beginners Questions & Answers

Bash find version of an installed application but if none is found set variable to App Not Installed

Hello Forum, I'm issuing a one line bash command to look for the version of an installed application and saving the result to a variable like so: APP=application --version But if the application is not installed I want to return to my variable that the Application is not installed. So I'm... (2 Replies)
Discussion started by: greavette
2 Replies
ATF-SH(1)						    BSD General Commands Manual 						 ATF-SH(1)

NAME
atf-sh [-s shell] -- interpreter for shell-based test programs SYNOPSIS
atf-sh script DESCRIPTION
atf-sh is an interpreter that runs the test program given in script after loading the atf-sh(3) library. atf-sh is not a real interpreter though: it is just a wrapper around the system-wide shell defined by ATF_SHELL. atf-sh executes the inter- preter, loads the atf-sh(3) library and then runs the script. You must consider atf-sh to be a POSIX shell by default and thus should not use any non-standard extensions. The following options are available: -s shell Specifies the shell to use instead of the value provided by ATF_SHELL. ENVIRONMENT
ATF_LIBEXECDIR Overrides the builtin directory where atf-sh is located. Should not be overridden other than for testing purposes. ATF_PKGDATADIR Overrides the builtin directory where libatf-sh.subr is located. Should not be overridden other than for testing purposes. ATF_SHELL Path to the system shell to be used in the generated scripts. Scripts must not rely on this variable being set to select a specific interpreter. EXAMPLES
Scripts using atf-sh(3) should start with: #! /usr/bin/env atf-sh Alternatively, if you want to explicitly choose a shell interpreter, you cannot rely on env(1) to find atf-sh. Instead, you have to hardcode the path to atf-sh in the script and then use the -s option afterwards as a single parameter: #! /path/to/bin/atf-sh -s/bin/bash ENVIRONMENT
ATF_SHELL Path to the system shell to be used in the generated scripts. SEE ALSO
atf-sh(3) BSD
September 27, 2014 BSD
All times are GMT -4. The time now is 09:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy