How to wait for system command to end

前端 未结 2 1774
渐次进展
渐次进展 2021-02-14 17:27

I\'m converting an XLS 2 CSV file with a system command in Ruby.

After the conversion I\'m processing the CSV files, but the conversion is still running when the progr

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-14 17:38

    Ruby's system("...") method is synchronous; i.e. it waits for the command it calls to return an exit code and system returns true if the command exited with a 0 status and false if it exited with a non-0 status. Ruby's backticks return the output of the commmand:

    a = `ls`
    

    will set a to a string with a listing of the current working directory.

    So it appears that xls2csv.exe is returning an exit code before it finishes what it's supposed to do. Maybe this is a Windows issue. So it looks like you're going to have to loop until the file exists:

    until File.exist?("file1.csv")
      sleep 1
    end
    

提交回复
热议问题