Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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 :
share|improve this question
    
awk's ' interferes with ssh's ' try escaping those with \ –  YoMismo Sep 1 at 8:38

2 Answers 2

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.

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

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).

share|improve this answer
    
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 Sep 1 at 22:11
    
well, but at least, can I save grep [s]mon instead of grep smon| grep -v grep ? –  Archemar Sep 2 at 4:50
    
Just make it pgrep smon or pidof smon if it's non-embedded Linux. –  Gilles Sep 2 at 8:43
    
Just make it ps -C smon and do not invent complexities where they are not needed :) –  galaxy yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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