Linux Command Results in a bash Variable - bash

I am trying to write a Ping result bash code.
this way it work, but i'm not a fan of using the last ouput function:
#!/bin/bash
ping -q -c2 10.10.50.120 > /dev/null
resp=$?
echo "$resp"
if [ "$resp" == 0 ]
then
echo "ok"
else
echo "not ok"
fi
Ouput:
0
ok
but this way it doesn't work:
#!/bin/bash
resp=$(ping -q -c2 10.10.50.120 > /dev/null)
echo "$resp"
if [ "$resp" == 0 ]
then
echo "ok"
else
echo "not ok"
fi
Ouput:
not ok
Can anyone help me to find out how to write it correctly?

I wanted to avoid the "$?" function.
That's great. So just use if.
if output=$(ping -q -c2 10.10.50.120); then
# or like: if ping -q -c2 10.10.50.120 >/dev/null; then
echo "ok"
else
echo "not ok"
fi
echo "Anyway, ping command ouptutted: $output"
It is possible to save the exit status to a variable without the "$?" function?
No.

Related

Run command in script and check return value

I am trying to run the diff command on two folders, check the return value and then output a message.
I have this:
#!/bin/bash
result='diff dir1 dir2'
if result == 0
then
echo "OK"
else
echo "ERROR"
fi
But I am getting result: command not found
How should I execute the command and return the value, to be compared in the IF?
Quite some problems here. This is an example, but you should do more research in the future.
#!/bin/bash
result="$(diff dir1 dir2)"
ret=$?
if [[ $ret -eq 0 ]]; then
then
echo "OK"
else
echo "ERROR"
fi
exit $ret
The simplest way is to check it directly:
#!/bin/bash
if diff dir1 dir2; then
echo "OK"
else
echo "ERROR"
fi
If you don't want diff to print anything, use the -q flag.
please use ${result} instead.
Also, note the backticks on the actual command
#!/bin/bash
result=`diff dir1 dir2`
if [[ -z ${result} ]]
then
echo "OK"
else
echo "ERROR"
fi

shell script handle an error from a command being excuted from echo

Hello I am new to shell script and I need to handle the error coming from a command being excuted inside echo like the following
echo -e "some internal command that I can't share \nq" | nc localhost 10000
I want to say
if [[ there's no error ]]
try
echo "YOUR SUPERSECRET COMMAND" | nc localhost 10000 | grep "Your expected error"
if [[ $? -eq 0 ]]; then
echo "Do something useful with error"
else
echo "Success"
fi
grep return 0 on matching and returns 1 when it doesn't find matching string.
The shell variable $? will give you the exit code. So, you could do:
echo -e "some internal command that I can't share \nq" | nc localhost 10000
rc=$?
if [[ $rc == 0 ]]; then
echo "success"
fi
Or simply,
if echo -e "some internal command that I can't share \nq" | nc localhost 10000; then
echo "success"
fi
Here is a concise way of doing it:
internalcommand &>/dev/null 2>&1 && echo OK || echo FAILED
If internalcommand succeeds OK will be printed to stdout, if it fails FAILED is printed.
Note tested on Bash v4

Bash : Check if PID exists with further logic

Question: How do you check if a PID exists and use the result within an IF statement in bash?
Things I've tried
if [ "$(ps -p $pid)" -eq 0 ]; then
echo "Running"
else
echo "Not Running"
fi
if [ "$(kill -0 $pid)" -eq 0 ]; then
echo "Running"
else
echo "Not Running"
fi
Neither of these evaluate correctly no matter how I redirect STDOUT/STDER
How do you check if a PID exists and use the result within an if statement?
You can capture the output in a variable and then check the exit status:
output=$(ps -p "$pid")
if [ "$?" -eq 0 ]; then
echo "Found"
echo "$output"
fi
Just remember that $? is getting reset every time you run a command, so something like the following wont work:
output=$(ps -p "$pid")
echo "$output"
# Now $? will be refering to the exit status of echo
if [ "$?" -eq 0 ]; then
echo "Found"
fi
One can also stick everything together in the if statement:
if output=$(ps -p "$pid")
then
echo "Found: $output"
fi
Make it dynamic by passing the pid you want to check:
#!/usr/local/bin/bash
if ps -p $1 > /dev/null;
then
echo "running"
else
echo "not running"
fi
Example runs:
What's your host OS?
If you have /proc then this may work for you:
if [ -d "/proc/$pid" ]; then
echo "Running"
else
echo "Not running"
fi

How to check if the file in the url exists or not

I'm trying to check if a file in the url exists or not using wget, but the result it is giving is not as expected.
It is giving the result as Spider mode enabled. Check if remote file exists.
The command I'm using is:
cd $path
if [ $? -eq 0 ]; then
touch laber.txt
if [ $? -eq 0 ]; then
echo $key >> laber.txt
if [ $? -eq 0 ]; then
wget -S --spider $url/laber.txt 2>&1 | grep -q 'HTTP/1.0 200 OK'
if [ $? -eq 0 ]; then
echo OK;
else
rm -r laber.txt
echo FAIL;
fi
else
echo 'test';
fi
else
echo '3';
fi
else
echo '4';
fi
Simple way to do it. Idea is to grep on the header output from wget on the requested URL and get the return code of the operation as such.
grep with -q will operate in silent mode i.e. will not output search string into stdout in case it is found.
A fancy one-liner could do the trick
#!/bin/bash
wget -S --spider $1 2>&1 | grep -q 'HTTP/1.0 200 OK' && echo SUCCESS || echo FAIL
Confirmation:-
$ ./script.sh www.go3463tgogle.com
FAIL
$ ./script.sh www.google.com
SUCCESS
Traditional way:-
#!/bin/bash
wget -S --spider $1 2>&1 | grep -q 'HTTP/1.0 200 OK'
if [ $? -eq 0 ]; then
echo OK
else
echo FAIL
fi
Confirmation:-
$ ./script.sh www.go3463tgogle.com
FAIL
$ ./script.sh www.google.com
OK
You can use any actual url with the script to adopt for your case.

search a string in Shell script variable

I have a shell script variable
$a = "Hello i am pass"
now i want to search for "pass" in variable $a.
if ["$a" == "pass"]; then
echo "`Success`"
else
echo "`fail`"
fi
Please give me shell script for searching pass keyword to use in above code.
Try with this,
#!/bin/bash
a="Hello i am pass";
if [ `echo $a | grep -c "pass" ` -gt 0 ]
then
echo "Success"
else
echo "Fail";
fi
flag=`echo $a|awk '{print match($0,"pass")}'`;
if [ $flag -gt 0 ];then
echo "Success";
else
echo "fail";
fi
Here is a more compact way to write this:
echo "$a" | grep "pass" && echo "Found." || echo "Not found."
You can use braces to put in multiple instructions instead of a simple echo:
echo "$a" | grep "pass" && {echo "Found.";exit 0} || {echo "Not found.";exit 1}
today i used extended choice parameter and using execute shell for the string match.
if someone will find same problem anyone can use this
if [ `echo $test| grep -c "abc" ` -gt 0 ]
then
echo "Success"
else
echo "Fail";
fi
Try this
a="Hello I am Pass";
a1="Hello, Passed my First Attempt"
a2="Passed in First Attempt"
if [[ ${a,,} =~ 'pass' ]]; then echo 'Success'; else echo 'First Attempt in Learning'; fi
Short command:
OPC="test"
PROD="This is a test"
[[ ${PROD,,} =~ $OPC ]] && echo -n 1 || echo -n 0
The answer will be "1" when find "OPC" in "PROD".

Resources