user567 user567 - 6 months ago 10
Bash Question

Use variable as command line input in shell script

I execute this command in my shell script.

sudo hcitool -i hci0 cmd 0x08 0x0008 1e 02 01 1a 1a ff 4c 00 02 15 e2 c5 6d b5 df fb 66 j8 hj g7 d0 f5 a7 10 96 e0 99 99 99 99 c5 00


I want to change the 4 couples of 99 to the same random value I generated,I tried this code but I gut 00. The random function is working fine.

random="$(cat /dev/urandom | base64 | head -c 2)"
echo $random
sudo hcitool -i hci0 cmd 0x08 0x0008 1e 02 01 1a 1a ff 4c 00 02 15 e2 c5 6d b5 df fb 66 j8 hj g7 d0 f5 a7 10 96 e0 $random $random $random $random c5 00

Answer

The problem is most probably that cat /dev/urandom | base64 | head -c 2 doesn't generate a hexadecimal number. Try this way instead:

random=$(cat /dev/urandom | head -c 1 | xxd -p)

And instead of using cat and head, it would be simpler and cleaner using just xxd (thanks @kojiro for the tip!):

random=$(xxd -p -l1 < /dev/urandom)
Comments