Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A QuizKindly log in to use this feature. We’ll take you to the login page automatically.
LoginGeneral Tech Bugs & Fixes 3 years ago
User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
ssue was an incorrect host name.
This answer is instead focusing on how to write the code in a better way.
You don't need to run the complete pipeline on the remote host. Running complicated commands on the command line with ssh is rather error prone, not least from a quoting point of view.
Instead:
#!/bin/bash
key="keys/cash_prod_key"
ssh_args=( -o StrictHostKeyChecking=no -qni "$key" user@host )
if ssh "${ssh_args[@]}" cat /var/log/FILE_send.log |
grep -F 'FILENAME' |
grep -q -i -F 'success'
then
echo 'Success'
fi
This would run only the cat on the remote host and then run the two grep commands locally.
I've also added the -F flag to the grep commands, assuming that the strings used as patterns are not regular expressions but literal strings that you'd like to search for. The last grep uses -q as we're not actually interested in seeing the output, just in whether it contains the string success or not.
To make it easier to read, I've put the arguments used with ssh into an array that I later use in the call, and I've also made the lines a bit shorter by simply inserting newlines after the pipe symbols. The script is still syntactically correct. Also note that any expansion of variable etc. should always be double quoted, unless you know in what contexts this is not needed.
Note that I'm assuming that the ssh command is invoked in the correct way.
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
manpreet
Best Answer
3 years ago
I'm trying to grep a string in the log file in a remote host. but I'm having trouble to return the echo to the host where I'm running the script.
I don't think this is the most correct way to do it.