我们有两台服务器,一台是aix,另一台是linux,因此在我们的linux服务器上,我试图调用位于aix上的脚本,以减少脚本执行时间。代码:在linux机器上
ssh user@$ip "sudo -u user2 'bash -c . /path (loading user profile) ; sh script.sh' "在Aix机器上:
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脚本在aix机器上运行良好,但是我在linux机器上出现了一个错误。
cat: cannot open state log and normal log : permission denied( even after giving
Full permission to file)-+-新changes++++++
根据您在评论部分的建议,我做了一些修改。
现在,在linux机器上,下面是我用来在远程服务器上调用脚本的代码。
ssh user@$ip 'sudo -u user2 bash script.sh' 在Aix机器上:
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 脚本现在在aix机器上不能很好地工作,有时它在if循环中获取第一个命令的值,有时在第二个命令中获取值。我不明白密码出了什么问题。提前感谢您的帮助!
+++++++New变更2 +++++++
我已经完成了aix脚本中的以下更改,并且它对我是有效的。
#/bin/bash ./path
db2连接到db_name
db2列表表空间\ grep -i状态\ wc -l > full_path_of_file1
db2列表表空间\ grep -i状态\ wc -l > full_path_of_file2
var1=$(cat full_path_of_file1)
var2=$(cat full_path_of_file2)
如果[]那么回波“表空间是正常的”“否则回波”表空间不是正常的“fi”
AIX的行为有点奇怪,但它可以工作。
发布于 2022-10-16 07:29:59
替换
ssh user@$ip "sudo -u user2 'bash -c . /path (loading user profile) ; sh script.sh' "使用
ssh user@$ip sudo -u user2 bash script.sh您不需要在脚本之外调用./path,因为脚本首先调用它本身。在这一点上,这条线可以大大简化。
如果这不能解决权限错误,请确保您位于脚本的正确目录中
ssh user@$ip sudo -Hu user2 bash script.sh更好的防御性编码是为state_log和normal_log文件使用脚本中的目录路径,显式地设置写入它们的必要目录。
如果您已经显示了完整的脚本,那么您甚至可以通过不向文件写入而是将db2输出直接捕获到两个变量来进一步优化它:
#/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虽然我不完全确定为什么$var和$var2在这种情况下会有所不同。
https://unix.stackexchange.com/questions/721143
复制相似问题