Search code examples
linuxbashcsvconvertersssconvert

Bash script to Batch convert .xlsx files to csv files using ssconvert after SFTP script


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 \;

Solution

  • 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 :)