语法错误Cygwin如果有其他条件

时间:2016-12-07 11:19:29

标签: bash if-statement cygwin

我正在使用Cygwin运行脚本,当我使用if else条件时,我收到语法错误。 你能告诉我我的剧本有什么问题吗?

  

./ test.sh:line 10:意外令牌附近的语法错误else' ./test.sh: line 10:其他'

#!/bin/bash
date
schema=''
table='JJJJ'
first=${table:0:1}
echo $first
if [$first == 'J'] 
echo 'SUCCESS';
else 
echo 'error';
fi

感谢

2 个答案:

答案 0 :(得分:2)

您的if声明不正确:

if [ "$first" == 'J' ]; then
  echo 'SUCCESS';
else 
  echo 'error';
fi

请注意then语句和$first变量的双引号。

答案 1 :(得分:1)

使用shellcheck.net这样的东西。您的#!/bin/bash date schema='' table='JJJJ' first=${table:0:1} echo $first if [ $first == 'J' ] then echo 'SUCCESS'; else echo 'error'; fi 附近存在语法错误。

正确的代码 -

import gateway
import datetime
import smtplib
merchant = {'merchantKey': '',
            'processorId': '',
            }
data = dict(merchant)
cardNumber = request.GET.get("cardNumber", "")
cardExpYear = request.GET.get("cardExpYear", "")
cardExpMonth = request.GET.get("cardExpMonth", "")
fullName = request.GET.get("fullName", "")
accountIDOriginal = request.GET.get("accountID", "")
accountID = accountIDOriginal.split('-')[0]
if Payments.objects.filter(accountID=accountID,paymentDate=str(datetime.date.today())).count() > 0:
    return HttpResponse('You Have already ran a payment today.')
else:
    theAccount = Account.objects.get(acctno=accountID)
    collectorID = theAccount.collector_id
    collectorEmail = Collector.objects.get(pk=collectorID).email
    ownerZip = request.GET.get("ownerZip", "")
    ownerState = request.GET.get("ownerState", "")
    ownerStreet = request.GET.get("ownerStreet", "")
    ownerCity = request.GET.get("ownerCity", "")
    cVV = request.GET.get("cVV", "")
    transactionAmountString = request.GET.get("transactionAmount", "")
    transactionAmount = Decimal(transactionAmountString)
    if ownerState == "CO" or ownerState == "co" or ownerState == "Co":
        creditCardCharge = round(transactionAmount * Decimal(1.00),2)
    else:
        creditCardCharge = round(transactionAmount * Decimal(1.03),2)
    data['cardNumber'] = cardNumber
    data['cardExpMonth'] = cardExpMonth
    data['cardExpYear'] = cardExpYear
    data['ownerState'] = ownerState
    data['ownerCity'] = ownerCity
    data['ownerName'] = fullName
    data['ownerStreet'] = ownerStreet
    data['ownerState'] = ownerState
    data['ownerZip'] = ownerZip
    data['cVV'] = cVV
    data['transactionAmount'] = creditCardCharge
    sale = gateway.RestGateway(data)
    resultsCC = str(sale.createSale())
    if sale.status == 'Success':
        fromaddr = 'email@domain.com'
        subject = 'New CC Payment'
        actualaccountid = Account.objects.get(acctno=accountID).accountid
        email = str(fullName) + str(' has made a payment for the amount of ') + str(transactionAmount) + str(
        '. Account ID is ') + str(accountID) +  str("""
       https://web.domain.com/admin/web/account/""") + str(actualaccountid)
        message = 'Subject: %s\n\n%s' % (subject, email)
        username = 'emailaddress'
        password = 'password'
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo()
        server.starttls()
        server.login(username, password)
        server.sendmail(fromaddr, collectorEmail, message)
        server.quit()
        previousBalance = theAccount.balance
        clientNumber = Account.objects.get(acctno=accountID).cl_no
        client = Client.objects.get(cl_no=clientNumber)
        clRates = Decimal(client.cl_rates)
        clientsRateCorrected = clRates / Decimal(100.00)
        agFee = Decimal(clientsRateCorrected) * transactionAmount
        # calculate taxes
        if client.cl_taxable == "Y":
            tax = transactionAmount * Decimal(client.cl_taxrate)
            tax = transactionAmount * Decimal(client.cl_taxrate)
        else:
            tax = '0.0'
        # calculate new balance
        newBalance = Decimal(theAccount.balance) - transactionAmount
        # save Account with new Balance
        theAccount.balance = Decimal(newBalance)
        # set new DLP
        theAccount.lastamt = transactionAmount
        theAccount.doctor_id = 3
        theAccount.lastpay = str(datetime.date.today())
        theAccount.save()
        newPayment = Payments(paymentDate=str(datetime.date.today()),
                          # claimDate='',
                          accountID_id=theAccount.acctno,
                          firstName=theAccount.first,
                          lastName=theAccount.last,
                          clientID=clientNumber,
                          pAgency=transactionAmount,
                          pClient=0.00,
                          rate=client.cl_rates,
                          agFee=agFee,
                          taxable=client.cl_taxable,
                          tax=tax,
                          # desc='',
                          fee=0.00,
                          # expense='',
                          # adjust='',
                          # salesman='',
                          balance=newBalance,
                          # intDue='',
                          status='$',
                          # area='',
                          ref_no='',
                          collector=theAccount.collector,
                          # cref_date='',
                          clName=client.cl_name,
                          # referred='',
                          # contact='',
                          isCC=True,
                          isProccessed=False,
                          creditCardChargedDate=str(datetime.date.today()),
                          )
        newPayment.save()
        paymentID = newPayment.paymentID
        handlingFee = Decimal(creditCardCharge) - Decimal(transactionAmount)
        clientName = Client.objects.get(cl_no=theAccount.cl_no).cl_name
        colelctorEmail = Collector.objects.get(name=theAccount.collector).email
        newReceipt = Invoice(paymentID=paymentID,
                         clientName=clientName,
                         accountNumber=theAccount.acctno,
                         claimNumber=theAccount.ref_no,
                         handlingFee=Decimal(handlingFee),
                         paymentAmount=Decimal(transactionAmount),
                         paymentAmountWithHandling=Decimal(creditCardCharge),
                         ccCharge=True,
                         newBalance=Decimal(newBalance),
                         previousBalance=Decimal(previousBalance),
                         originalBalance=Decimal(theAccount.referred),
                         paymentDate=str(datetime.date.today()),
                         collector=theAccount.collector,
                         fullName=fullName,
                         lastFourofCard=1234,
                         collectorEmailAddress = colelctorEmail
                         )
        newReceipt.save()
        return HttpResponse(str('https://domain.com/invoices/') + str(newReceipt.pk) + str('/') + str(theAccount.acctno) + str('/'))
   #return HttpResponse(final)
    else:
        fromaddr = 'email'
        subject = 'Error CC Payment'
        actualaccountid = Account.objects.get(acctno=accountID).accountid
        email = str(fullName) + str(' has attempted to make a payment for the amount of ') + str(transactionAmount) + str(
        '. Account ID is ') + str(accountID) + str("""

        Attached are the error results from the card being ran:
        """) +  str(resultsCC)
        message = 'Subject: %s\n\n%s' % (subject, email)
        username = 'email'
        password = 'Password'
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo()
        server.starttls()
        server.login(username, password)
        server.sendmail(fromaddr, collectorEmail, message)
        server.quit()
        response = "An Error Has Occured. Please Contact ____ for Help."
        return HttpResponse(response)