gpt4 book ai didi

linux - 如何在子shell中设置变量?

转载 作者:太空宇宙 更新时间:2023-11-04 10:54:23 25 4
gpt4 key购买 nike

我有一个脚本,它将在每台服务器上运行,并将某些文件复制到其中。脚本知道我在哪里运行以及我需要复制哪些文件。

脚本将从本地数据中心 local_dc 复制文件,但如果它已关闭或没有响应,那么它将从远程数据中心 remote_dc_1 复制相同的文件,如果它也已关闭, 然后它将从另一个远程数据中心 remote_dc_2 复制相同的文件,如下所示。

现在假设如果 local_dc 机器宕机,那么它将从 remote_dc_1 机器复制文件,所以我需要发送一封电子邮件说这台机器宕机了,所以从其他远程机器。如果 local_dc 机器出现故障,我不想发送多封电子邮件,所以我在最后发送电子邮件,这样我只收到一封电子邮件,而不是每个文件收到多封电子邮件。

下面是我的脚本-

do_Copy() {
el=$1
PRIMSEC=$2
scp david@"$local_dc":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/. \
|| (a=1 && scp david@"$remote_dc_1":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/.) \
|| (b=2 && scp david@"$remote_dc_2":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/.) \
|| (c=3 && exit 1)
}

export -f do_Copy

parallel --retries 10 -j 10 do_Copy {} $PRIMARY ::: "${PRIMARY_PARTITION[@]}" &
parallel --retries 10 -j 10 do_Copy {} $SECONDARY ::: "${SECONDARY_PARTITION[@]}" &
wait

# problem is this doesn't work at all?
if [ $a -eq 1 ]
then
echo "Local machine $local_dc was down, so copied from Primary Remote machine $remote_dc_1" | mailx -r "david@host.com" -s "$local_dc machine down" "david@host.com"
fi
if [ $b -eq 2 ]
then
echo "Primary Remote machine $remote_dc_1 was down, so copied from Secondary Remote machine $remote_dc_2" | mailx -r "david@host.com" -s "$remote_dc_1 machine down" "david@host.com"
fi
if [ $c -eq 3 ]
then
echo "All three machine's are down. Exiting out." | mailx -r "david@host.com" -s "All three machine's are down" "david@host.com"
fi

我在子 shell 中使用变量 a、b 和 c 做错了吗?

最佳答案

您无法使用 shell 或环境变量将此状态传回,但文件系统上的标志可以很好地完成这项工作。

#!/bin/bash
export status_dir=$(mktemp -t -d transfer.XXXXXX)
cleanup() { rm -rf "$status_dir"; }
trap cleanup 0 # automatically clean up on exit

do_Copy() {
el=$1
PRIMSEC=$2
scp david@"$local_dc":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/. \
|| { touch "$status_dir/local_down" && scp david@"$remote_dc_1":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/.; } \
|| { touch "$status_dir/primary_down" && scp david@"$remote_dc_2":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/.; } \
|| { touch "$status_dir/secondary_down"; exit 1; }
}

...以及以后...

[[ -e "$status_dir/local_down" ]] && \
mailx -r "david@host.com" -s "$local_dc machine down" "david@host.com" \
<<<"Local machine $local_dc was down, so copied from Primary Remote machine $remote_dc_1"

[[ -e "$status_dir/primary_down" ]] && \
mailx -r "david@host.com" -s "$remote_dc_1 machine down" "david@host.com" \
<<<"Primary Remote machine $remote_dc_1 was down, so copied from Secondary Remote machine $remote_dc_2"

[[ -e "$status_dir/secondary_down" ]] && \
mailx -r "david@host.com" -s "All three machine's are down" "david@host.com" \
<<<"All three machines are down. Exiting out."

关于linux - 如何在子shell中设置变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29424791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com