Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:34:11+00:00 2026-06-14T17:34:11+00:00

I have a script which has a few functions inside it which the main

  • 0

I have a script which has a few functions inside it which the main body uses to execute. Now, I want to run this script on 3 remote unix machines. Which is the neatest way to do this ?
Most importantly, I don’t to write a second script for remote connection. Everything should be inside this one script.

I’ve tried heredoc with ssh, which is not working because of the big functions !

Code –

#!/bin/bash

# Year Month Day Related functions
# FUNCTIONS
# Find no. of days in a year
yeardays()
{
# argument check
if [ X$1 = X ]
then
        read year
else
        year=$1
fi
# Check for leap years
if [ `expr $year % 400` = 0 ]
then
        echo 366
        exit
fi

if [ `expr $year % 100` = 0 ]
then
        echo 365
        exit
fi

if [ `expr $year % 4` = 0 ]
then
        echo 366
        exit
fi

echo 365
}

# Find no. of days in a Month
monthdays()
{
# argument check
if  [ X$1 = X ]
then
     read ymd   # year in yyyymmdd format
elif [ X$2 = X ]
then
      ymd=$1
else
      ymd=`expr \( $1 \* 10000 \) + \( $2 \* 100 \) + 1`
fi

year=`expr $ymd / 10000` ;
month=`expr \( $ymd % 10000 \) / 100` ;

case $month in
      1|3|5|7|8|10|12) echo 31 ; exit ;;
      4|6|9|11) echo 30 ; exit ;;
      *) ;;
esac

# except for month 2, which depends on whether the year is a leap year
# Use yeardays to get the number of days in the year and return a value
# accordingly.
daysInYear=`yeardays $year`

case $daysInYear in
   365) echo 28 ; exit ;;
   366) echo 29 ; exit ;;
esac
}

ymd2yd()    # convert from YYYYMMDD(gregorian) to YYYYDDD(julian)
{
# argument check
if [ X$1 = X ]
then
        read date
else
        date=$1
fi

year=`expr $date / 10000`
month=`expr \( $date % 10000 \) / 100`
days=`expr $date % 100`

count=1
while [ `expr $count \< $month` = 1 ]
do
        daysInMonth=`monthdays $year $count`
        days=`expr $days + $daysInMonth`
        count=`expr $count + 1`
done

julian=`expr \( $year \* 1000 \) + $days`
echo $julian
}

yd2ymd()    # convert from YYYYDDD(julian) to YYYYMMDD(gregorian)
{
# argument check
if [ X$1 = X ]
then
        read date
else
        date=$1
fi

year=`expr $date / 1000`
days=`expr $date % 1000`

month=1
while [ `expr $days \> 0` = 1 ]
do
        daysInMonth=`monthdays $year $month`
        days=`expr $days - $daysInMonth`
        month=`expr $month + 1`
done

days=`expr $days \+ $daysInMonth`
month=`expr $month \- 1`

gregorian=`expr \( $year \* 10000 \) + \( $month \* 100 \) + $days`
echo $gregorian
}

ydadd()     # Add/Subtract days to YYYYDDD format
{
# argument check
if [ X$2 = X ]
then
        difference=$1
        read yd     # Read the YYYYDDD format date
else
        yd=$1
        difference=$2
fi

days=`expr $yd % 1000`
year=`expr $yd / 1000`

days=`expr $days + $difference`
daysInYear=`yeardays $year`

while [ `expr $days \> $daysInYear` = 1 ]
do
        days=`expr $days - $daysInYear`
        year=`expr $year + 1`
        daysInYear=`yeardays $year`
done

while [ `expr $days \< 1` = 1 ]
do
        year=`expr $year - 1`
        daysInYear=`yeardays $year`
        days=`expr $days + $daysInYear`
done

yd=`expr \( $year \* 1000 \) + $days`       # Final date in YYYYDDD format
echo $yd
}

ymdadd()    # Add/Subtract days to YYYYMMDD format
{
if [ X$2 = X ]
then
        difference=$1
        read ymd    # Read YYYYMMDD format date
else
        ymd=$1
        difference=$2
fi

echo $ymd | ymd2yd | ydadd $difference | yd2ymd # Convert YYYYMMDD to YYYYDDD, perform date arithmetic, then revert to YYYYMMDD format
}

daysLeft()  # Calculate days between two dates in YYYYMMDD format
{
# argument check
if [ X$1 = X ]
then
        read ymd1   # First date in YYYYMMDD format
        read ymd2   # Second date in YYYYMMDD format
elif [ X$2 = X ]
then
        ymd1=$1
        read ymd2
else
        ymd1=$1
        ymd2=$2
fi

year1=`expr $ymd1 / 10000`
month1=`expr \( $ymd1 % 10000 \) / 100`
day1=`expr $ymd1 % 100`

year2=`expr $ymd2 / 10000`
month2=`expr \( $ymd2 % 10000 \) / 100`
day2=`expr $ymd2 % 100`

daysm1=`monthdays $year1 $month1`
days=`expr $daysm1 - $day1`
month1=`expr $month1 + 1`

while [ `expr $month1 \<= 12` = 1 ]
do
        daysm1=`monthdays $year1 $month1`
        days=`expr $days + $daysm1`
        month1=`expr $month1 + 1`
done

x=1
while [ `expr $x \< $month2` = 1 ]
do
        daysm2=`monthdays $year2 $x`
        days=`expr $days + $daysm2`
        x=`expr $x + 1`
done

days=`expr $days + $day2`
echo $days
}

# MAIN BODY
# Connect to different Servers
declare -a SERVER=('server1' 'server2' 'server3')
remoteUser=abc
serverNumbers=${#SERVER[@]}
count=0
while [ `expr $count \< $serverNumbers` = 1 ]
do
    # Connect to  server
    ssh -T -q $remoteUser@${SERVER[count]} <<-"END_TEXT"
    VALUE=`cat /home/cognos/cognos/c8/configuration/cogstartup.xml | grep -i xsd:long | head -1 | cut -d">" -f2 | sed 's/[:/<|crn:value]*//g'`
    VALUE_BACKUP=$VALUE
    let 'VALUE -= 30'       
    let 'VALUE *= 86400'
    RESULT1=`perl -e '@stats = stat("/home/cognos/cognos/c8/configuration/signkeypair"); print ((time - $stats[9]) < "$VALUE");'`
    RESULT2=`perl -e '@stats = stat("/home/cognos/cognos/c8/configuration/encryptkeypair"); print ((time - $stats[9]) < "$VALUE");'`
    RESULT3=`perl -e '@stats = stat("/home/cognos/cognos/c8/configuration/caSerial"); print ((time - $stats[9]) < "$VALUE");'`
    while [ "$RESULT1" -o "$RESULT2" -o "$RESULT3" ]
    do
            echo "Sending mail."
            # Calculate days left
            CURRENT_DATE=`date +"%Y-%m-%d" | sed 's/-//g'`
            CREATION_DATE=`ls -logE /home/cognos/cognos/c8/configuration/ | grep -i signkeypair | awk '{print $4}' | sed 's/-//g'`
            EXPIRY_DATE=`ymdadd $CREATION_DATE $VALUE_BACKUP`
            DAYS_LEFT=`daysLeft $CURRENT_DATE $EXPIRY_DATE`
            # Identify environment from hostname - DEV/UAT/PRD
            LOCALHOST=`hostname`
            ENVIRONMENT_TYPE=`echo $LOCALHOST | perl -ne '~m/.*([a-zA-Z]{3})[0-9]*$/; print $1;'|tr '[a-z]' '[A-Z]'`
            # Identify process - FormPF/GoReporting
            if (echo $LOCALHOST | grep -i cfp >/dev/null) then
                   PROCESS="FormPF"
            else
                   PROCESS="GoReporting"
            fi
            # Add details to mail body
            echo "The key was created on `ls -log /home/cognos/cognos/c8/configuration/ | grep -i signkeypair | awk '{print $4,$5,$6}'`, and was set to expire after $VALUE_BACKUP days." > mail.txt
            echo "Key expires in $DAYS_LEFT days !!" >> mail.txt
            echo " " >> mail.txt
            echo "The server is-" >> mail.txt
            hostname >> mail.txt
            echo " " >> mail.txt
            echo "Status of folders/files-" >> mail.txt
            echo " " >> mail.txt
            ls -log /home/cognos/cognos/c8/configuration/ | grep -i signkeypair | awk '{print $4,$5,$6,$7}' >> mail.txt
            ls -log /home/cognos/cognos/c8/configuration/ | grep -i encryptkeypair | awk '{print $4,$5,$6,$7}' >> mail.txt
            ls -log /home/cognos/cognos/c8/configuration/ | grep -i caSerial | awk '{print $4,$5,$6,$7}' >> mail.txt
        echo " " >> mail.txt
        echo "To reactive key, please restart the server at weekend with below options:" >> mail.txt
        echo " " >> mail.txt
        echo "/home/cognos/etc/restartAllServers.sh -cdsk" >> mail.txt
            # Send warning mail !!
            SUBJECT="!! ($ENVIRONMENT_TYPE) $PROCESS ($LOCALHOST) Cognos CSK(Common Symmetric Key) Expiry !!"
            EMAIL="xyz@abc.com"
            cat mail.txt | mailx -s "$SUBJECT" "$EMAIL"
        rm mail.txt
            break
    done
    logout
    END_TEXT
    count=`expr $count + 1`
done
exit 0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:34:12+00:00Added an answer on June 14, 2026 at 5:34 pm

    Move the functions inside the heredoc. It may require some changes to the code structure but it will work.

    ssh app01.datacentre.private <<EOD
    hostdatefunc () {
    echo \$(hostname) \$(date)
    }
    hostdatefunc
    EOD
    

    You’ll get the hostname and date on the remote server, not yours. Works. Stupid example, but proof of concept.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP-script originally developed on Ubuntu, which now has to run on
Right now I have a table with a few items, each of which has
I have a js script which declares a namespace, then has a method called
I have a global js file which has an ajax script. which is included
I have just found a script we are using which has a sub that
I have a python framework which has to execute bash scripts as plugins. We
I have a script which I can run perfectly if I call it manually
I have a script which similar to this: foo.php class Foo { function Foo()
I have a long SQL Script which I run to pre-populate some temp tables.
Updated below: I have a script which has a function I made that calculates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.