0

We have two servers, one is aix and other is linux, so from our linux server, i am trying to invoked script that is located on aix to minimise script execution time. code: On linux machine

ssh user@$ip "sudo -u user2 'bash -c .  /path (loading user profile) ; sh script.sh' "

On Aix machine:

script.sh

#/bin/bash
. /path [loading user profile]

db2 connect to db_name

db2 list tablespaces | grep -i state | wc -l  > state_log

db2 list tablespaces | grep -i state | wc -l  > normal_log



var1=$(cat state_log)

var2=$(cat normal_log)

if [[ "${var1}" == "${var2}" ]]
then
echo " tablespaces are normal "
else
echo "tablespaces are not normal"
fi

script is working fine on aix machine, however I am getting a error on linux machine

cat: cannot open state log and normal log : permission denied( even after giving
Full permission to file)

----++++New changes++++++

As per your advise in comment section, i have done some changes.

Now on linux machine below code i am using for invoking script on remote server.

ssh user@$ip 'sudo -u user2 bash script.sh' 

On Aix machine:

script.sh:

#/bin/bash 
. /path [user profile]
 if [[ `db2 connect to db_name |  db2 list tablespaces | grep -i state | wc -l` == `db2 connect to db_name` | db2 list tablespaces | grep -i state | wc -l` ]]
then 
echo " tablespaces are normal " 
else 
echo "tablespaces are not normal" 
fi 

script is now, not working fine on aix machine, Sometime it fetches value of first command in if loop and sometime second. I am not getting what's wrong with the code. Thanks in advance for all of your help !!!

+++++++New changes 2 +++++++

I have done following changes in aix script and it works for me.

#/bin/bash . /path [loading user profile]

db2 connect to db_name

db2 list tablespaces | grep -i state | wc -l > full_path_of_file1

db2 list tablespaces | grep -i state | wc -l > full_path_of_file2

var1=$(cat full_path_of_file1)

var2=$(cat full_path_of_file2)

if [[ "${var1}" == "${var2}" ]] then echo " tablespaces are normal " else echo "tablespaces are not normal" fi

Kind of weird behaviour of AIX OS but it works.

8
  • 1
    Can you try adjusting your SSH command to bash script.sh db_name? The default sh shell might be pointing to an older shell that cannot understand bash syntax.
    – Haxiel
    Commented Oct 16, 2022 at 4:17
  • @Haxiel that was command line argument bash script.sh db_name... If you see script.sh there is a command db2 connect to $1. Now I have changed it for more clear understanding. Any help from anyone would be appreciate
    – Geek
    Commented Oct 16, 2022 at 4:52
  • 1
    I was trying to clarify a different point. In your SSH command, you are executing the script using sh script.sh (the arguments are not relevant). That executes the script using the sh shell, but you have written your script using the bash shell. Most modern Linux distros have sh to be the same as bash, but I believe AIX comes with a very old version of ksh as the default shell. So, specifying bash script.sh in your SSH command line could resolve the issue.
    – Haxiel
    Commented Oct 16, 2022 at 5:01
  • It would be better if you did not anntotate code and error messages with inline comments. The error message from cat at the end seems to refer to a file called (literally) state log and normal log.
    – Kusalananda
    Commented Oct 16, 2022 at 8:21
  • @Haxiel The bash-specific code comes after any use of cat in the script.
    – Kusalananda
    Commented Oct 16, 2022 at 8:22

1 Answer 1

0

Replace

ssh user@$ip "sudo -u user2 'bash -c .  /path (loading user profile) ; sh script.sh' "

with

ssh user@$ip sudo -u user2 bash script.sh

You don't need to invoke ./path outside the script because the first the the script does is invoke it itself. At that point the line can be vastly simplified.

If that doesn't resolve the permissions error, ensure that you're in the correct directory for your script

ssh user@$ip sudo -Hu user2 bash script.sh

Better defensive coding would be to use directory paths in your script for the files state_log and normal_log, explicitly setting the necessary directory in which to write them.

If you have shown your complete script, you can potentially further optimise it by not even writing to files but instead capturing the db2 outputs directly into the two variables:

#/bin/bash
. /path    # Load user profile

db2 connect to db_name
var1=$(db2 list tablespaces | grep -i state | wc -l)
var2=$(db2 list tablespaces | grep -i state | wc -l)

if [[ $var1 -eq $var2 ]]
then
    echo "tablespaces are normal"
else
    echo "tablespaces are not normal"
fi

Although I'm not entirely sure why $var and $var2 would ever be different in this situation.

7
  • As per your advise, i have done some changes ssh user@$ip 'sudo -u user2 bash script.sh' On Aix machine: script.sh #/bin/bash . /path [user profile] if [[ db2 connect to db_name | db2 list tablespaces | grep -i state | wc -l == db2 list tablespaces | grep -i state | wc -l ] then echo " tablespaces are normal " else echo "tablespaces are not normal" fi script is now not working fine on aix machine, Sometym it fetches value of first command in if loop and sometym second. I am not getting what's wrong with the code. Thanks for your help.
    – Geek
    Commented Oct 16, 2022 at 15:12
  • I have tried the way, you have explained, however there was still it was not able fetch value in to variable. I have tried other way around and it works.
    – Geek
    Commented Oct 17, 2022 at 19:17
  • @Geek I'm pleased to hear that something works. You forgot to explain what went wrong when you tried to use my suggested code (errors, etc), and "the other way" isn't specific enough to identify what worked for you Commented Oct 17, 2022 at 19:28
  • Hi roima Sorry for late reply, i have added new changes which I have made in script which works for me. Again appreciated buddy for your help. Now I have another new problem, i will post a new question, as this thread kind of jumbled😅
    – Geek
    Commented Oct 18, 2022 at 16:49
  • Where I can get this option, i am newbie to stack exchange.😀
    – Geek
    Commented Oct 18, 2022 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .