0

The below command works perfectly fine in command line mode

find . -maxdepth 1 -name "*.xlsx" -exec ssconvert {} --export-type=Gnumeric_stf:stf_csv \;

But while adding the same in a Bash script file after SFTP download code, it isn't executing. Please help. Thank you!

Bash script code -

/usr/bin/expect<<EOD

spawn /usr/bin/sftp -o Port=${PORT} username@hostname
expect "password:"
send "$Pass_Pwd\r"
expect "sftp>"
send "lcd ${Src_Dir}\r"
expect "sftp>"
send "mget ${File1}\r"
expect "sftp>"
send "mget ${File1}\r"
expect "sftp>"
send "bye\r"
EOD
echo "Download done"


find . -maxdepth 1 -name "*.xlsx" -exec ssconvert {} --export-type=Gnumeric_stf:stf_csv \;
Alekhya varma
  • 93
  • 2
  • 8
  • Possible duplicate of [How to loop through file names returned by find?](https://stackoverflow.com/q/9612090/608639), [find -exec cmd {} + vs | xargs](https://stackoverflow.com/q/896808/608639), [Why is looping over find's output bad practice?](https://unix.stackexchange.com/q/321697), [Looping through files with spaces in the names?](https://unix.stackexchange.com/q/9496), etc. – jww Nov 14 '19 at 20:28
  • Thank you for your links , But My main intention was to execute the ssconvert code after the sftp process is done, i have modified the question, please find it – Alekhya varma Nov 14 '19 at 21:41
  • What does "isn't executing" mean exactly? Does the computer just laugh at you, or do you maybe even get an error message? In the latter case, it might be helpful if you would post it here. – user1934428 Nov 15 '19 at 06:24
  • @user1934428 - Thank you for your response! But i guess you could say that in a better way. Sure, I can be more precise. So it downloads all the files and ends at "Download done". So, somehow it ignores the last line. – Alekhya varma Nov 15 '19 at 14:31
  • This still does not explain. It means that you don't see the last message. But you said the "download did not work", which is different from "a message has not been displayed". I guess the downloaded file is not there; so, did you get any error from the ftp commands which you are sending? – user1934428 Nov 17 '19 at 07:30
  • @Alekhyavarma : If the files don't have been downloaded, I would at least run expect with the [`-d`](https://wiki.tcl-lang.org/page/Debugging+Expect+programs) option, to get more information. – user1934428 Nov 17 '19 at 07:39
  • Changing `-name` to `-iname` should be the only change you need here (presuming that you have filenames with uppercase names -- otherwise the answer would not have been expected to work, as that's the only pertinent fix it makes). – Charles Duffy Apr 11 '22 at 16:46

1 Answers1

0

Using find may result in non executed stuff because it may spawn subprocesses which don't inherit parent variables. From what I see in your code, this shouldn't happen, but:

  • Using "./" as directory is error prone, especially when you'll run that script via cron
  • Using -name ".xlsx" in find command may skip uppercase files which just happen to exist when end users are involved
  • Using a while loop with find will allow to expand your script later without having to worry about passing variables to find subprocesses

Anyway, here's a hardened example of what you might want to code:

workdir = /data/ftpdownloads
cd "$workdir" || { echo 'Failed to chdir into $workdir' ; exit 1; }

# Insert your expect script here

while IFS= read -r -d $'\0' file; do
    ssconvert "$file" --export-type=Gnumeric_stf:stf_csv
done < <(find "$workdir" -type f -iname "*.xlsx" -print0)

Disclaimer: Use with bash :)

Orsiris de Jong
  • 2,819
  • 1
  • 26
  • 48
  • Wow! That worked like a charm, Thank you so much :) – Alekhya varma Nov 15 '19 at 16:01
  • Hey Orsiris, is there a way I could replace the < < with some wrapper script something like.. $1, because, although the while loop works exactly fine in command line as you have mentioned, while calling the same bash script through the informatica pre-session command task , i feel like somehow there is an error with the < < , because the ehile loop isnt executing over there, it is getting skipped! So, can you please help me with a script replacing the < < symbols with something else. Thank you – Alekhya varma Nov 25 '19 at 03:23
  • Probably the script gets executed bydash or something else. You could try running it really with `bash myscript.sh`. If that's not the reason, you could still output the find commands results into a tmp file and then read it from there without the second substitution `<`sign, rendering the whole thing more portable. – Orsiris de Jong Nov 25 '19 at 15:07