在shell中存储数据

时间:2015-06-17 02:05:07

标签: shell sh

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

我不知道如何实现这一目标并一直在寻找。

命令示例

gcb "type" "customernumber" "date"

我想提取与该客户编号相关联的电子邮件,并使用它填充变量。 我希望这可以存储在脚本中,如果可能的话,不会存储在单独的文件中。

@shellter

因此,正如您在上面看到的那样,我的命令的客户编号为$ 2,我正在努力让电子邮件查找器能够解决这个问题。所以我创建了一个脚本来测试电子邮件查找器功能。它可以正常工作,如下所示,但如果我想要它寻找$ 2 == cust_id它什么都不返回。以下是我的代码。

#!/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

我运行的测试命令是

sh emailtest.sh test 90624

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

CustomerNumber  CustomerName  Email

我将在此文件中存储更多数据以填充其他变量,我确信一旦弄明白,我可以理清其他数据。

感谢您的所有帮助。

2 个答案:

答案 0 :(得分:0)

#!/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)

: - |

  

如果[" $ 1" =" 42" ]。那么X =" CustomerA" ;科幻
     如果[" $ 1" =" 43" ]。那么X =" CustomerB" ;网络

答案 1 :(得分:0)

  

此脚本将被调用并传递一个客户编号。

myEmailFinder "$CustID"
  

我想根据传入的客户编号填充变量。

emailAddr=$( myEmailFinder "$CustID")
  

我想提取与该客户编号相关联的电子邮件,并使用它填充变量。

     

如果可能,我希望将其存储在脚本中,而不是存储在单独的文件中。

首选使用数据库,但....根据您的书面规范,请尝试使用

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       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 $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

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

更好的解决方案是将“查找表”存储在单独的文件中。那看起来像是

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

其中emaillist.cfg如上所示,以制表符分隔。

IHTH