DST打破了bash脚本,如何设置变量的本地时间?

时间:2014-10-29 11:11:27

标签: bash shell centos

我有一些简单的脚本可以生成和发送/评估SOAP请求到许多Web服务,实际上它们提交了一个示例请求以确保Web服务已启动,并按预期运行。

它们用作我的客户的状态检查。

所以DST刚刚改变了,因为bash的时间是UTC而且我在BST / GMT,脚本停止工作,我已经尝试了我能想到的一切(导出TZ =“:欧洲/都柏林“等等......但没有欢乐。

您知道如何更改“ctime”变量以产生当地时间吗?

#!/bin/bash

#//Get parameters
ENV="$1"
ENV2="$2"

checkdir="/opt/local/c/c/c/"

#//Generate nonce and hashed password
nonce=$(echo "$RANDOM$RANDOM$RANDOM")
hashnonce=$(echo $nonce | base64)
passwd="tiger"
ctime=`echo $(date +'%s')`
etime=`echo "$ctime+600" | bc`
timestamp=`echo $(date -d "1970-01-01 00:00:00 $ctime seconds" +'%Y-%m-%dT%H:%M:%S.%3NZ')`
timecreate=`echo $(date -d "1970-01-01 00:00:00 $ctime seconds" +'%Y-%m-%dT%H:%M:%SZ')`
timeexpire=`echo $(date -d "1970-01-01 00:00:00 $etime seconds" +'%Y-%m-%dT%H:%M:%SZ')`
combined=`echo -e "$nonce\n$timestamp$passwd"`
hashpass=$(echo -n "$combined" | openssl sha1 -binary | base64)

#//Output generated XML to file
echo -e '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sear="http://www.x.com/c/c/c">
<soapenv:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soapenv:mustUnderstand="1"><wsu:Timestamp wsu:Id="TS-20"><wsu:Created>'$timecreate'</wsu:Created><wsu:Expires>'$timeexpire'</wsu:Expires></wsu:Timestamp><wsse:UsernameToken wsu:Id="UsernameToken-19"><wsse:Username>scott</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">'$hashpass'</wsse:Password><wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">'$hashnonce'</wsse:Nonce><wsu:Created>'$timestamp'</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header>
<soapenv:Body>
<sear:searchRequest>
<sear:importantNumber>'$ENV2'</sear:importantNumber>
<sear:status>R</sear:status>
<sear:versionNumber>1.0</sear:versionNumber>
</sear:searchRequest>
</soapenv:Body>
</soapenv:Envelope>' > $checkdir/Request_$ENV.xml

#//Send soap request and output to text file
curl -s --header "content-type: application/soap+xml" --data @$checkdir/Request_$ENV.xml https://$ENV.x.com:443/c/c/c/c/ > $checkdir/Result_$ENV.txt

#//Grep for response
echo `grep "Complete" $checkdir/Result_$ENV.txt | wc -l`

exit;

2 个答案:

答案 0 :(得分:2)

这是一个解决方案。我删除了所有无用的echo以及反引号。您的问题的解决方案只是将TZ提供给date,请参阅下文。

#!/bin/bash

# Get parameters
if (($# != 2)); then
    echo "USAGE: $0 arg1 arg2" >&2    # Specify what parameters you need
    exit 1
fi
ENV="$1"
ENV2="$2"

# Configuration
MYTZ="Europe/Dublin"
checkdir="/opt/local/c/c/c/"

# Generate nonce and hashed password
nonce="$RANDOM$RANDOM$RANDOM"
hashnonce=$(echo $nonce | base64)
passwd="tiger"
ctime=$(TZ="$MYTZ" date +'%s')
etime=$(echo "$ctime+600" | bc)
timestamp=$(TZ="$MYTZ" date -d "1970-01-01 00:00:00 $ctime seconds" +'%Y-%m-%dT%H:%M:%S.%3NZ')
timecreate=$(TZ="$MYTZ" date -d "1970-01-01 00:00:00 $ctime seconds" +'%Y-%m-%dT%H:%M:%SZ')
timeexpire=$(TZ="$MYTZ" date -d "1970-01-01 00:00:00 $etime seconds" +'%Y-%m-%dT%H:%M:%SZ')
combined=$(echo -e "$nonce\n$timestamp$passwd")
hashpass=$(echo -n "$combined" | openssl sha1 -binary | base64)

# Output generated XML to file
cat > "$checkdir/Request_$ENV.xml" << ENDOFTEXT
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sear="http://www.x.com/c/c/c">
<soapenv:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soapenv:mustUnderstand="1"><wsu:Timestamp wsu:Id="TS-20"><wsu:Created>'$timecreate'</wsu:Created><wsu:Expires>'$timeexpire'</wsu:Expires></wsu:Timestamp><wsse:UsernameToken wsu:Id="UsernameToken-19"><wsse:Username>scott</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">'$hashpass'</wsse:Password><wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">'$hashnonce'</wsse:Nonce><wsu:Created>'$timestamp'</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header>
<soapenv:Body>
<sear:searchRequest>
<sear:importantNumber>'$ENV2'</sear:importantNumber>
<sear:status>R</sear:status>
<sear:versionNumber>1.0</sear:versionNumber>
</sear:searchRequest>
</soapenv:Body>
</soapenv:Envelope>
ENDOFTEXT

# Send soap request and output to text file
curl -s --header "content-type: application/soap+xml" --data "@$checkdir/Request_$ENV.xml" https://$ENV.x.com:443/c/c/c/c/ > "$checkdir/Result_$ENV.txt"

# Grep for response
grep "Complete" $checkdir/Result_$ENV.txt | wc -l

exit

答案 1 :(得分:0)

所以以下内容对我有用。

#Decide if it's winter or summer time, alter var ctime accordingly
ctime=$(date +%s)
if perl -e 'exit ((localtime)[8])' ; then
    ((ctime+=3600))
fi

我知道代码非常混乱,感谢提示小伙伴们,回到脚本中运行Perl,不理想,但它有效:) - 对你们所有的好业力:)