将数据存储在 shell

Storing data in shell

我需要在 shell 脚本中存储电子邮件列表。该脚本将被调用并传递一个客户编号。根据客户编号,我想根据传入的客户编号填充一个变量。

我不确定如何完成这个并且一直在寻找。

命令示例

gcb "type" "customernumber" "date"

我想提取与该客户编号关联的电子邮件并用它填充一个变量。 如果可能的话,我希望将其存储在脚本中而不是单独的文件中。

@shell之三

正如您在上面看到的那样,我的命令将客户编号设为 $2,我正在尝试让电子邮件查找器考虑到这一点。所以我创建了一个脚本来测试电子邮件查找器功能。它工作正常,就像你在下面看到的那样,但如果我想让它寻找 $2 == cust_id 它 returns 什么都没有。下面是我的代码。

#!/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="" '{if ( == cust_id) {print }}'     /verification/custlist.cfg
       }


emailAddr=$( fn_myEmailFinder "")
echo $emailAddr

我运行测试的命令就是这个

sh emailtest.sh test 90624

我的配置文件布局如下,制表符分隔

CustomerNumber  CustomerName  Email

我将在这个文件中存储更多数据以填充其他变量,我相信一旦我弄明白了这一点,我就可以整理出其他数据。

非常感谢你的帮助。

#!/bin/bash -
''''echo "Customer number: "
X=$(/bin/env python [=10=] )
echo $X
exit
'''

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

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

:-|

如果 [ "$1" = "42" ];然后 X="CustomerA" ;菲
如果 [ "$1" = "43" ];然后 X="CustomerB" ; fi

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.

首选使用数据库,但是......根据您的书面规范,试试这个

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="" '{
         if ( == cust_id) {
                 print 
         }
        }' <<-EOF
          1       user1@myCorp.com
          2       user2@myCorp.com
          5       user3@myCorp.com
        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 
emailAddr=$( fn_myEmailFinder "")
mailx -S "Test Email" "$emailAddr" <<-EOM
   Here is the body of an email addressed to $emailAddr with CustID=$custID
EOM

#end of script

由 EOF 分隔的块是存储您的 custID 和相关电子邮件地址的地方。每行一个,制表符分隔。每行的缩进应该用制表符完成。结束 EOF 行必须仅使用制表符完成。

一个更好的解决方案是将 "lookup table" 存储在一个单独的文件中。那看起来像

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="" '{
         if ( == cust_id) {
                 print 
         }
        }' "$cfgDir"/emaillist.cfg

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

其中 emaillist.cfg 的布局如上,以制表符分隔。

IHTH