3

I am building a script running in #/bin/sh on a modem running a variant of openwrt:

root@Inteno:~# ls -l /bin/sh
lrwxrwxrwx    1 root     root             7 Oct 15  2014 /bin/sh -> busybox

The script is gathering and presenting data. I have run into a problem. I need to return a string of a computername connected, this in the fifth column:

root@Inteno:~# cat /tmp/dhcp.leases
1449374455 00:00:00:00:00:00 192.168.1.245 * 00:00:00:00:00:00
1449371955 11:11:11:11:11 192.168.1.135 android-27718d96d947c125 11:11:11:11:11

This is how it is supposed to work:

root@Inteno:~# printf '%s\n' $(grep -i 11:11:11:11:11 /tmp/dhcp.leases | awk {'printf $4'})
android-27718d96d947c125

However, if the connected computer does not have a network name set, it is listed as a *:

root@Inteno:~# printf '%s\n' $(grep -i 00:00:00:00:00:00 /tmp/dhcp.leases | awk {'printf $4'})
script2.sh
test.sh

Upon execution, the * is returned and expanded as the filenames in the running folder which leads to the script failing. This is rewritten from a nested function where the input file is a list of mac addresses, one per line:

while read enhet; do 
  printf '%-6s%-32s%-5s%-19s%-4s%-16s%-20s%-4s%-3s\n' "Navn: " $(cat /tmp/dhcp.leases | grep -i $enhet | awk {'printf $4'}) "MAC: " $(cat /tmp/dhcp.leases | grep -i $enhet | awk {'printf $2'}) "IP: " $(cat /tmp/dhcp.leases | grep -i $enhet | awk {'printf $3'}) "Signalstyrke(RSSI):" $(wlctl rssi $enhet) "dBm"; 
done < temp_assoc

What I need is to return a * character as a string, without expanding it.

Can it be done in this context? Best regards

1 Answers1

3

The result of a command substitution is subject to pathname expansion. You need to quote it to prevent characters like * being treated as wildcards.

printf '%s\n' "$(grep -i 00:00:00:00:00:00 /tmp/dhcp.leases | awk {'printf $4'})"

Also, your grep | awk is a bit of an antipattern:

awk '/00:00:00:00:00:00/ {print $4}' /tmp/dhcp.leases
chepner
  • 497,756
  • 71
  • 530
  • 681