Bash脚本将csv保存在数组中并搜索字符串的一部分

时间:2018-11-22 13:21:23

标签: bash shell unix sh subshell

在编辑脚本后,我不久想解释一下我想做什么:

  1. 检查文件是否在文件夹中
  2. 查看文件名的开头
  3. 搜索不到1小时的文件
  4. 获取文件并执行sqlldr ..如果成功,则将文件移至其他文件夹...如果不发送邮件

这是我的脚本,有人可以告诉我这是否有效吗?我不确定语法,也不确定nr。 3和4。发送邮件是这样的。

    #!/bin/sh

    #check if files are in folder
    declare -a arrCSV   #create array
    for file in *.csv
    do
    arrCSV=("${CSV[@]}" "$file")
    done

    shopt -s nullglob
    for file in read*.csv; do
    #run on all files starting with "read" and ending with ".csv" 
  for find $LOCATION -name $file -type f -mmin -60 do
    if
    sqlldr read*.csv 
then mv "$file" "$HOME/fail/" ;
else{ echo "Failed to load" | mail -s "FAIL" email@email.com}
done
    done

    for file in write*.csv; do
    #run on all files starting with "write" and ending with ".csv" 
      for find $LOCATION -name $file -type f -mmin -60 do
 if
sqlldr write*.csv 
then mv "$filen" "$HOME/unisem/fail/" ;
else { echo "Failed to load 2" | mail -s "FAIL" email@email.com}
done
    done

1 个答案:

答案 0 :(得分:1)

如果可以以任何顺序处理读写文件,则不需要数组:

shopt -s nullglob
for file in read*.csv; do
    # run on all files starting with "read" and ending with ".csv" 
    sqldr ...
done
for file in write*.csv; do
    # run on all files starting with "write" and ending with ".csv" 
    sqldr ...
done