0

I have a file /tmp/hostlist containing hosts 1000+ . I am trying to fetch line which has particular word from crontab -l on 1000+ hosts via pssh command to expedite the task, but unable to fetch the line

I am trying below command. The reason for using echo \$(echo ) is to display the output in same line when triggering more than one OS command

pssh -h /tmp/hostlist -i "echo \$(echo ), \$(crontab -l|grep root)"

The above command works fine when i replaced "crontab -l|grep root" with any OS Command , Having an issue with crontab -l -> It doesn't display the expected output.

Just to add-

The above command works when using only one Command as below.

pssh -h /tmp/hostlist -i 'crontab -l|grep -i root'  --> Works fine

Is there any possibility to use more than one command and display output in one line with crontab -l command like below.

pssh -h /tmp/hostlist -i "echo \$(echo ), \$(crontab -l|grep root),  \$(uptime)"   --> doesn't work or doesn't give expected output of crontab -l|grep root

and when using below without crontab command works fine.

pssh -h /tmp/hostlist -i "echo \$(echo ), \$(uname -a),  \$(uptime)" --> Works fine
Jack15
  • 19
  • 4
  • Output of `crontab -l` depends on which user account you're currently logged in as. Compare the account `pssh` is authenticating to to the one with the crontab whose contents you want to list. – Charles Duffy Apr 08 '19 at 15:08
  • BTW, why are you using double quotes and thus forcing yourself to add escapes, vs just single quotes, as in `pssh 'echo $(crontab -l | grep root), $(uptime)'`? – Charles Duffy Apr 08 '19 at 15:09
  • BTW, notice that you're using `grep -i root` when you say it works, and `grep root` without the `-i` when you say it doesn't? Either the difference is pertinent and you should have the `-i` everywhere, or you don't actually want/need case insensitivity and you shouldn't have it at all. – Charles Duffy Apr 08 '19 at 15:12
  • As an aside, generally speaking, it's helpful towards getting reproducible behavior to be aggressive with your redirections. `crontab -l &1 | grep -i root` avoids your stdin or stderr FDs from having an impact on output. – Charles Duffy Apr 08 '19 at 15:13
  • @Charles Duffy , I am the root user and executing the mentioned cmd as root user.Do we still require to specify user root ?. Executing with double quotes "echo \$(echo ), \$(crontab -l|grep root), \$(uptime)" only works and displays the output. Single quotes doesn't display output. – Jack15 Apr 08 '19 at 15:24
  • Nonsense. On UNIX systems (Windows behaves quite differently under-the-hood), `pssh` *can't tell the difference* between `foo "\$(bar)"` and `foo '$(bar)'`, because they turn into the exact same string in the `argv` array passed to its `main`. I can point you to no end of Q&A entries by people asking if they can detect the quoting used to invoke their command and being -- correctly -- told that it's impossible. – Charles Duffy Apr 08 '19 at 15:28
  • Run `set -x` to enable trace-level logging before your commands, and the shell will print a representation of the strings as they're passed to the underlying program. That representation will show `"\$(foo)"` changed to `'$(foo)'`, because the latter is the terser, simpler representation of the exact same value. – Charles Duffy Apr 08 '19 at 15:30
  • (of course, the above assumes that the code given in the question is run in isolation, in a default quoting context; if everything in the question is, say, inside an unquoted heredoc, that changes things -- but providing enough context to let someone else reproduce the problem themselves is the core of building a [mcve]). – Charles Duffy Apr 08 '19 at 15:53

0 Answers0