Shell script worked correctly until I added variable :(


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script worked correctly until I added variable :(
# 1  
Old 01-22-2015
Shell script worked correctly until I added variable :(

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:

Code:
./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.

Last edited by Scrutinizer; 01-22-2015 at 05:57 PM.. Reason: code tags
# 2  
Old 01-22-2015
This looks fishy to me. It seems like carriage returns have ended up in there. How did you edit this file?
# 3  
Old 01-22-2015
Quote:
Originally Posted by Corona688
This looks fishy to me. It seems like carriage returns have ended up in there. How did you edit this file?
I edited the script using nano. The text above I ran through notepad to remove passwords before posting it here so maybe notepad has messed up the text?

Actually thinking about it, I may have performed the real changes in notepad, then pasted them back in to the script using nano. Could that have killed it?
# 4  
Old 01-22-2015
It's possible. Try this.

tr -d '\r' < original.sh > fixed.sh
# 5  
Old 01-22-2015
Try using [ expr ] instead of [[ expr ]].
Just had the same thing when running a script.
This User Gave Thanks to sea For This Post:
# 6  
Old 01-22-2015
Quote:
Originally Posted by Corona688
It's possible. Try this.

tr -d '\r' < original.sh > fixed.sh
I just gave that a try and no difference unfortunately.
# 7  
Old 01-22-2015
Quote:
Originally Posted by gjws
I just gave that a try and no difference unfortunately.
did you try original.sh or fixed.sh?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. UNIX for Dummies Questions & Answers

Validation to be added in the shell script

I need help with one of my shell script. The script is working fine but i need to add two condition - i need to get rid of all the below ftp messages and need to have only ftp completed or failed message. example when i run the script i get below lines - Connected to xxxx 220 (vsFTPd... (1 Reply)
Discussion started by: chandraprakash
1 Replies

3. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

4. Shell Programming and Scripting

Shell $,/r getting added in echo on variable substitution.

Hi All, I'm trying to run a similar script to copy a files from one location to another. #!/bin/bash source="/home/pradeepk/a.txt" destination="/home/pradeepk/dir1" cp $source $destinationi'm getting following error. cp: cannot stat `/home/pradeepk/a.txt\r': No such file or directorywhen... (1 Reply)
Discussion started by: pradeep2002gs
1 Replies

5. Shell Programming and Scripting

CRON shell script only runs correctly on command line

Hi, I'm new to these forums, and I'm hoping that someone can solve this problem... To make things short: I have DD-wrt set up on a router. I'm trying to run a script in CRON that fetches the daily password from my database using SSH. CRON is set like so(in web interface): * * * *... (4 Replies)
Discussion started by: louieaw
4 Replies

6. Shell Programming and Scripting

Result of the grep is not storred correctly into the variable

I am facing a problem while storring the grep results into a variable. I need to count the occurence of the pattern \, in a file and store that in a variable. I have given the below command p=`grep -c '\\,' filename` But while echoing the variable, i am getting the total number of lines in... (2 Replies)
Discussion started by: renjithv
2 Replies

7. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

8. Shell Programming and Scripting

Shell script not unzipping and copying correctly

Hi, I am trying to unzip a file( $pfile, it contains a couple of files and 4 folders with subfolders and files) and have its contents go into a directory instead of into a folder in that directory (ZZZZ), I have the following script: #Unzip the build unzip -o "$HOME/ZZZZ/$pfile" -d... (2 Replies)
Discussion started by: SlumberMachine
2 Replies

9. Shell Programming and Scripting

Variable not working correctly.

Hi, I have a script where I am trying to set a local variable using the following, MYVAR="$NAME"_"$NAME2".txt where say, NAME = one NAME2 = two so I want the output one_two.txt but what I am getting is, two.txt basically the $NAME2 is overwriting, what am I doing wrong? ... (3 Replies)
Discussion started by: walsh_j
3 Replies

10. Shell Programming and Scripting

Variable with $ do not show correctly

Hey guys i need help i have a script to push a password to a remote server the only problem is the $ENCRYPT variable has $'s in it (from the encrypted password in the shadow file) and they drop out when apending to the shadow file via the usermod command so $1$Q/6a08n$EoAcBuR/YnoCQC shows up as... (3 Replies)
Discussion started by: insania
3 Replies
Login or Register to Ask a Question