Storing data in shell

103 views Asked by At

I have a need to store a list of e-mails in a shell script. This script will get called and passed a customer number. depending on the customer number I want to populate a variable based on the passed in customer number.

I am not sure how to accomplish this and have been looking.

command example

gcb "type" "customernumber" "date"

I want to pull an e-mail associated with that customer number and populate a variable with it. I would prefer this get stored in the script and not in a separate file if possible.

@shellter

So as you can see above my command has the customer number as $2, i am trying to get the email finder to work with that in mind. So I created a script to test the e-mail finder function with. It works fine as you have it below, but if i want it to look for $2 == cust_id it returns nothing. Here is my code below.

#!/bin/sh
#case $# in 0 ) echo "usage: myEmailFinder2 CustID" ; exit 1 ;; esac
cfgDir="/verification"

# given cust file like
# cust_id "\t" email_addr
fn_myEmailFinder() {
       awk -F"\t" -v cust_id="$2" '{if ($2 == cust_id) {print $3}}'     /verification/custlist.cfg
       }


emailAddr=$( fn_myEmailFinder "$1")
echo $emailAddr

The command I run to test this is this

sh emailtest.sh test 90624

My config file is layed out like this, tab delimited

CustomerNumber  CustomerName  Email

I am going to store more data in this file to populate other variables, I'm sure once i get this figured out, I can sort out the other data.

I appreciate all of your help.

2

There are 2 answers

3
shellter On BEST ANSWER

This script will get called and passed a customer number.

myEmailFinder "$CustID"

I want to populate a variable based on the passed in customer number.

emailAddr=$( myEmailFinder "$CustID")

I want to pull an e-mail associated with that customer number and populate a variable with it.

I would prefer this get stored in teh script and not in a separate file if possible.

Using a database is preferred, but .... per your written specification, try this

cat myEmailFinder
#!/bin/bash
case $# in 0 ) echo "usage: myEmailFinder CustID" ; exit 1 ;; esac

# given cust file like
# cust_id "\t" email_addr
fn_myEmailFinder() {
    awk -F"\t" -v cust_id="$1" '{
         if ($1 == cust_id) {
                 print $2
         }
        }' <<-EOF
          1       [email protected]
          2       [email protected]
          5       [email protected]
        EOF
        #--^tabCh^---make sure you put a real tab char between custID and emailAddr
    #tabCh-TabCh--- data indented with TabChars. EOS indented with **only** tabCh.

#send an email to cust in $1
emailAddr=$( fn_myEmailFinder "$1")
mailx -S "Test Email" "$emailAddr" <<-EOM
   Here is the body of an email addressed to $emailAddr with CustID=$custID
EOM

#end of script

The block delimited by EOF is the place to store your custID and associated email Addresses. One per line, tab-delimited. The Indents on each line should be done with tab chars. The closing EOF line must be done ONLY with tab chars.

A preferable solution would be to store the "lookup table" in a separate file. That would look like

cat myEmailFinder2
#!/bin/bash
case $# in 0 ) echo "usage: myEmailFinder2 CustID" ; exit 1 ;; esac

cfgDir="/usr/local/data"

# given cust file like
# cust_id "\t" email_addr
fn_myEmailFinder() {
    awk -F"\t" -v cust_id="$1" '{
         if ($1 == cust_id) {
                 print $2
         }
        }' "$cfgDir"/emaillist.cfg

#send an email to cust in $1
emailAddr=$( fn_myEmailFinder "$1")
mailx -S "Test Email" "$emailAddr" <<-EOM
   Here is the body of an email addressed to $emailAddr with CustID=$custID
EOM

where emaillist.cfg is laid out as above, tab-delimited.

IHTH

0
TessellatingHeckler On
#!/bin/bash -
''''echo "Customer number: $1"
X=$(/bin/env python $0 $1)
echo $X
exit
'''

customers = {
     42: 'customerA'
    ,43: 'customerB'
}

import sys
print customers.get(int(sys.argv[1]), '')
sys.exit(0)

:-|

if [ "$1" = "42" ]; then X="CustomerA" ; fi
if [ "$1" = "43" ]; then X="CustomerB" ; fi