0

I am having difficulty on the command below. Something is not right with the awk command. I tried putting backslash but still no good. Anyone can pin point where is my syntax gone wrong?

for i in $(cat db_hostlist2.txt | grep -v '^#'); do
  echo "Checking for $i";
  ssh admin@$i '
    . ./.bashrc; echo "Hostname : `hostname`";
    echo -e "Total DB is : `ps -ef | grep smon | grep -v grep | wc -l`\n";
    echo -e "Total DB is : \n`ps -ef | grep smon | grep -v grep |awk -F_ '{print $3}'`\n"
  ';
done

Output:

Hostname : TEST123
Total DB is : 3

awk: cmd. line:1: {print
awk: cmd. line:1:       ^ unexpected newline or end of string
Total DB is :
1
  • awk's ' interferes with ssh's ' try escaping those with \ – YoMismo Sep 1 '15 at 8:38
1
for HOST in $(grep -v '^#' db_hostlist2.txt)
do
        echo "Checking for $HOST"
        ssh admin@$HOST <<EOF
echo "Hostname : " \$(hostname)
echo -e "Total DB is : \$(ps -ef | grep smon | grep -v grep | wc -l)\n"
echo -e "Total DB is : \n\$(ps -ef | grep smon | grep -v grep |awk -F_ '{print \$3}')\n"
EOF
        echo "$HOST Complete"
done

Everything between the two EOF's gets treated normally and executed remotely, no need for escaping special characters, except for when using $(cmd) to execute a remote command. You can put local variables in there if you'd like.

2
  • it sounds to mee that $(cmd) within here document will be executed locally. – Archemar Sep 1 '15 at 14:17
  • You're right, I modified the answer to reflect that. Thank you – Zachary Brady Sep 1 '15 at 15:27
0

this seems over complicated.

my proposal :

for i in $(cat db_hostlist2.txt | grep -v '^#')
do 
     echo "Checking for $i"
     ssh admin@$i '. ./.bashrc;   
          echo "Hostname : "hostname ;
          echo -e "Total DB is : " ;
          ps -ef | grep -c [s]mon ;
          echo " DB are : " \
          ps -ef | grep [s]mon  |awk -F_ \'{print $3}\' ' 
 done

where

  • thou shalt not be worry over new line inside single quote. (newline inside ' are OK)
  • grep smon | grep -v grep is replace by grep [s]mon which doesn't grep itself.
  • ps -ef | grep smon | grep -v grep | wc -l is replace by ps -ef | grep -c [s]mon which return count of items.

  • echo -e "texta command\n" is replaced by echo text ; a command (why use -e and add \n ? also most command produce a newline).

4
  • Doesn't work. You can't use \' to quote a single quote inside single quotes (you can use '\''). Your code is also missing some backticks, maybe because you used the version of the question broken by an edit instead of the original. – Gilles 'SO- stop being evil' Sep 1 '15 at 22:11
  • well, but at least, can I save grep [s]mon instead of grep smon| grep -v grep ? – Archemar Sep 2 '15 at 4:50
  • Just make it pgrep smon or pidof smon if it's non-embedded Linux. – Gilles 'SO- stop being evil' Sep 2 '15 at 8:43
  • Just make it ps -C smon and do not invent complexities where they are not needed :) – galaxy Sep 4 '15 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.