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.
1 Answer 1
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.
-
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– Geek2022年10月16日 15:12:21 +00:00Commented 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– Geek2022年10月17日 19:17:21 +00:00Commented 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 youChris Davies– Chris Davies2022年10月17日 19:28:43 +00:00Commented 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– Geek2022年10月18日 16:49:09 +00:00Commented Oct 18, 2022 at 16:49
-
Where I can get this option, i am newbie to stack exchange.😀Geek– Geek2022年10月18日 17:09:49 +00:00Commented Oct 18, 2022 at 17:09
bash script.sh db_name
? The defaultsh
shell might be pointing to an older shell that cannot understand bash syntax.sh script.sh
(the arguments are not relevant). That executes the script using thesh
shell, but you have written your script using thebash
shell. Most modern Linux distros havesh
to be the same asbash
, but I believe AIX comes with a very old version ofksh
as the default shell. So, specifyingbash script.sh
in your SSH command line could resolve the issue.cat
at the end seems to refer to a file called (literally)state log and normal log
.cat
in the script.