Below I try to control that the input is good an IP :
#!/bin/bash
cp /home/scripts/choice_interfaces.txt /home/scripts/interfaces.txt
chmod 644 /home/scripts/interfaces.txt
echo -e "Please enter the network informations into the /etc/network/interfaces file, complete them below :\n"
sed -n '1,10p' choice_interfaces.txt
printf "address " ; read -r address
if [[ $address =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
printf "network " ; read -r network
printf "netmask " ; read -r netmask
printf "broadcast " ; read -r broadcast
printf "gateway " ; read -r gateway
else
echo "$address doesn't correct"
fi
sed -i '12s/$/\n address '$address'/' interfaces.txt
sed -i '13s/$/\n network '$network'/' interfaces.txt
sed -i '14s/$/\n netmask '$netmask'/' interfaces.txt
sed -i '15s/$/\n broadcast '$broadcast'/' interfaces.txt
sed -i '16s/$/\n gateway '$gateway'/' interfaces.txt
mv /home/scripts/interfaces.txt /etc/network/interfaces
I try a loop.. :
printf "address " ; read -r address
while [[ $address =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; do
printf "address " ; read -r address
done
I wanted that the user be invited to type until his input is good
printf "network " ; read -r network
printf "netmask " ; read -r netmask
printf "broadcast " ; read -r broadcast
printf "gateway " ; read -r gateway
I do an example that I will wish, because I row.. :
#!/bin/bash
cp /home/scripts/choice_interfaces.txt /home/scripts/interfaces.txt
chmod 644 /home/scripts/interfaces.txt
echo -e "Please enter the network informations into the /etc/network/interfaces file, complete them below :\n"
sed -n '1,10p' choice_interfaces.txt
while[[ $address $network $netmask $broadcast $gateway =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[-z $address $network $netmask $broadcast $gateway]]; do (I found it on google to test if the input is empty but Its not working)
printf "address " ; read -r address
printf "network " ; read -r network
printf "netmask " ; read -r netmask
printf "broadcast " ; read -r broadcast
printf "gateway " ; read -r gateway
done
sed -i '12s/$/\n address '$address'/' interfaces.txt
sed -i '13s/$/\n network '$network'/' interfaces.txt
sed -i '14s/$/\n netmask '$netmask'/' interfaces.txt
sed -i '15s/$/\n broadcast '$broadcast'/' interfaces.txt
sed -i '16s/$/\n gateway '$gateway'/' interfaces.txt
#mv /home/scripts/interfaces.txt /etc/network/interfaces1
Thanks in advance I hope I was enough clear :rolleyes:
You can have more than one line in a while/until condition.
The following uses a code block between until...do plus another explicit code block within {...}
Hi, thanks to you both for the solutions I make a script for a basic installation of a servers, here the beginning, if anyone is interested and if you have some critics or advices don't hesitate, it will be beneficial to me :
1st step, I have created a file with a template from an interface file, named choice_interfaces.txt :
#--------------------------------------------------------------
# The loopback network interface
#--------------------------------------------------------------
auto lo
iface lo inet loopback
#--------------------------------------------------------------
# Eth0
#--------------------------------------------------------------
auto eth0
iface eth0 inet static
2nd step, I have created another file with my script :
#!/bin/bash
#!/bin/bash
fetchip() {
printf "%s: " "$1"
until
read
[[ "$REPLY" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] &&
{
IFS=. read ip1 ip2 ip3 ip4 <<< "$REPLY"
[[ $ip1 -le 255 && $ip2 -le 255 && $ip3 -le 255 && $ip4 -le 255 ]]
}
do
printf "Invalid $1 IP address - Please re-enter\n%s: " "$1"
done
}
cp /home/scripts/choice_interfaces.txt /home/scripts/interfaces.txt
chmod 644 /home/scripts/interfaces.txt
echo -e "Please enter the network informations into the /etc/network/interfaces file, complete them below :\n"
sed -n '1,10p' choice_interfaces.txt
fetchip address
fetchip network
fetchip netmask
fetchip broadcast
fetchip gateway
sed -i '12s/$/\n address '$address'/' interfaces.txt
sed -i '13s/$/\n network '$network'/' interfaces.txt
sed -i '14s/$/\n netmask '$netmask'/' interfaces.txt
sed -i '15s/$/\n broadcast '$broadcast'/' interfaces.txt
sed -i '16s/$/\n gateway '$gateway'/' interfaces.txt
#mv /home/scripts/interfaces.txt /etc/network/interfaces