Arrays 附加到Bash关联数组中的元素

Arrays 附加到Bash关联数组中的元素,arrays,csv,Arrays,Csv,我的名字是迈克,我是Bash编程的一个彻头彻尾的笨蛋。话虽如此,请不要把我嚼成肉酱。这是我的设想。我正在导入csv并逐行处理它。它在AD和管道中查找导致变量$ADresult1的信息 然后,我获取该输出并将其发送到数组中 declare -A arrmail arrmail[$ADresult1]="$v1, $v3" appendvar+="; ${arrmail[$ADresult1]}" if [ ! -z "${arrmail[$ADresult1]}" ]; then echo

我的名字是迈克,我是Bash编程的一个彻头彻尾的笨蛋。话虽如此,请不要把我嚼成肉酱。这是我的设想。我正在导入csv并逐行处理它。它在AD和管道中查找导致变量$ADresult1的信息

然后,我获取该输出并将其发送到数组中

declare -A arrmail
arrmail[$ADresult1]="$v1, $v3"
appendvar+="; ${arrmail[$ADresult1]}"
if [ ! -z "${arrmail[$ADresult1]}" ]; then
    echo ${arrmail[@]}
else
    echo ${arrmail[@]}="$appendvar"
fi
现在在我的CSV文件中,var
$ADresult1
有多个相同的条目,但是每个元素应该不同

例如:第一行可能包含
arrmail[p100]=Y2K,Y2J
第二行可能
arrmail[p100]=GGG,GG1

我的问题是如何将第一行和第二行的元素组合成一行输出和/或如果可能的话

例如
arrmail[p100]=Y2K,Y2J;GGG,GG1

如果需要的话,我可以发布整个脚本以获得更大的图片

这是剧本

#!/bin/bash

###--------------------------------------------------------------------------
# Author : Volha Zhdanava (p146819)
# Date   : 11-FEB-2013
# Purpose: To automate the process of sending out notification emails to developers when 
# Assigned DEV Machine is on the Expired Devices List.
#----------------------------------------------------------------------------
#            ----MODIFIED----
#----------------------------------------------------------------------------------------------------
#  Author               Revsn Date      Revsn ID        Revsn Purpose
# Mike Bell         02-28-14    Rev.03      Add ability to email j or p number
# Mike Bell     01-02-15    Rev.04      Add verbiage about 90 day licensing
###----------------------------------------------------------------------------------------------------


Purple='\e[1;35m'
ColorOFF='\e[0m'
TO_TEAM="dev-tools@edwardjones.com"

#-----------------------------------------
#Ask for input CSV file
#-----------------------------------------

FILEDIR="/export/share/is/dev_env/expdev"
FILENAME="expired_devices.csv"

#if the directory is not empty, find the csv file
if [[ $(ls -A $FILEDIR) ]]
then
#check if the csv file was stored with default name <expired_devices.csv>

       if [ -f $FILEDIR/$FILENAME ]
      then
#absolute path to the downloaded file
       CSVFILE=`echo $FILEDIR/$FILENAME`
#if the csv file was stored under a user-defined name,
#re-define the absolute path to that file

      else  
             #count how many files are saved in the expdev directory
             NAME=( `ls -A $FILEDIR` )
             CNT=${#NAME[@]}
             echo -e "There is/are ${Purple}${CNT}${ColorOFF} file(s) in the ${Purple}$FILEDIR${ColorOFF} directory:"
         echo ${NAME[@]}
        #if there is only one file saved within the expdev directory
                if [ ${CNT} -eq 1 ]
                   then
                     FILENAME=${NAME[0]}          
                   else
        #it will prompt to enter the name of the file
                 REPLY="n"
                 while [ "$REPLY" != "y" ]  
                 do
                  echo -e "Please enter the full name of the downloaded csv file: \c"
                  read FILENAME
                  echo -e "You entered ${Purple}$FILENAME${ColorOFF}. Is this correct? y/n: \c\n"
                  read ANSWER 
                  REPLY=`echo $ANSWER | tr [:upper:] [:lower:]`
                 done
                 fi
              #verify the specified file exists in <expdev> directory 

              echo `ls -Al $FILEDIR/$FILENAME` 
              echo -e "\nThe script will run against the listed above ${Purple}$FILENAME${ColorOFF} file.\n${Purple}NOTE${ColorOFF}: Do NOT proceed if it says ${Purple}No such file or directory${ColorOFF}.\nWould you like to proceed? y/n -->:\c "
                 read PROCEED
                 PROCEED=`echo $PROCEED | tr [:upper:] [:lower:]`
                     if [ "$PROCEED" == "y" ]
                           #user agrees
                       then
                        CSVFILE=`echo $FILEDIR/$FILENAME`
                       else
                        echo -e "Make sure you saved your CSV file in ${Purple}$FILEDIR${ColorOFF} directory and run the script again."
                        exit 1
                     fi

        fi
#----------------------------------------------
#Remove header from CSVFILE
#and create a temp csvfile.txt 
#for "WHILE READ" loop
#----------------------------------------------
Nrows=`cat ${CSVFILE}| wc -l`
N=`expr $Nrows - 1`
tail -$N ${CSVFILE} > csvfile.txt
#----------------------------------------------
#Determine the Extension Date
#----------------------------------------------
TODAYsDATE=`date +"%a"`
case $TODAYsDATE in
Mon ) ExtDate=`date --date='4 day' +"%A, %d-%b-%Y"`
    ;;
Tue ) ExtDate=`date --date='6 day' +"%A, %d-%b-%Y"`
    ;;
Wed ) ExtDate=`date --date='6 day' +"%A, %d-%b-%Y"`
    ;;
Thu ) ExtDate=`date --date='6 day' +"%A, %d-%b-%Y"`
    ;;
Fri ) ExtDate=`date --date='6 day' +"%A, %d-%b-%Y"`
    ;;
* ) echo "Cannot determine the Extention Date."
    REPLY="n"
    while [ "$REPLY" != "y" ]
    do
    echo -e "Please enter Extended Date: \c"
    read ExtDate
    echo -e "You entered ${Purple}$ExtDate${ColorOFF}. Is this correct? y/n: \c "
    read ANSWER
    REPLY=`echo $ANSWER | tr [:upper:] [:lower:]`
    done

esac

echo -e "The Extended Date for the Application Owners will be ${Purple}$ExtDate${ColorOFF}.\nPlease make a note for End Date in the APEX Workstation Tracking Tool." 

#create a temp confirmation file that will be sent to dev-tools
echo -e "\nThe Extended Date is $ExtDate.\nSent emails to: " > confirmation.txt

#----------------------------------------------
#Read the csvfile.txt
#----------------------------------------------
while IFS=','  read v1 v2 v3 v4 other
do

#----------------------------------------------
#Determine email address
#----------------------------------------------
v2=` echo $v2 | tr '"/,\&' ' ' `
NAME=( ${v2} )
cnt=${#NAME[@]}
#-----------------------------------------------
# Filter j or p<number> userids from table V2
#--------------------------------------------------

if [[ "$NAME" =~ [P|p][[:digit:]]{6}$|[J|j][[:digit:]]{5}$ ]]

    then
        ADresult1=`getent passwd "$NAME" | awk -F":" '{print $1}'`
echo ${ADresult1}

#------------------------------------------------------------------------------------------------------
# Processing Array
#-----------------------------------------------------------------------------------------------------

declare -A arrmail

arrmail[$ADresult1]="$v1, $v3"

appendvar+="; ${arrmail[$ADresult1]}"

if [ ! -z "${arrmail[$ADresult1]}" ]

        then
               echo ${arrmail[@]}
else

        echo ${arrmail[@]}="$appendvar"
fi

#--------------------------------------------------------------------------------------
# If query matches j or p#, then email user
#------------------------------------------------------------------------------------------
#
#
#           v1=` echo $v1 | tr '"' ' ' `
#                        SUBJECT=" ${v1}, ${v3} - Your Development Machine Status Update"
#
#                        TO="${ADresult1}@edwardjones.com"
#
#                        v4=` echo $v4 | tr '"' ' ' `
#
#           devmailmessage="\nGreetings ${NAME[0]}, \n\nI am in the process of cleaning up the machines in our lab.\n\nAssigned device: ${v1} \n\nEffort: ${v3}\n\nThe above machine have been assigned to you since ${v4}.\n\nDo you still need this machine or can I reclaim it?\nIf this machine is still required, please let us know how much longer you will need it.\nPlease keep in mind that Dev. workstations or laptops can only be assigned for 90 days due to licensing restrictions.\n\nIf this machine is no longer needed, please let us know also.\n\nThis machine will be rebuilt on or after $ExtDate.\n\nPlease reply to dev-tools@edwardjones.com by $ExtDate.\n\n\nThank you,\n\nDevelopment Environment Support team\ndev-tools@edwardjones.com " 
#
#echo -e ${devmailmessage}|mail -s "$SUBJECT" $TO -- -f $TO_TEAM

#--------------------------------------------------------------------------------------------
# If query does not match j or p number, then lookup username via first and last name
#----------------------------------------------------------------------------------------------

elif [ ${cnt} -eq 2 ]

    then

        ADresult=`getent passwd | grep -i "${NAME[0]}" | grep -i "${NAME[1]}"`

echo ${ADresult}
#count search results from Active Directory; only email if there is one unique result
        ADsearch1=`echo ${ADresult} | grep -i "${NAME[0]}" | wc -l`
        ADsearch2=`echo ${ADresult} | grep -i "${NAME[1]}" | wc -l`
                if [ ${ADsearch1} -eq 1 ] && [ ${ADsearch2} -eq 1 ]
                   then
                        pNumber=`echo ${ADresult} | awk -F":" '{print $1}'`
#------------------------------------------------------------------------------------------------------
# Processing Array
#-----------------------------------------------------------------------------------------------------

#declare -A arrmail

#arrmail[$pNumber]="$v1, $v3"

#appendvar+="; ${arrmail[$pNumber]}"

#if [ ! -z "${arrmail[$pNumber]}" ]

#        then
#           {arrmail[$pNumber]}="${arrmail[$ADresult1]}"
#else

#   {arrmail[$ADresult1]}="$appendvar"
#fi

#------------------------------------------------------------------------------------------------------------
#                        v1=` echo $v1 | tr '"' ' ' `
 #                       SUBJECT=" ${v1}, ${v3} - Your Development Machine Status Update"

  #                      TO="${pNumber}@edwardjones.com"

   #                     v4=` echo $v4 | tr '"' ' ' `

#           devmailmessage="\nGreetings ${NAME[0]}, \n\nI am in the process of cleaning up the machines in our lab.\n\nAssigned device: ${v1} \n\nEffort: ${v3}\n\nThe above machine have been assigned to you since ${v4}.\n\nDo you still need this machine or can I reclaim it?\nIf this machine is still required, please let us know how much longer you will need it.\nPlease keep in mind that Dev. workstations or laptops can only be assigned for 90 days due to licensing restrictions.\n\nIf this machine is no longer needed, please let us know also.\n\nThis machine will be rebuilt on or after $ExtDate.\n\nPlease reply to dev-tools@edwardjones.com by $ExtDate.\n\n\nThank you,\n\nDevelopment Environment Support team\ndev-tools@edwardjones.com "

 #                       echo -e ${devmailmessage}| mail -s "$SUBJECT" $TO -- -f $TO_TEAM
#       else
 #                           TO=`echo "ACTION REQUIRED: Email address cannot be determined for the following user: ${v2}, with assigned device: ${v1}. Please email this user yourself."`
               fi

#            else
 #                          TO=`echo "ACTION REQUIRED: Email address cannot be determined for the following user: ${v2}, with assigned device: ${v1}. Please email this user yourself."`

    fi
#echo -e "\n$TO ${v2}, ${v1}" >> confirmation.txt
echo "NEXT ROW"

done < csvfile.txt

#----------------------------------------------
#sending out confirmation email to dev-tools
#----------------------------------------------
#subject="WW - Notification Emails to Developers - Status"
#cat confirmation.txt |  mail -s "$subject" $TO_TEAM -- -f $TO_TEAM

#echo -e "Result: Processing is COMPLETED. Please check the ${Purple}$subject${ColorOFF} email. Note: ${Purple}$FILENAME${ColorOFF} file was deleted from ${Purple}$FILEDIR${ColorOFF} directory to avoid duplicates in the future."
rm ${CSVFILE}
rm csvfile.txt
rm confirmation.txt

else
#----------------------------------------------------
#if the directory is empty
#------------------------------------------------------
echo -e "Please make sure the CSV file was downloaded to ${Purple}$FILEDIR${ColorOFF} directory and run the script again."

fi


###
#--------THE END---------------------------------
###
#/bin/bash
###--------------------------------------------------------------------------
#作者:Volha Zhdanava(第46819页)
#日期:2013年2月11日
#目的:在需要时自动向开发人员发送通知电子邮件
#已分配的开发人员计算机位于过期设备列表中。
#----------------------------------------------------------------------------
#----修改----
#----------------------------------------------------------------------------------------------------
#作者Revsn日期Revsn ID Revsn用途
#Mike Bell 02-28-14 Rev.03添加电子邮件j或p号码功能
#Mike Bell 01-02-15 Rev.04添加关于90天许可证的措辞
###----------------------------------------------------------------------------------------------------
紫色='\e[1;35m'
ColorOFF='\e[0m'
发送至_TEAM=“dev”-tools@edwardjones.com"
#-----------------------------------------
#请求输入CSV文件
#-----------------------------------------
FILEDIR=“/export/share/is/dev_env/expdev”
FILENAME=“expired_devices.csv”
#如果目录不为空,请查找csv文件
如果[[$(ls-A$FILEDIR)]]
然后
#检查csv文件是否以默认名称存储
如果[-f$FILEDIR/$FILENAME]
然后
#下载文件的绝对路径
CSVFILE=`echo$FILEDIR/$FILENAME`
#如果csv文件以用户定义的名称存储,
#重新定义该文件的绝对路径
其他的
#计算expdev目录中保存的文件数
NAME=(`ls-A$FILEDIR`)
CNT=${#NAME[@]}
echo-e“在${Purple}$FILEDIR${ColorOFF}目录中有${Purple}${CNT}${ColorOFF}文件:”
echo${NAME[@]}
#如果expdev目录中只保存了一个文件
如果[${CNT}-eq 1]
然后
FILENAME=${NAME[0]}
其他的
#它将提示输入文件名
答复=“n”
而[“$REPLY”!=“y”]
做
echo-e“请输入下载的csv文件的全名:\c”
读取文件名
echo-e“您输入了${Purple}$FILENAME${ColorOFF}。是否正确?y/n:\c\n”
阅读答案
REPLY=`echo$ANSWER | tr[:上限:[:下限:]`
完成
fi
#验证目录中是否存在指定的文件
echo`ls-Al$FILEDIR/$FILENAME`
echo-e“\n脚本将针对上面列出的${Purple}$FILENAME${ColorOFF}文件运行。\n${Purple}注意${ColorOFF}:如果它说${Purple}没有这样的文件或目录${ColorOFF},请不要继续。\n是否继续?y/n-->:\c”
继续阅读
继续=`echo$procedue | tr[:上限:[:下限:]`
如果[“$PROCEND”==“y”]
#用户同意
然后
CSVFILE=`echo$FILEDIR/$FILENAME`
其他的
echo-e“确保将CSV文件保存在${Purple}$FILEDIR${ColorOFF}目录中,然后再次运行脚本。”
出口1
fi
fi
#----------------------------------------------
#从CSVFILE中删除头文件
#并创建一个临时csvfile.txt
#用于“读时”循环
#----------------------------------------------
Nrows=`cat${CSVFILE}| wc-l`
N=`expr$Nrows-1`
tail-$N${CSVFILE}>CSVFILE.txt
#----------------------------------------------
#确定延期日期
#----------------------------------------------
今天日期=`date+%a“`
案件$今日(星期四)
Mon)ExtDate=`date--date='4天'+%A,%d-%b-%Y`
;;
星期二)ExtDate=`date--date='6天'+%A,%d-%b-%Y`
;;
Wed)ExtDate=`date--date='6天'+%A,%d-%b-%Y`
;;
Thu)ExtDate='date--date='6天'+%A,%d-%b-%Y'`
;;
Fri)ExtDate=`date--date='6天'+%A,%d-%b-%Y`
;;
*)echo“无法确定延长日期。”
答复=“n”
而[“$REPLY”!=“y”]
做
echo-e“请输入延长日期:\c”
读取ExtDate
echo-e“您输入了${Purple}$ExtDate${ColorOFF}。是否正确?y/n:\c”
阅读答案
REPLY=`echo$ANSWER | tr[:上限:[:下限:]`
完成
以撒
echo-e“应用程序所有者的延长日期将为${Purple}$ExtDate${ColorOFF}。\n请在APEX工作站跟踪工具中记录结束日期。”
#创建将发送到开发工具的临时确认文件
echo-e“\n扩展日期为$ExtDate。\n将电子邮件发送到:“>confirmation.txt”
#----------------------------------------------
#阅读csvfile.txt文件
#----------------------------------------------
当IFS='时,读取v1 v2 v3 v4其他
做
#----------------------------------------------
#确定电子邮件地址
#----------------------------------------------
v2=`echo$v2 | tr'/,\&''`
名称=(${v2})
cnt=${#NAME[@]}
#------