Wrap a command for execution by the platform shell.
cmd /c "..." with doubled quotessh -c '...' with standard single-quote escaping| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | cmd |
function shell_wrap(cmd) result(wrapped) character(len=*), intent(in) :: cmd character(len=:), allocatable :: wrapped if (is_windows()) then wrapped = 'cmd /c "' // escape_quotes_win(trim(cmd)) // '"' else wrapped = "sh -c '" // escape_quotes_sh(trim(cmd)) // "'" end if contains function escape_quotes_win(s) result(r) character(len=*), intent(in) :: s character(len=:), allocatable :: r integer :: i r = "" do i = 1, len_trim(s) if (s(i:i) == '"') then r = r // '""' else r = r // s(i:i) end if end do end function escape_quotes_win function escape_quotes_sh(s) result(r) character(len=*), intent(in) :: s character(len=:), allocatable :: r integer :: i r = "" do i = 1, len_trim(s) if (s(i:i) == "'") then r = r // "'""'""'" else r = r // s(i:i) end if end do end function escape_quotes_sh end function shell_wrap