Pass conky object to external script - bash

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.

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.

How to save to the var, only one from the output

Im writing a script that executes dig command on 2 domains, and after next cmd is host on output.
And always i will get for exmaple:
findUserServer=for r in $(dig +short $login.example.COM && dig +short $login.example.ORG); do host $r|awk '{print $NF}';done | awk -F "." '{print $1}';
1 output: >> asdf02 example
asdf02 - its a server name, its always same name starts "asdf".
Question: Have you any idea how to save to the variable only asdf02?
question+: asdf02 woudln't be always first, could be example asdf02
Should i do maybe a sed which looks on 4 first characters? If it's "asdf", then: [...]
Try not to pipe awk commands into each other and so:
for r in $(dig +short $login.example.COM && dig +short $login.example.ORG); do host $r;done | awk -F [.\ ] '/asdf02/ { print $10 }'
We use both a space and . as delimiters and then pattern match the output for the occurance of asdf02. If we find is, we print the address.
Run that through shellcheck.net ...
Try this.
findUserServer="$( for end in COM ORG; do
host $( dig +short $login.example.$end );
done | sed -n '/ asdf/{ s/^.* //; s/[.].*//; p; }' )"
This will run 2 digs and pipe the collective output through sed,
which will ignore lines that don't have asdf, and strip the matches clean for you.
Let me know if I missed details, because I don't have those exact values available.

How to append lots of variables to one variable with a simple command

I want to stick all the variables into one variable
A=('blah')
AA=('blah2')
AAA=('blah3')
AAB=('blah4')
AAC=('blah5')
#^^lets pretend theres 100 more of these ^^
#Variable composition
#after AAA, is AAB then AAC then AAD etc etc, does that 100 times
I want them all placed into this MASTER variable
#MASTER=${A}${AA}${AAA} (<-- insert AAB, AAC and 100 more variables here)
I obviously don't want to type 100 variables in this expression because there's probably an easier way to do this. Plus I'm gonna be doing more of these therefore I need it automated.
I'm relatively new to sed, awk, is there a way to append those 100 variables into the master variable?
For this specific purpose I DO NOT want an array.
You can use a simple one-liner, quite straightforward, though more expensive:
master=$(set | grep -E '^(A|AA|A[A-D][A-D])=' | sort | cut -f2- -d= | tr -d '\n')
set lists all the variables in var=name format
grep filters out the variables we need
sort puts them in the right order (probably optional since set gives a sorted output)
cut extracts the values, removing the variable names
tr removes the newlines
Let's test it.
A=1
AA=2
AAA=3
AAB=4
AAC=5
AAD=6
AAAA=99 # just to make sure we don't pick this one up
master=$(set | grep -E '^(A|AA|A[A-D][A-D])=' | sort | cut -f2- -d= | tr -d '\n')
echo "$master"
Output:
123456
With my best guess, how about:
#!/bin/bash
A=('blah')
AA=('blah2')
AAA=('blah3')
AAB=('blah4')
AAC=('blah5')
# to be continued ..
for varname in A AA A{A..D}{A..Z}; do
value=${!varname}
if [ -n "$value" ]; then
MASTER+=$value
fi
done
echo $MASTER
which yields:
blahblah2blah3blah4blah5...
Although I'm not sure whether this is what the OP wants.
echo {a..z}{a..z}{a..z} | tr ' ' '\n' | head -n 100 | tail -n 3
adt
adu
adv
tells us, that it would go from AAA to ADV to reach 100, or for ADY for 103.
echo A{A..D}{A..Z} | sed 's/ /}${/g'
AAA}${AAB}${AAC}${AAD}${AAE}${AAF}${AAG}${AAH}${AAI}${AAJ}${AAK}${AAL}${AAM}${AAN}${AAO}${AAP}${AAQ}${AAR}${AAS}${AAT}${AAU}${AAV}${AAW}${AAX}${AAY}${AAZ}${ABA}${ABB}${ABC}${ABD}${ABE}${ABF}${ABG}${ABH}${ABI}${ABJ}${ABK}${ABL}${ABM}${ABN}${ABO}${ABP}${ABQ}${ABR}${ABS}${ABT}${ABU}${ABV}${ABW}${ABX}${ABY}${ABZ}${ACA}${ACB}${ACC}${ACD}${ACE}${ACF}${ACG}${ACH}${ACI}${ACJ}${ACK}${ACL}${ACM}${ACN}${ACO}${ACP}${ACQ}${ACR}${ACS}${ACT}${ACU}${ACV}${ACW}${ACX}${ACY}${ACZ}${ADA}${ADB}${ADC}${ADD}${ADE}${ADF}${ADG}${ADH}${ADI}${ADJ}${ADK}${ADL}${ADM}${ADN}${ADO}${ADP}${ADQ}${ADR}${ADS}${ADT}${ADU}${ADV}${ADW}${ADX}${ADY}${ADZ
The final cosmetics is easily made by hand.
One-liner using a for loop:
for n in A AA A{A..D}{A..Z}; do str+="${!n}"; done; echo ${str}
Output:
blahblah2blah3blah4blah5
Say you have the input file inputfile.txt with arbitrary variable names and values:
name="Joe"
last="Doe"
A="blah"
AA="blah2
then do:
master=$(eval echo $(grep -o "^[^=]\+" inputfile.txt | sed 's/^/\$/;:a;N;$!ba;s/\n/$/g'))
This will concatenate the values of all variables in inputfile.txt into master variable. So you will have:
>echo $master
JoeDoeblahblah2

Unix user created variables

I am going though some growing pains with Unix. My question:
I want to be able to print all my user defined variables in my shell. Let say I do the following in the shell:
$ x=9
$ y="Help"
$ z=-18
$ R="My 4th variable"
How would I go about printing:
x y z R
You should record your variables first at runtime with set, then compare it later to see which variables were added. Example:
#!/bin/bash
set | grep -E '^[^[:space:]]+=' | cut -f 1 -d = | sort > /tmp/previous.txt
a=1234
b=1234
set | grep -E '^[^[:space:]]+=' | cut -f 1 -d = | sort > /tmp/now.txt
comm -13 /tmp/previous.txt /tmp/now.txt
Output:
a
b
PIPESTATUS
Notice that there are still other variables produced by the shell but is not declared by the user. You can filter them with grep -v. It depends on the shell as well.
Add: Grep and cut could simply be just one sed a well: sed -n 's/^\([^[:space:]]\+\)=.*/\1/p'
Type set:
$ set
Apple_PubSub_Socket_Render=/tmp/launch-jiNTOC/Render
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="51" [3]="1" [4]="release" [5]="x86_64-apple-darwin13")
BASH_VERSION='3.2.51(1)-release'
COCOS2DROOT=/Users/andy/Source/cocos2d
COLUMNS=80
DIRSTACK=()
...
(Oh, and BTW, you appear to have your variable syntax incorrect as you assign, say, A but print $A)
If variables are exported then you can use env command in Unix.

Why doesn't this use of sed command work?

I'm have a bash script that I use to manipulate files on a computation cluster.
The files I am trying to manipulate are of the format:
beadSize=6.25
minBoxSize=2.2
lipids=1200
chargedLipids=60
cations=0
HEAD=0
CHEAD=-2
BODY=2
TAIL=3
ION=-1
RHO_BODY=10
RHO_TAIL=14
tol=1e-10
lb=7.1
FTsize=8
ROUNDS=1000000
ftROUNDS=10
wROUNDS=1000
dt=0.01
alpha=1
transSize=0.15
transSizeZ=0.0
ionsTransSize=2.8
ionsTransSizeZ=2.8
rotateSize=0.18
volSize=8
modSize=0.0
forceFactor=2
kappaCV=0
sysSize=26
zSize=300
iVal=1
split=0
randSeed=580
I call a function inside a loop:
for per in $(seq 70 -5 5); do
for seed in {580..583}; do
for c in {"fs","fd","bfs","bfd"}; do
let count=$count+1
startJob $per $seed $c $count
done
done
done
and the lines I use to manipulate:
let n=$1*12
echo $n
cat trm.dat | sed '/memFile*/d' | sed '/rStart*/d' | sed '/test*/d'| sed 's/modSize=[0-9.]*/modSize=0.0/' | sed 's/chachargedLipids=[0-9]*/chargedLipids="$n"/' | grep char #> propFile.dat
for $per=15, for example, I expect $n==180. However when I run the script I see:
180
chargedLipids=120
What am I doing wrong?
Note
I have also tried to use:
sed "s/chachargedLipids=[0-9]*/chargedLipids=$n/"
With the same result.
chacharged != charged, and the final sed is doing nothing. With single quotes, you would expect to see the literal text chargedLipids="$n" in your output if a replacement was being made.

Resources