0

Im doing a zenity form for easy network configuration. The end user just fills in the information and the script does the rest.
Is there a better way to do this?
Any input from you guys is welcome.
Thanks

sudo sed -i.bak '7,8 d' /etc/network/interfaces
sudo sed -i '/inet netmask/d' /etc/network/interfaces
sudo sed -i '/inet nameservers/d' /etc/network/interfaces
sudo sed -i '/inet address/d' /etc/network/interfaces
sudo sed -i '/inet gateway/d' /etc/network/interfaces
sudo sed -i "8a iface eth0 inet static" /etc/network/interfaces
ip=$(zenity --entry --title="Ip adress" --text="Ip adress:")
sudo sed -i "9a address $ip" /etc/network/interfaces
mask=$(zenity --entry --title="Netmask" --text="Netmask address:")
sudo sed -i "11a netmask $mask" /etc/network/interfaces
gateway=$(zenity --entry --title="Gateway address" --text="Gateway address:")
sudo sed -i "10a gateway $gateway" /etc/network/interfaces
dns=$(zenity --entry --title="Dns" --text="Dns address:")
sudo sed -i "12a dns-nameservers $dns" /etc/network/interfaces
sudo sed -i "13a iface eth1 inet static" /etc/network/interfaces
sudo sed -i "14a address $ip" /etc/network/interfaces
sudo sed -i "15a gateway $gateway" /etc/network/interfaces
sudo sed -i "16a netmask $mask" /etc/network/interfaces
sudo sed -i "17a dns-nameservers $dns" /etc/network/interfaces
zenity --question --text "Is the information accurate"; echo $?
 if [ $? > 0 ];
    then
        echo "--- do it again ---"
        sudo cp home/Test/interfaces /etc/network/ #this copies a interfaces.bak 
                                                    #over the changed one
        sleep 3s
        exit
    fi

Now i changed to this:

cp /etc/network/interfaces /tmp/interfaces
sed -i -e '7,8 d' \
       -e '/inet address/d '\
       -e '/inet netmask/d' \
       -e '/inet nameservers/d' \
       -e '/inet gateway/d' \
       -e '/iface eth0 inet static/d' \
       -e '/iface eth1 inet static/d' /tmp/interfaces
sudo sed -i "8a iface eth0 inet static" /tmp/interfaces
ip=$(zenity --entry --title="Ip adress" --text="Ip adress:")
sudo sed -i "9a address $ip" /tmp/interfaces
mask=$(zenity --entry --title="Netmask" --text="Netmask address:")
sudo sed -i "10a netmask $mask" /tmp/interfaces
gateway=$(zenity --entry --title="Gateway address" --text="Gateway address:")
sudo sed -i "11a gateway $gateway"/tmp/interfaces
dns=$(zenity --entry --title="Dns" --text="Dns address:")
sudo sed -i "12a dns-nameservers $dns" /tmp/interfaces
sudo sed -i "13a iface eth1 inet static" /tmp/interfaces
sudo sed -i "14a address $ip" /tmp/interfaces
sudo sed -i "15a gateway $gateway" /tmp/interfaces
sudo sed -i "16a netmask $mask" /tmp/interfaces
sudo sed -i "17a dns-nameservers $dns" /tmp/interfaces
wait 1s;
sudo zenity --text-info --height=500 --width=400 < <(cat /tmp/interfaces)
if ! zenity --question --text "Är alla addresser rätt ifyllda";  then
    cp /tmp/interfaces /etc/network/interfaces 
    exit
    fi

And nothing happens... no changes to the file whatsoever.. help? :D

  • First of all, you don't use sudo in a script, run the script itself as root if you need it. Are you creating a bash script here? – dessert Oct 11 '17 at 7:10
  • use if ! zenity --question --text "Is the information accurate" ; then … – dessert Oct 11 '17 at 7:20
  • I recommend shellcheck.net. – dessert Oct 11 '17 at 7:21
1
  1. Run the script with sudo. Don't prefix every single command with sudo. Alternatively, make a copy of /etc/network/interfaces as non-root and work on that and then use sudo to copy it back into place.

  2. sed ...; sed ...; sed ... may be replaced by sed -e '...' -e '...' -e '...'.

  3. Testing on $? is not needed.

So what you get is

cp /etc/network/interfaces /tmp/interfaces
sed -i -e '7,8 d' \
       -e '/inet netmask/d' \
       -e '/inet nameservers/d \
       (etc.)
       -e '8a iface eth0 inet static' /tmp/interfaces
(etc.)

if ! zenity --question --text "Is the information accurate"; then
    ...
fi
  • check the main question please.. i have an issue .. and i cant really explain it :) – hibridpc Oct 11 '17 at 8:50
  • @hibridpc I was making a guess on the if test. You may have to remove the !. Also, you still have way too many sed invocations. If you set ip, gateway and mask at the beginning, you should be ok with just one sed. – Kusalananda Oct 11 '17 at 8:55
  • why do i get "file not found" when i write this command in shell?sudo sed -i '7a iface eth0 inet static' /tmp/interfaces \; – hibridpc Oct 11 '17 at 9:38
  • sudo sed -i '7a iface eth0 inet static' /tmp/interfaces \; – hibridpc Oct 11 '17 at 9:40
  • @hibridpc You are trying to run sed on a file called ;. – Kusalananda Oct 11 '17 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.