2

I am trying to collect dump of Java process with jstack and I have tried below command. But it's saying ambiguous redirect as I can't redirect to a command.

 jstack -l `pgrep java` > /tmp/TD_`hostname`_`date`_`pgrep java`
-bash: /tmp/TD_`hostname`_`date`_`pgrep java`: ambiguous redirect

Is there any work-around I can achieve taking threaddump in a single line command with date and Java PID attached to threaddump file name?

1 Answer 1

4

Your command substitution output probably has spaces, leading to field splitting, etc. Use quotes:

$ echo foo > /tmp/TD_`hostname`_`date`_`pgrep java`
bash: /tmp/TD_`hostname`_`date`_`pgrep java`: ambiguous redirect
$ echo foo > /tmp/"TD_`hostname`_`date`_`pgrep java`"
$ ls /tmp/TD*
/tmp/TD_muru-arch_Sat 26 Dec 16:14:29 IST 2015_
6
  • you are awesome , how did I miss that ?
    – Raja G
    Dec 26, 2015 at 10:53
  • 1
    it might be worth noting that bash's $IFS splitting for the <> redirection target word is nonstandard and wouldn't occur in a POSIX shell.
    – mikeserv
    Dec 26, 2015 at 23:04
  • 1
    @mikeserv, POSIX allows that behaviour for interactive shells. When non-interactive, bash doesn't do it when called as sh. Dec 26, 2015 at 23:09
  • @StéphaneChazelas - it does do it in the script though as bash. quoting redirects annoys me, but i do it for bash's sake.
    – mikeserv
    Dec 26, 2015 at 23:15
  • @mikeserv, agreed. Same here. You could do it as well for ksh and yash sake though, as code sourced from interactive ksh or yash would also need the quoting (for the glob part). Dec 26, 2015 at 23:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .