F
F
fokin_nikolay19892021-10-29 07:32:56
linux
fokin_nikolay1989, 2021-10-29 07:32:56

How to write everything in one line and output everything to a file?

There is a bash code:

#/bin/sh
inetADD=$1
#GATEWAY=
#DNS1=
NETMASK=255.255.255.128
ip address | grep -v lo | cut -d ' ' -f2 | tr ':' '\n' | awk NF
echo "Enter interface:"
read nameINT
uuid=$($nameINT)
echo -e "NAME=$nameINT\nDEVICE=$nameINT\nBOOTPROTO=static\nUUID=$uuid\nONBOOT=yes\nIPADDR=$1\nNETMASK=$NETMASK" >> /etc/sysconfig/network-scripts/ifcfg-$nameINT
ifdown $nameINT && sleep 3 && ifup $nameINT
if [ "`ping -c 1 -I $nameINT 192.168.1.1`" ] then
  echo "Ping GOOD. System EXIT"
exit
else
  echo "Reloading the interface"
ifdown $nameINT && sleep 10 && ifup $nameINT
ping -c 1 -I $nameINT 192.168.1.1
echo "ERROR"
fi

How can all this content be placed in a file, as text through echoor printwell in a format like this:
echo -e "#/bin/bash\n.....и так далее из файла" >> /home/script.sh

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Karabanov, 2021-10-29
@karabanov

Use Ansible and Jinja2 Templates

S
Saboteur, 2021-10-29
@saboteur_kiev

escape all dollars and do cat via DocumentHere:

cat <<EOF >newfile.sh
...
EOF

Here is a complete example, you can put this text into a script, execute it or paste it entirely from the buffer, or type it by hand, and it will make you a new file.sh
cat <<EOF>file.sh
#/bin/sh
inetADD=\$1
#GATEWAY=
#DNS1=
NETMASK=255.255.255.128
ip address | grep -v lo | cut -d ' ' -f2 | tr ':' '\n' | awk NF
echo "Enter interface:"
read nameINT
uuid=\$(\$nameINT)
echo -e "NAME=\$nameINT\nDEVICE=\$nameINT\nBOOTPROTO=static\nUUID=\$uuid\nONBOOT=yes\nIPADDR=\$1\nNETMASK=\$NETMASK" >> /etc/sysconfig/network-scripts/ifcfg-\$nameINT
ifdown \$nameINT && sleep 3 && ifup \$nameINT
if [ "\$(ping -c 1 -I \$nameINT 192.168.1.1)" ] then
  echo "Ping GOOD. System EXIT"
exit
else
  echo "Reloading the interface"
ifdown \$nameINT && sleep 10 && ifup \$nameINT
ping -c 1 -I \$nameINT 192.168.1.1
echo "ERROR"
fi
EOF

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question