Search code examples
bashshellunixshsubshell

Bash script save csv in array and search for part of string


after editing my script I would shortly like to explain what i want to do:

  1. Check if files are in Folder
  2. Look at begin of file name
  3. search for file less than 1 hour old
  4. take the file and do sqlldr ..if this succeeds move file to an other folder ...if not send mail

This is my Script, can someone please tell me if this is going to work? I am not sure about the syntax and also not sure if nr. 3 and 4. send mail works like this.

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

Solution

  • You don't need an array if the read... and write... files can be processed in any order:

    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