Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
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.
#!/bin/bash #dt=$(date +"%Y%m%d") #HHMM=$(date '+%H:%M') key="keys/cash_prod_key" if ssh -o StrictHostKeyChecking=no -qni $key user@host "cat /var/log/FILE_send.log | grep FILENAME | grep -i success"\; then echo "Success" fi
I don't think this is the most correct way to do it.
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.
ssh
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.
cat
grep
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.
-F
-q
success
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.
General Tech 10 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 9 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.