egrep is returning 0 in bash script but executing manually the command returning correct value | unix bash - bash

I'm executing a bash script that returns me if ftp connection failed or was success using egrep, the issue is that when I'm trying to get a word with egrep is returning 0 but If I execute the command manually is returning 2.
this is my code:
#Create the FTP Connection.
for ip_address in ${IP_ADDRESS[#]}; do
ftp ${ip_address} <<ftp_commands > ${FTP_RESULTS}
user "${USER_ID}" "${USER_PASSWORD}"
pwd
bye
ftp_commands
ftp_result_id=`egrep -c "Login failed|Connection refused|Not connected|Connection timed out" ${FTP_RESULTS}`
if [ ${ftp_result_id} -gt 0 ]; then
echo "$(date +%m/%d/%y_%H:%M:%S) - ${ip_address} - Not connected" >> ${CONNECTION_RESULTS_FILE}
else
echo "$(date +%m/%d/%y_%H:%M:%S) - ${ip_address} - Connected" >> ${CONNECTION_RESULTS_FILE}
fi
done
the ftp_results_id is returning 0 in the egrep -c command, but I'm executing manually after it ran and create the file "FTP_RESULTS" and is working, it suppose that found 2 matches with "Not connected"
any suggestion?

The egrep -c command counts the matches.
Then you use a condition to do something if there are more than 0 matches.
A simpler and better solution is to use the exit code of egrep.
egrep exits with 0 (= success) if it found a match,
and non-zero otherwise.
You can write the if statement like this:
if egrep -q "Login failed|Connection refused|Not connected|Connection timed out" "${FTP_RESULTS}"; then
This is equivalent to the logic in your posted code.
There's no need for the ftp_result_id variable.
And there's no need to save the output of egrep.
I added the -q flag so that egrep doesn't produce any output.
None needed.

Related

jenkins stuck after restarting remote service agent

This is part of bigger code that checking the OS version and choosing the correct condition by it.
( after checking OS version, it will go to this if condition: )
if [ "$AK" = "$OS6" ]
then
if [ "$(ls -la /etc/init.d/discagent 2>/dev/null | wc -l)" == 1 ]
then
/etc/init.d/discagent restart 2>&1 > /dev/null
/etc/init.d/discagent status |& grep -qe 'running'
if [ $? -eq 0 ] ; then
echo " Done "
else
echo " Error "
fi
fi
fi
If im hashing service discagent restart the pipeline is passing.
But if im not hashing it then it hang and not given any errors, And on the output file it is showing only the second server (out of few) that its hang on, And not moving to the next server.
what could be the issue?
p. S
while running it direct on the server it is working.
Run this script manually on the servers, that this script will be running on.
You can use xtrace -x which would show each statement before being executed, when you use with -v with -x so this would be -xv and the statement would be outputted, and then the line after the output of the statement with the variables substituted before the code is substituted.
using -u would show you the exact error when this occurs.
Another command is the trap command to debug your bash script.
More information on the following webpage,
https://linuxconfig.org/how-to-debug-bash-scripts

How to get IP of a domain requested with wget?

I'm searching for a way to fetch IP address of the domain requested with wget command, when the command fails.
I can't use ping command for fetching the IP, because the address may change after the wget command is terminated.
I would like to perform this in a shell script.
When wget fails, it terminates with non-zero exit status, and the errors are written to the standard error descriptor (2).
So you can check the exit code ($? variable), and parse the strings written to the standard error:
url='http://stackoverflow.com/users/edit/1646322'
output=$( wget "$url" 2>&1 )
if [[ $? -ne 0 ]]; then
printf '%s' "$output" | \
perl -ne '/^Connecting to .*\|([^\|]+)\|/ and print $1'
fi
Sample Output
151.101.129.69

string comparison is shell script

I have a scenario to copy file from one server to another, for that i need to check any existing scp is in progress, have wrote a sample shell script but the condition is not being met even though syntax is correct, the main problem here is the output of ps command will gets stored in variable scpstat and the same compared for matching string in if statement, here I'm getting the output of the variable is different from executing outside of the script. can see it is formatted different in script execution when executing sh -x scpsamp.sh, why there is "sh" appended to the output, but while comparing without ps and assigning as scpstat='scp' i can able to get the condition correct, am i doing anything wrong while getting output in to the variable. please help
#!/bin/sh
scpstat=`ps -ef | grep scp | egrep -v 'grep|ssh' | awk '{print $8}')`
if [ "$scpstat" = "scp" ];
then
echo "SCP is in progress"
else
echo "No SCP in progress"
fi
sh -x output
It's notoriously difficult to extract information from the output of ps. If your system has pgrep, it's much easier:
if pgrep scp >/dev/null
then
echo "SCP is in progress"
else
echo "No SCP in progress"
fi

Checking if output of a command contains a certain string in a shell script

I'm writing a shell script, and I'm trying to check if the output of a command contains a certain string. I'm thinking I probably have to use grep, but I'm not sure how. Does anyone know?
Testing $? is an anti-pattern.
if ./somecommand | grep -q 'string'; then
echo "matched"
fi
Test the return value of grep:
./somecommand | grep 'string' &> /dev/null
if [ $? == 0 ]; then
echo "matched"
fi
which is done idiomatically like so:
if ./somecommand | grep -q 'string'; then
echo "matched"
fi
and also:
./somecommand | grep -q 'string' && echo 'matched'
Another option is to check for regular expression match on the command output.
For example:
[[ "$(./somecommand)" =~ "sub string" ]] && echo "Output includes 'sub string'"
A clean if/else conditional shell script:
if (ls | grep '$1')
then
echo "exists"
else
echo "doesn't exist"
fi
SHORT ANSWER
All the above (very excellent) answers all assume that grep can "see" the output of the command, which isn't always true:
SUCCESS can be sent to STDOUT while FAILURE to STDERR.
So depending on which direction you test, your grep can fail. That's to say that if you are testing for the case of FAILURE you must redirect the output of the command to STDOUT using 2>&1 in such a case as this.
LONGER ANSWER w/ PROOFS
I had what I thought was a very simple test in a bash script using grep and it kept failing. Much head scratching followed. Use of set -x in my script revealed that the variable was empty! So I created the following test to understand how things were breaking.
NOTE: iscsiadm is a Linux tool from the "open-iscsi" package used to connect/disconnect a host to SAN storage. The command iscsiadm -m session is used to show if any LUN connections are established):
#!/bin/bash
set -x
TEST1=$(iscsiadm -m session)
TEST2=$(iscsiadm -m session 2>&1)
echo
echo 'Print TEST1'
echo $TEST1
echo
echo 'Print TEST2'
echo $TEST2
echo
If a LUN WAS connected, BOTH variables were successfully populated with values:
Print TEST1
tcp: [25] 192.168.X.XX:3260,1 iqn.2000-01.com.synology:ipdisk.Target-LUN1 (non-flash) tcp: [26] 192.168.X.XX:3260,1 iqn.2000-01.com.synology:storagehost.Target-LUN1 (non-flash)
Print TEST2
tcp: [25] 192.168.X.XX:3260,1 iqn.2000-01.com.synology:ipdisk.Target-LUN1 (non-flash) tcp: [26] 192.168.X.XX:3260,1 iqn.2000-01.com.synology:storagehost.Target-LUN1 (non-flash)
However, if a LUN WASN'T connected, iscsiadm sent the output to STDERR, and only the "TEST2" variable was populated where we had redirected to STDOUT using 2>&1; "TEST1" variable which had no redirection to STDOUT was empty:
iscsiadm: No active sessions.
Print TEST1
Print TEST2
iscsiadm: No active sessions.
CONCLUSION
If you have a funky, half-broken- works in one direction but not the other- situation such as this, try the above test replacing iscsiadm with your own command and you should get the proper visibility to rewrite your test to work correctly.

Not able to force exit on Jenkins Build

I've been having a lot of trouble with this so here goes.
I have a Jenkins build that executes the following shell script:
#!/bin/sh -x
if [ 'grep -c "It misses" log' -gt 0 ];
then exit 1;
fi
I know that the grep returns 1 when it finds something and technically Jenkins should mark the build as failed on a non-zero exit, but the jenkins still marks it as a success.
The console output for the jenkins build when running the script is:
Started by user bla
[project_name] $ /bin/sh -x /var/tmp/hudson41276.sh
+ [ grep -c "It misses" log -gt 0 ]
Finished: SUCCESS
Could anybody give me a hand and point out what I'm missing here?
Thanks,
CJ
If I understand right, you want the job to fail if "It misses" is not found in file "log". You can do this by not using the -c option of grep, just redirect the output like this:
grep "It misses" log > /dev/null
Grep will return 0 if it finds the phrase, and the job will succeed. If it does not find the phrase, grep will return 1, and the job will fail. If you want it the other way around (fail if it does find the phrase) just use grep -v. $? is your friend when you want to be sure of the exit status of a shell command.
Try this:
#!/bin/sh
set -e
grep -c "It misses" log
set -e: Exit at the first error.
grep -c 'arg': Exit 1 if nothing was grepped.
The problem is with your script, not Jenkins. The part of your script where you attempt to compare the exit code of grep looks like this:
if [ 'grep -c "It misses" log' -gt 0 ] ...
This will not even execute grep. In fact, it is simply comparing a string to a number.
You were probably attempting to do:
if [ `grep -c "It misses" log` -gt 0 ] ...
Note the use of backticks (`). Shell will execute grep and replace the backticks with the output of grep.
Bonus item: the condition in the if statement is actually a command that gets executed and its exit code determines where the execution will continue. So... why not use the grep command and it's useful exit code as the condition? (grep will exit with code 0 when it finds matches.)
if grep "It misses" log; then
exit 1
fi
It's shorter, much more readable and ever performs better because it does not need to execute so many commands.
Such a short if statement could even be replaced with a one-liner:
grep "It misses" log && exit 1
By default jenkins start shell with -e, so it exists at first error.
You could turn it off by
set +e
do failing task..

Resources