I need to generate a script from within a script but am having problems because some of the commands going into the new script are being interpreted rather than written to the new file. For example i want to create a file called start.sh in it I want to set a variable to the current IP address:
echo "localip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')" > /start.sh
what gets written to the file is:
localip=192.168.1.78
But what i wanted was the following text in the new file:
localip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')"
so that the IP is determined when the generated script is run.
What am i doing wrong ?
grep | tail | awk | cutsequence means you're doing it wrong.awkcan do the work of grep, tail and cut, and much more efficiently.ip -o addrinstead of bareip addr-- that way you get everything on one line.ip -o addroutput with built-in regex support, thus not needing awk, tail, cut or grep at all.statein myip addrresults at all; sure you aren't conflating it withip link?