assign output of memcache command to a variable in shell/bash script - bash

I have a bash script with this code
echo -e 'get mykey\r' | nc localhost 11211
when I run the script I get this output on the terminal :
VALUE mykey 0 1
0
END
But instead of printing it on the terminal I want to assign the output of the command 'get mykey\r' | nc localhost 11211 to a variable in my bash script.
Also when I use echo -e it prints VALUE mykey 0 1 and END which I don't want in my variable.
So the expected output is that the variable should contain only the value of the corresponding key i.e in this case the variable should contain the value 0 (can be anything depending on the key which is being get) only.
What I tried :
output = 'get mykey\r' | nc localhost 11211
echo $output
but this gives output: command not found error
How do I do it?

You could just do:
output=$(echo -e 'get mykey\r' | nc localhost 11211 | awk 'NR==2')
echo "$output"
but check the man page for nc to see if it has any options to control what it outputs.

Related

Bash printf value does not show up or is cut off

I trying to get a value from a command into a var
and then print it our using printf.
Problem: i got the value in the var but with printf it does not appear
or is cut off.
INFO: In my script im calling redis-cli info memory
and to check whats wrong i tried a call on vmstat -s.
Working vmstat test:
format="%-16s | %-16s"
container_name="some_name"
used_memory=$(vmstat -s | sed -n "s/^\(.*\) K used memory.*$/\1/p")
row=$(printf "${format}" "${container_name}" "${used_memory}")
echo "${row}"
Output: some_name | 11841548
The actual script that is not working:
format="%-50s | %-16s"
container_name="totally_secret_container_name_1"
used_memory=$(docker exec -it "${container_name}" redis-cli info memory | sed -n "s/^used_memory_human:\(.*\)$/\1/p")
row=$(printf "${format}" "${container_name}" "${used_memory}")
echo "${row}"
Output: ecret_container_name_1 | 1.08M
Weird is than when i set the format to format="%-50s | %-1s"
then it works - the container name (left value) gets printed correctly.
What happen here?
How can i fix this?
Thanks for your time!
You need to remove the \r characters in the output that are causing it to go back to the beginning of the line and overwrite.
used_memory=$(docker exec -it "${container_name}" redis-cli info memory | sed -n "s/^used_memory_human:\(.*\)$/\1/p")
used_memory=${used_memory//$'\r'/}
row=$(printf "${format}" "${container_name}" "${used_memory}")
This uses the bash ${variable//old/new} replacement operator.

BASH IF statement false negative

I am having some problems comparing a variable from a script to the IP address of the system. I have a source file for an app which is not bash fiendly but it has a value set like so
DKUS_MASTER=127.0.0.1
I am fetching that variable in my bash file by doing
DKUSMASTER=`grep "DKUS_MASTER" /root/somestuff.conf | sed 's/DKUS_MASTER=//'`
Here is what i am then doing in my script as i am trying to see if eth0 ip is set to the DKUS_MASTER parameter i am fetching
DKUSMASTER=`grep "DKUS_MASTER" /root/somestuff.conf| sed 's/DKUS_MASTER=//'`
ETH0=$(ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
if [ "$ETH0" = "$DKUSMASTER" ]; then
DKUS_STATUS="Master"
else
DKUS_STATUS="Slave"
fi
doing some testing, i can see the variables look correct
echo $DKUSMASTER
echo $ETH0
however, the end status is else false and never true as seen here. In my case the variable $DKUSMASTER does indeed equal to $ETH0, so the status should come back as master.
echo $DKUS_STATUS

Bash script sshd group check

hello I have script written in python. This script is saving results output of bash script modules. I have to written module that check that are user in
cat /etc/group |grep sshd and write in excel
in group 203. When i execute command command in host i recveive output
staff:!:,sshd,sosnix,xo29321,siwja8211,293912,29314
sshd:!:203:sshd
In this module "compilant" and "actual_value" this is row's from excel
My code of module
module_id="XX.TEST"
echo " === $module_id module === "
command_output=`cat /etc/group |grep sshd`
if [ "$command_output" = "cat /etc/group |grep sshd" ]; then
compliant="Yes"
actual_value="GUID 203"
else
compliant="No"
actual_value="N/A"
fi
# SCRIPT RESULT
echo :::$module_id:::$compliant:::$actual_value:::
echo " === End of $module_id module === "
and this script write in my excel result No In compilant and N/A in actual value
This is correct behavior.
You compare two strings: first string with result of cat /etc/group |grep sshd execution and second string the command itself - "cat /etc/group |grep sshd".
These strings are not equivalent. So 'If' goes by 'else' branch and you got the mentioned output.
Please refer for https://www.gnu.org/software/bash/manual/bash.html "6.4 Bash Conditional Expressions" for more information.

Pass conky object to external script

I have a conky object, and I want to pass it as parameter of a bash / lua script. How do I do it?
Example:
conky object: ${tcp_portmon 1 61000 lport 0}
i.e the port of the first tcp connection
script: $ lsof -i :<PORT> | sed -n 2p | awk '{print $1}'
i.e finds the process with that port
What I tried (unsuccessfully):
${exec lsof -i :${tcp_portmon 1 61000 lport 0} | sed -n 2p | awk '{print $1}'}
${exec echo $(lsof -i :${tcp_portmon 1 61000 lport 0} | sed -n 2p | awk '{print $1}')}
${lua conky_proc_port ${tcp_portmon 1 61000 lport 0}}, where conky_proc_port simply outputs the parameter
How do I do it?
PS: for reference, here is the link to the Github issue
The best way to pass the conky object to a lua function is... by not passing the object.
Instead, use the lua API function conky_parse within the lua function to evaluate the string '${tcp_portmon 1 61000 lport 0}' and then process the result as desired.
Alternativly, rather than hardcoding the string, you could pass the string '${tcp_portmon 1 61000 lport 0}' to the lua function and then use conky_parse on the passed string.
See the "LUA API" section of man conky for more info.

shell script pattern matching

Need help on shell script.
I have following result in a file
name-label ( RW) : host1
networks (MRO): 1/ip: 192.168.1.2; 0/ip: 192.168.1.10
name-label ( RW) : host2
networks (MRO): 1/ip: 192.168.1.15; 1/ipv6/0: fe80::9060:b6ff:fec1:7bbb; 0/ip: 192.168.1.20; 0/ipv6/0: fe80::286d:7cff:fefe:3ed7
I want only the hostname and corresponding 0/ip value from file. Final output will be
host1 192.168.1.10
host2 192.168.1.20
Perl solution:
perl -ne '/^name-label .*: (.+)/ and $name = $1; m(0/ip: ([0-9.]+)) and print "$name $1\n"'
Name-label is stored in a variable, it's printed with the IP when processing the next line.
#!/bin/bash
grep ") :" FILENAME | sed 's/.*) ://' | sed 's/networks.*ip://' | sed 's/;.*//'
FILENAME is your file.

Resources