I
I
itgraine2020-05-23 22:38:22
linux
itgraine, 2020-05-23 22:38:22

Write bash command value to variable?

Hello!
I'm trying to write the ip address of the network adapter into a variable and then insert the resulting ip address into a file, here's the script itself

#!/bin/bash
PATH=/etc/3proxy:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
echo -e "AT^RESET\r\n" >/dev/ttyUSB2
killall 3proxy
sleep 20
echo -en 'AT^NDISDUP=1,1,"internet.tele2.ru"\r\n' > /dev/ttyUSB2
#IP=`ip addr show wwp0s6u2i1 | grep -oP `inet \K\S[0-9.]+``
ip=$(ip addr show wwp0s6u2i1 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
echo "" > /etc/3proxy/3proxy.cfg
cat > /etc/3proxy/3proxy.cfg << END
monitor /etc/3proxy/3proxy.cfg
daemon
timeouts 1 5 30 60 180 1800 15 60
maxconn 5000
nscache 65535
log /dev/null
flush
proxy -n -a -p8000 -i192.168.1.55 -e$ip
END
/usr/bin/3proxy /etc/3proxy/3proxy.cfg


If I paste the command into the console, then it returns the ip address of the adapter, i.e. the command itself works

ip addr show wwp0s6u2i1 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1


But if I run the script, then the variable in which the result is written is empty for some reason.
I tried to create a variable with a text string, the value of the variable is transferred, but that's exactly the value of this command is not transferred.

In theory, the output file should look like this

monitor /etc/3proxy/3proxy.cfg
daemon
timeouts 1 5 30 60 180 1800 15 60
maxconn 5000
nscache 65535
log /dev/null
flush
proxy -n -a -p8000 -i192.168.1.55 -e(Тут должен быть ip из переменной)


Please tell me what could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2020-05-23
@jcmvbkbc

echo -en 'AT^NDISDUP=1,1,"internet.tele2.ru"\r\n' > /dev/ttyUSB2

Is this command to establish a connection? And by the time the command is called, ip=$(ip addr show wwp0s6u2i1 ...it already has time to be installed and the interface receives an ip-address?

A
Alexey, 2020-05-24
@AlexeyKolodchenko

Wrap your variable in quotes; -e"$ip"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question