linux - Nested grep with SSH -
i have following shell command runs successfully:
sed -e "1,/^$(date -d -24hour +'%y-%m-%d %h')/d" logfile | grep -a20 -b10 'exception' | mail -s "sample report" xyz@yourdomain
however, have ssh particular machine run this. avoid this, modified in following way leads failure:
ssh myserver 'sed -e "1,/^$(date -d -24hour +'%y-%m-%d %h')/d" logfile | grep -a20 -b10 'exception' | mail -s "sample report" xyz@yourdomain '
what wrong command? how can fix this?
embed code in quoted heredoc avoid needing modify (which error-prone process nature):
ssh myserver 'bash -s' <<'eof' sed -e "1,/^$(date -d -24hour +'%y-%m-%d %h')/d" logfile \ | grep -a20 -b10 'exception' \ | mail -s "sample report" xyz@yourdomain eof
in specific case here, obvious reason original modifications failed internal single quotes terminating ones surrounding code whole.
specifically:
+'%y-%m-%d %h'
...the first '
in expression terminates 1 opening before command, space isn't syntactically protected.
you instead following, since shell bash:
ssh myserver $'sed -e "1,/^$(date -d -24hour +\'%y-%m-%d %h\')/d" logfile | grep -a20 -b10 exception | mail -s "sample report" xyz@yourdomain'
$''
extension within backslashes can escape single quotes (and used other literals -- \n
newline, \t
tab, etc), functionality not available typical posix sh syntax.
by contrast, in posix sh, 1 can switch quoting types embed literal single-quote: 'foo'"'"'bar'
defines string both foo
, bar
single-quoted, , separated single literal single-quote between them -- valid syntax, not easy readers unfamiliar idiom process).
Comments
Post a Comment