ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
but its not working in script with ssh
its gives below error:
script.sh: line 5: syntax error near unexpected token `('
Script is:
#!/bin/ksh
echo -e "Script to check and list if keyword is being used by any server \n" > /tmp/test.txt
for f in `cat /root/sync_script/serverlist1.txt`
do
ssh $f.exam5.com 'w=$(grep -irH "keyword.com" /home/*/public_html/*);if [ $? -eq 0 ];then echo -e "\n $HOSTNAME";echo $(grep -irH "keyword.com" /home/*/public_html/* | cut -d: -f1 | uniq | awk '{for (i=1;i<=NF;i++) printf "%s \n",$i}');fi' >> /tmp/test.txt
done
Please respond ASAP.
Thanks & Regards,
Monika
Last edited by Monika32011; 04-23-2011 at 04:11 AM.
Reason: security
Have already tried that, it will execute script on same server but wont do ssh :-(
Have also tried putting back tick (`) instead of (') and without anything as well.. gives same result, executes on same server.
I think it is getting confused between 2 pair of quotes ('), but is required at both places for ssh and awk.
It gives lot of error if I remove quotes from awk command.
unfortunately I cannot help whith your question. But I would recommend to use the "report" button (at one of your posts) and ask a Moderator to move this thread to the "Programming" part of LQ http://www.linuxquestions.org/questions/programming-9/. I think you will find more help there.
Looking at the script I do wonder if this isn't too complicated for what you have in mind.
I do believe you want to check, on different hosts, if a certain string is present in multiple files and if this is the case you want to print the host name and the file name(s).
If my above assumption is correct, try the following:
Code:
#!/bin/ksh
echo -e "Script to check and list if keyword is being used by any server \n" > /tmp/test.txt
for HOST in `cat /root/sync_script/serverlist1.txt`
do
ssh $HOST.exam5.com ' OUTPUT=$(grep -irH "keyword.com" /home/*/public_html/*) ; if [ $? -eq 0 ] ; then echo -e "\n $HOSTNAME" ; echo $OUTPUT | cut -d: -f1 ; fi' >> /tmp/test.txt
done
This is based on assumptions, I don't know what the generated input and output is in your case and which info you want to see. This also means the above solution is untested.
you sohuld escape the inner pair of single quotes, like this:
#!/bin/ksh
echo -e "Script to check and list if keyword is being used by any server \n" > /tmp/test.txt
for f in `cat /root/sync_script/serverlist1.txt`
do
ssh $f.exam5.com 'w=$(grep -irH "keyword.com" /home/*/public_html/*);if [ $? -eq 0 ];then echo -e "\n $HOSTNAME";echo $(grep -irH "keyword.com" /home/*/public_html/* | cut -d: -f1 | uniq | awk \'{for (i=1;i<=NF;i++) printf "%s \n",$i}\');fi' >> /tmp/test.txt
done
Last edited by fbianconi; 04-23-2011 at 07:45 AM.
Reason: emphasis
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves. As per kind request.
Most likely the problem is - as already noticed - the opening single quote is actually closed by the opening single quote of the awk command. To prevent this behaviour you need to let the shell close it, then pass a literal single quote embedded in double quotes. Do the same for the closing single quote of the awk command and the trick is done:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.