0

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 protected]}
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 protected]}
done
    done
2
  • for file in *.csv will look for all .csv files in root of the directory where script is run, it will not look in subfolders or folders above, as for only finding files with starting with read and write, you don't need arrays, as mentioned in Socowi answer Commented Nov 22, 2018 at 13:34
  • Shameless self plug, but I think you'll benefit from watching these videos on variable expansion and brace expansion: youtube.com/watch?v=yTijxqjZhRo and youtube.com/watch?v=82ESpisUh3Q Commented Nov 22, 2018 at 13:36

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you that helped a lot. Could you please also tell me how to insert an exception handling if the sqlldr doesnt work?
@Swoop Use sqldr ... || exceptionHandling. The latter part will only be executed if sqldr fails. You could for instance use { echo "error while processing $file" >&2; exit 1; }.
so now mei scrip looks like this: for file in read*.csv; do sqlldr readcsv || { echo "error while processing $file" >&2; exit 1; } rm read.csv done Will this work? I would like to delete the file after the sqlldr hase been done. And if the sqlld doesnt work just stop the sqlldr and dont delete the file
@Swoop. Why don't you try it yourself? But no, it will probably not work. You did not use "$file" in your sqlldr command. To delete only on success use if sqlldr ...; then rm "$file"; else echo ...; fi but your control flow should work too, because the script exits on error before removing.
I am just writing the script. i have no access to the unix. but thank you very much for helping.But what does $file exactly mean? And why would this work if i have sqlldr with read*csv and then rm $file Thank you!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.