# Mender client inventory bash inside bash question

**URL:** <https://hub.mender.io/t/mender-client-inventory-bash-inside-bash-question/1658>\
**Category:** General Discussions\
**Tags:** inventory\
**Created:** [March 19, 2020, 12:05pm UTC](https://hub.mender.io/t/mender-client-inventory-bash-inside-bash-question/1658 "2020-03-19T12:05:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![genedupre](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/genedupre/32/289_2.png) [@genedupre](https://hub.mender.io/u/genedupre)\
**Post date:** [March 19, 2020, 12:05pm UTC](https://hub.mender.io/t/mender-client-inventory-bash-inside-bash-question/1658/1 "2020-03-19T12:05:50Z")

</div>

Hello everyone,  
i’m looking into adding more attributes to the mender client inventory, to keep an eye that the necessary devices are connected.

i’m encountering that i am unable to use a bash inside a bash to generate my attributes, let me give an example

last line will ask bash to execute another script ‘keyboardconnect’

> #!/bin/bash  
> set -ue  
> LC\_ALL=C  
> export LC\_ALL  
> grep ‘model name’ /proc/cpuinfo | uniq | awk -F’: ’ ’  
> // { printf(“cpu\_model=%s\n”, $2);}  
> ’  
> echo “kernel=$(cat /proc/version)”
> 
> cat /proc/meminfo | awk ’  
> /MemTotal/ {printf(“mem\_total\_kB=%d\n”, $2)}  
> ’  
> echo “hostname=$(cat /etc/hostname)”
> 
> echo “keyboard-connected=$(./keyboardconnect)”

content keyboardconnect

> #!/bin/bash  
> if [[$(lsusb | grep 03f0:034a)]]; then  
> echo “connected”  
> else  
> echo “not connected”  
> fi

this generates:

> root@localhost:/usr/share/mender/inventory# ./mender-inventory-hostinfo  
> cpu\_model=ARMv7 Processor rev 3 (v7l)  
> kernel=Linux version 4.19.75-v7l+ (dom@buildbot) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g84  
> 60611)) #1270 SMP Tue Sep 24 18:51:41 BST 2019  
> mem\_total\_kB=3999784  
> hostname=localhost  
> keyboard-connected=connected

yet the attribute on mender stays empty  
 ![empty](https://canada1.discourse-cdn.com/flex036/uploads/mender/original/1X/e17c009fa92e3f06556f6a508c8187393466e5d0.png)

but when keeping the keyboard script locally

> #!/bin/bash  
> set -ue  
> keyboard1=“error”  
> if [[$(lsusb|grep 03f0:034a)]]; then  
> keyboard1=“connected”  
> else  
> keyboard1=“not connected”  
> fi
> 
> LC\_ALL=C  
> export LC\_ALL  
> grep ‘model name’ /proc/cpuinfo | uniq | awk -F’: ’ ’  
> // { printf(“cpu\_model=%s\n”, $2);}  
> ’  
> echo “kernel=$(cat /proc/version)”
> 
> cat /proc/meminfo | awk ’  
> /MemTotal/ {printf(“mem\_total\_kB=%d\n”, $2)}  
> ’  
> echo “hostname=$(cat /etc/hostname)”
> 
> echo “keyboard-connected=${keyboard1}”

is giving the same output in terminal, but this one does work, could someone explain why this is the case?

Greets and thanks, Gene

---

<div class="post-metadata">

**Author:** ![drewmoseley](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/drewmoseley/32/47_2.png) [@drewmoseley](https://hub.mender.io/u/drewmoseley)\
**Post date:** [March 19, 2020, 1:33pm UTC](https://hub.mender.io/t/mender-client-inventory-bash-inside-bash-question/1658/2 "2020-03-19T13:33:47Z")

</div>

Hi Gene,

I’m not certain but I have 2 suggestions.

1. Your “cat /etc/hostname” bit is missing a “$”. It may work like this but I’m not sure what the side effects are.
2. Just on a hunch, can you change the key value to be “keyboard\_connected”? I’m not sure if I’ve ever tried an inventory key with a ‘-’ in the name.

Drew

---

<div class="post-metadata">

**Author:** ![genedupre](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/genedupre/32/289_2.png) [@genedupre](https://hub.mender.io/u/genedupre)\
**Post date:** [March 19, 2020, 11:56pm UTC](https://hub.mender.io/t/mender-client-inventory-bash-inside-bash-question/1658/3 "2020-03-19T23:56:39Z")

</div>

hello @drewmoseley thank you for the reply

i tried the “-” and this worked without a problem.

my scripts by this time changed quite a with more succes, but i still could use you advice on this.

mender-inventory-cardreader which is a simple grep on lsusb of an device ID

```
#!/bin/bash
if [[$(lsusb | grep 03f0:034a)]]; then
	echo "cardreader=connected"
else
	echo "cardreader=not-connected"
fi

```

and this works fine, now i made another script where, only the ID of the device has been detected once, it wil give it status in the future (to prevent of giving an inventory of devices not meant for that mender client)

mender-inventory-printer ↓

```
#!/bin/bash
source mender-inventory-variables
echo "mender-inventory-variables=$active_printerM2"
#to test if importing variable works

if [["$active_printer" = "0"]];then
	if [[$(lsusb | grep 04b8:0e15)]]; then
	new_active_printer="1"
	sed -r -i.bak "s/active_printer=([[:graph:]]+)/active_printer=$new_active_printer/" mender-inventory-variables
#set variable active_keyboard to 1 in mender-inventory-variables
	source mender-inventory-variables
#reload the variables from mender-inventory-variables
	fi
fi

if [["$active_printerM2" = "1"]]; then	
	if [[$(lsusb | grep 04b8:0e15)]]; then
		echo "Printer=connected"
	else
		echo "Printer=not_connected"
	fi
fi

```

when running this script manually it gives an normal output  
(mender-inventory-variables=1 is just to test the IF)  
but this script does not work/ give feedback to mender inventory, and is actually the original problem.

> root@localhost:/usr/share/mender/inventory# ./mender-inventory-printer  
> mender-inventory-variables=1  
> Printer=connected

both scripts give the ‘attribute=value’ as described in the [doc’s](https://docs.mender.io/2.3/client-configuration/inventory)

Thanks for the help, Gene

---

<div class="post-metadata">

**Author:** ![drewmoseley](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/drewmoseley/32/47_2.png) [@drewmoseley](https://hub.mender.io/u/drewmoseley)\
**Post date:** [March 20, 2020, 6:41pm UTC](https://hub.mender.io/t/mender-client-inventory-bash-inside-bash-question/1658/4 "2020-03-20T18:41:34Z")

</div>

I can’t answer why it works when you run it manually but I think the culprit is the line:

```auto
	if [[$(lsusb | grep 04b8:0e15)]]; then

```

That seems like it should be

```auto
    if lsusb | grep -q 04b8:0e15; then

```

or

```auto
   if [[-z "$(lsusb | grep 04b8:0e15)"]]; then

```

Drew

---

<div class="post-metadata">

**Author:** ![madisox](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/madisox/32/446_2.png) [@madisox](https://hub.mender.io/u/madisox)\
**Post date:** [March 20, 2020, 8:00pm UTC](https://hub.mender.io/t/mender-client-inventory-bash-inside-bash-question/1658/5 "2020-03-20T20:00:13Z")

</div>

The problem is using of relative path names to invoke the scripts from within the script. Try using the full path name for the script instead. For example:

```
echo “keyboard-connected=$(./keyboardconnect)”

```

should be

```
echo “keyboard-connected=$(/full/path/to/location/of/keyboardconnect)”
```

---

<div class="post-metadata">

**Author:** ![genedupre](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/genedupre/32/289_2.png) [@genedupre](https://hub.mender.io/u/genedupre)\
**Post date:** [March 23, 2020, 7:56pm UTC](https://hub.mender.io/t/mender-client-inventory-bash-inside-bash-question/1658/6 "2020-03-23T19:56:48Z")

</div>

hello @madisox @drewmoseley,

it seemed that the use of ‘source’ the use of AND (&&) operant made the right ‘output’ terminal wise, but not for mender. the workaround i ended doing is

```
if [[$(lsusb | grep 04b8:0e28 && grep active_printer_model3=1 mender-inventory-variables)]]; then	
	echo "Printer=connected"
else
	echo "Printer=not_connected"
fi

if [[$(lsusb | grep 04b8:0e28 && grep active_printer_model3=0 mender-inventory-variables)]]; then	
	active_printer_model3="1"
	sed -r -i.bak "s/active_printer_model3=([[:graph:]]+)/active_printer_model3=$new_active_printer_model3/" /usr/share/mender/inventory/mender-inventory-variables
fi

```

the use off full path (to change a variable) was also a solution  
Thanks Gene
