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:
- Check if files are in Folder
- Look at begin of file name
- search for file less than 1 hour old
- 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
bash shell unix sh subshell
add a comment |
after editing my script I would shortly like to explain what i want to do:
- Check if files are in Folder
- Look at begin of file name
- search for file less than 1 hour old
- 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
bash shell unix sh subshell
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
– rAlen
Nov 22 '18 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
– Conner
Nov 22 '18 at 13:36
add a comment |
after editing my script I would shortly like to explain what i want to do:
- Check if files are in Folder
- Look at begin of file name
- search for file less than 1 hour old
- 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
bash shell unix sh subshell
after editing my script I would shortly like to explain what i want to do:
- Check if files are in Folder
- Look at begin of file name
- search for file less than 1 hour old
- 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
bash shell unix sh subshell
bash shell unix sh subshell
edited Dec 19 '18 at 10:27
Swoop
asked Nov 22 '18 at 13:21
SwoopSwoop
14
14
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
– rAlen
Nov 22 '18 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
– Conner
Nov 22 '18 at 13:36
add a comment |
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
– rAlen
Nov 22 '18 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
– Conner
Nov 22 '18 at 13:36
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
– rAlen
Nov 22 '18 at 13:34
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
– rAlen
Nov 22 '18 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
– Conner
Nov 22 '18 at 13:36
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
– Conner
Nov 22 '18 at 13:36
add a comment |
1 Answer
1
active
oldest
votes
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
Thank you that helped a lot. Could you please also tell me how to insert an exception handling if the sqlldr doesnt work?
– Swoop
Dec 18 '18 at 8:22
@Swoop Usesqldr ... || exceptionHandling
. The latter part will only be executed ifsqldr
fails. You could for instance use{ echo "error while processing $file" >&2; exit 1; }
.
– Socowi
Dec 18 '18 at 8:31
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
Dec 18 '18 at 8:42
@Swoop. Why don't you try it yourself? But no, it will probably not work. You did not use"$file"
in yoursqlldr
command. To delete only on success useif sqlldr ...; then rm "$file"; else echo ...; fi
but your control flow should work too, because the script exits on error before removing.
– Socowi
Dec 18 '18 at 9:06
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!!
– Swoop
Dec 18 '18 at 9:34
|
show 6 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431939%2fbash-script-save-csv-in-array-and-search-for-part-of-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Thank you that helped a lot. Could you please also tell me how to insert an exception handling if the sqlldr doesnt work?
– Swoop
Dec 18 '18 at 8:22
@Swoop Usesqldr ... || exceptionHandling
. The latter part will only be executed ifsqldr
fails. You could for instance use{ echo "error while processing $file" >&2; exit 1; }
.
– Socowi
Dec 18 '18 at 8:31
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
Dec 18 '18 at 8:42
@Swoop. Why don't you try it yourself? But no, it will probably not work. You did not use"$file"
in yoursqlldr
command. To delete only on success useif sqlldr ...; then rm "$file"; else echo ...; fi
but your control flow should work too, because the script exits on error before removing.
– Socowi
Dec 18 '18 at 9:06
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!!
– Swoop
Dec 18 '18 at 9:34
|
show 6 more comments
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
Thank you that helped a lot. Could you please also tell me how to insert an exception handling if the sqlldr doesnt work?
– Swoop
Dec 18 '18 at 8:22
@Swoop Usesqldr ... || exceptionHandling
. The latter part will only be executed ifsqldr
fails. You could for instance use{ echo "error while processing $file" >&2; exit 1; }
.
– Socowi
Dec 18 '18 at 8:31
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
Dec 18 '18 at 8:42
@Swoop. Why don't you try it yourself? But no, it will probably not work. You did not use"$file"
in yoursqlldr
command. To delete only on success useif sqlldr ...; then rm "$file"; else echo ...; fi
but your control flow should work too, because the script exits on error before removing.
– Socowi
Dec 18 '18 at 9:06
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!!
– Swoop
Dec 18 '18 at 9:34
|
show 6 more comments
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
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
answered Nov 22 '18 at 13:32
SocowiSocowi
6,4402724
6,4402724
Thank you that helped a lot. Could you please also tell me how to insert an exception handling if the sqlldr doesnt work?
– Swoop
Dec 18 '18 at 8:22
@Swoop Usesqldr ... || exceptionHandling
. The latter part will only be executed ifsqldr
fails. You could for instance use{ echo "error while processing $file" >&2; exit 1; }
.
– Socowi
Dec 18 '18 at 8:31
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
Dec 18 '18 at 8:42
@Swoop. Why don't you try it yourself? But no, it will probably not work. You did not use"$file"
in yoursqlldr
command. To delete only on success useif sqlldr ...; then rm "$file"; else echo ...; fi
but your control flow should work too, because the script exits on error before removing.
– Socowi
Dec 18 '18 at 9:06
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!!
– Swoop
Dec 18 '18 at 9:34
|
show 6 more 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
Dec 18 '18 at 8:22
@Swoop Usesqldr ... || exceptionHandling
. The latter part will only be executed ifsqldr
fails. You could for instance use{ echo "error while processing $file" >&2; exit 1; }
.
– Socowi
Dec 18 '18 at 8:31
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
Dec 18 '18 at 8:42
@Swoop. Why don't you try it yourself? But no, it will probably not work. You did not use"$file"
in yoursqlldr
command. To delete only on success useif sqlldr ...; then rm "$file"; else echo ...; fi
but your control flow should work too, because the script exits on error before removing.
– Socowi
Dec 18 '18 at 9:06
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!!
– Swoop
Dec 18 '18 at 9:34
Thank you that helped a lot. Could you please also tell me how to insert an exception handling if the sqlldr doesnt work?
– Swoop
Dec 18 '18 at 8:22
Thank you that helped a lot. Could you please also tell me how to insert an exception handling if the sqlldr doesnt work?
– Swoop
Dec 18 '18 at 8:22
@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; }
.– Socowi
Dec 18 '18 at 8:31
@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; }
.– Socowi
Dec 18 '18 at 8:31
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
Dec 18 '18 at 8:42
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
Dec 18 '18 at 8:42
@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.– Socowi
Dec 18 '18 at 9:06
@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.– Socowi
Dec 18 '18 at 9:06
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!!
– Swoop
Dec 18 '18 at 9:34
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!!
– Swoop
Dec 18 '18 at 9:34
|
show 6 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431939%2fbash-script-save-csv-in-array-and-search-for-part-of-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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
– rAlen
Nov 22 '18 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
– Conner
Nov 22 '18 at 13:36