Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

Shell script worked correctly until I added variable :(

$
0
0
Hi everyone,

I have been using a shell script for the last 6 months to copy a database from a POS system, then analyse the database and print the current sales total. This has worked flawlessly, the only issue was that I had hard coded the IP address of the POS and occasionally I would need to edit the script to reflect the change. I decided to set the IP address programatically, and after playing around to find an easy way to detect the address I added a small piece of code to the previously working script.

Unfortunately the change I made has had an unexpected result, and the script no longer works as it should. The script accepts a single command line argument to specify if the processing should occur on a local copy of the database, or if a new version should be copied from the POS prior to processing, and it appears that the changes I made have broken the IF statement I use to perform that decision, based on the passed parameter. Here is the script before the changes:

Code:

#!/bin/bash

 if [[ $1 = "full" ]]
then
        sudo umount /mnt/reckon
        sudo mount.cifs //192.168.0.36/reckon /mnt/reckon -o user=posquery,password=**********,ro
        cp /mnt/reckon/Point\ of\ Sale\ Lite\ 2013\ Administrator/QBPOS.PDB /home/pi/
        echo "Select DateTime, ItemName, AmountGrossIncTax from Transaction where FunctionCaption='Total'" | mdb-sql -p /home/pi/QBPOS.PDB | grep $(date +"%m/%d/%y")
        echo
        printf "Total: $"
        echo "Select DateTime, ItemName, AmountGrossIncTax from Transaction where FunctionCaption='Total'" | mdb-sql -p /home/pi/QBPOS.PDB | grep $(date +"%m/%d/%y") | awk '{print $3}' | awk '{x+=$0}END{print x}'

elif [[ $1 = "local" ]]
then
        printf "Total: $"
        echo "Select DateTime, ItemName, AmountGrossIncTax from Transaction where FunctionCaption='Total'" | mdb-sql -p /home/pi/QBPOS.PDB | grep $(date +"%m/%d/%y") | awk '{print $3}' | awk '{x+=$0}END{print x}'
else
        umount /mnt/reckon
        sudo mount.cifs //192.168.0.36/reckon /mnt/reckon -o user=posquery,password=**********,ro
        cp /mnt/reckon/Point\ of\ Sale\ Lite\ 2013\ Administrator/QBPOS.PDB /home/pi/
        printf "Total: $"
        echo "Select DateTime, ItemName, AmountGrossIncTax from Transaction where FunctionCaption='Total'" | mdb-sql -p /home/pi/QBPOS.PDB | grep $(date +"%m/%d/%y") | awk '{print $3}' | awk '{x+=$0}END{print x}'
fi



As I said, the above code works perfectly. Here is the script after I updated it to find the IP address problematically, changes highlighted in red:



Code:

#!/bin/bash

ipaddress="$(nmblookup frontofhouse-pos | awk 'FNR == 2 {print $1}')"

echo $ipaddress

echo $1


 if [[ $1 = "full" ]]
then
        sudo umount /mnt/reckon
        sudo mount.cifs //"$ipaddress"/reckon /mnt/reckon -o user=posquery,password=**********,ro
        cp /mnt/reckon/Point\ of\ Sale\ Lite\ 2013\ Administrator/QBPOS.PDB /home/pi/
        echo "Select DateTime, ItemName, AmountGrossIncTax from Transaction where FunctionCaption='Total'" | mdb-sql -p /home/pi/QBPOS.PDB | grep $(date +"%m/%d/%y")
        echo
        printf "Total: $"
        echo "Select DateTime, ItemName, AmountGrossIncTax from Transaction where FunctionCaption='Total'" | mdb-sql -p /home/pi/QBPOS.PDB | grep $(date +"%m/%d/%y") | awk '{print $3}' | awk '{x+=$0}END{print x}'

elif [[ $1 = "local" ]]
then
        printf "Total: $"
        echo "Select DateTime, ItemName, AmountGrossIncTax from Transaction where FunctionCaption='Total'" | mdb-sql -p /home/pi/QBPOS.PDB | grep $(date +"%m/%d/%y") | awk '{print $3}' | awk '{x+=$0}END{print x}'
else
        umount /mnt/reckon
        sudo mount.cifs //"$ipaddress"/reckon /mnt/reckon -o user=posquery,password=**********,ro
        cp /mnt/reckon/Point\ of\ Sale\ Lite\ 2013\ Administrator/QBPOS.PDB /home/pi/
        printf "Total: $"
        echo "Select DateTime, ItemName, AmountGrossIncTax from Transaction where FunctionCaption='Total'" | mdb-sql -p /home/pi/QBPOS.PDB | grep $(date +"%m/%d/%y") | awk '{print $3}' | awk '{x+=$0}END{print x}'
fi

Unfortunately, when I run the script now I receive the following error, which point directly to the IF and ELSEIF lines:

./ip: 7: ./ip: [[: not found
./ip: 17: ./ip: [[: not found



Clearly I have broken the IF statement block with the addition of the ipaddress= assignment, but I don't understand why, or how to fix it?

I added the echo statements at the beginning of the script for testing, and both the ipaddress variable and the passed command line parameter are still correct, so what have I done wrong?

Any help would be most appreciated!

Thank you.

Viewing all articles
Browse latest Browse all 16232

Trending Articles