pattern match in live stream

hi!

i have a situation like this where i have to analyse the live log generated from

/bin/scp -v you@example.com

is if a pattern like say "Too many connections" comes i shud be able to identify it .

/bin/scp -v you@example.com 2>&1 | grep "too many connections"

1 Like

hi Corona688! thanks !..i want the script to exit with status 1 when the string in the is found ....

You don't need to analyze the logfile at all to quit when scp fails. Every program has an exit status, 0 for success, anything else for some sort of error. You can tell it to quit when scp returns nonzero by:

/bin/scp -v you@example.com || exit 1

If you want to be more specific about the error conditions, scp appears to return different error codes for different errors:

SCP Return Codes
0	Operation was successful
1	General error in file copy
2	Destination is not directory, but it should be
3	Maximum symlink level exceeded
4	Connecting to host failed.
5	Connection broken
6	File does not exist
7	No permission to access file.
8	General error in sftp protocol
9	File transfer protocol mismatch
10	No file matches a given criteria
65	Host not allowed to connect
66	General error in ssh protocol
67	Key exchange failed
68	Reserved
69	MAC error
70	Compression error
71	Service not available
72	Protocol version not supported
73	Host key not verifiable
74	Connection failed
75	Disconnected by application
76	Too many connections
77	Authentication cancelled by user
78	No more authentication methods available
79	Invalid user name 

So:

/bin/scp -v you@example.com
# There must be no statements between this assignment and the scp call!
SCPSTATUS="$?"

if [ "$SCPSTATUS" -ne 0 ]
then
        echo "SCP quit with status $SCPSTATUS"
        exit 1
fi