简体   繁体   English

Bash 脚本在 SFTP 脚本后使用 ssconvert 批量将 convert.xlsx 文件转换为 csv 文件

[英]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.但是,在 SFTP 下载代码后在 Bash 脚本文件中添加相同内容时,它没有执行。 Please help.请帮忙。 Thank you!谢谢!

Bash script code - Bash 脚本代码 -

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

Using find may result in non executed stuff because it may spawn subprocesses which don't inherit parent variables.使用 find 可能会导致未执行的东西,因为它可能会产生不继承父变量的子进程。 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使用“./”作为目录容易出错,尤其是当您通过 cron 运行该脚本时
  • Using -name ".xlsx" in find command may skip uppercase files which just happen to exist when end users are involved在 find 命令中使用 -name ".xlsx" 可能会跳过在涉及最终用户时恰好存在的大写文件
  • Using a while loop with find will allow to expand your script later without having to worry about passing variables to find subprocesses使用带有 find 的 while 循环将允许稍后扩展您的脚本,而不必担心传递变量来查找子进程

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:)免责声明:与 bash 一起使用 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM