i'm just starting out with bash & am trying to write a script to search specific files in a server remotely based on: (a)device name and (b) string. my goal is to get all output containing 'string' for the device specified. when i tried the script below just hangs. however, when i run the command directly on the server("grep -i "router1" /var/log/router.log | grep -i "UPDOWN"), it works. any ideas?any ideas?
#!/bin/bash # read -p "Enter username: " user read -p "Enter device name: " dev read -p "Enter string: " str while read /home/user1/syslogs do ssh "$user"@server1234 'grep -i "$dev" /var/log/"$syslogs" 2> /dev/null | grep -i "$str"' done
You seem to be mis-using the
read command. You don't specify the file to read from as an argument;
read always reads from standard input. It's not clear what you want to do with the value you read from the file as a result, but you want something like this:
read -p "Enter username: " user read -p "Enter device name: " dev read -p "Enter string: " str while read fileName; do # Also: I'm borrowing sputnick's solution to the nested quote problem. ssh [email protected] <<EOF grep -i "$dev" /var/log/$fileName 2>/dev/null | grep -i "$str" EOF done < /home/user1/syslogs