Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8609203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:49:28+00:00 2026-06-12T03:49:28+00:00

I know there are other posts on this but i cannot get any of

  • 0

I know there are other posts on this but i cannot get any of them to work. My problem is that i want to create a script on my rpi to automatically change wifi networks and change eth0 between static and dhcp. It is not yet finished i am currently only working on the wireless part. But the script so far reads…

#!/bin/bash
ANS=''
ssid=''
psk=''
file='/etc/network/interfaces'

function wireless {
    echo 'The wireless network '$ssid' has now been set up'
    start
}
function ssid {
    echo 'What is your Network SSID?'
    echo -e '> \c'
    read ssid
    echo 'You entered '$ssid'. Is this correct? (y/n)'
    echo -e '> \c'
    read ANS
    if [ $ANS = 'y' ]; then
        psk
    else
        echo 'Please renter your SSID'
        ssid
    fi
}
function psk {
    echo 'What is your Network PSK?'
    echo -e '> \c'
    read psk 
    echo 'You entered '$psk'. Is this correct? (y/n)'
    echo -e '> \c'
    read ANS
        if [ $ANS = 'y' ]; then
                wireless
        else
                echo 'Please renter your PSK'
                psk
        fi

}
function start {
echo 'What do you want to do?'
echo ''
echo 'Press w to set up wireless ssid and psk'
echo 'Press s to change eth0 to a static ip address'
echo 'Press d to change eth0 to a dhcp ip address'
echo ''
echo 'Or press ctrl+c to quit'
echo -e '> \c'
read ANS
if [ $ANS = 'w' ]; then
    ssid
else
    if [ $ANS = 's' ]; then
        static
    else
        if [ $ANS = 'd' ]; then
            dhcp
        fi
    fi
fi
}
#backup of /etc/network/interfaces
#auto lo
#iface lo inet loopback
#iface eth0 inet dhcp
#iface eth0 inet static
#   address ###########
#   netmask #############
#   broadcast ###########
#   gateway ##########
#
#allow-hotplug wlan0
#
#auto wlan0
#
#iface wlan0 inet dhcp
#wpa-ssid "Home Network"
#wpa-psk "psk"
start
exit 0

So thats all fine but in the function wireless i want to send the psk and the ssid to lines 15 and 16 of /etc/network/interfaces. Please could someone tell me the best method to do this by.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:49:30+00:00Added an answer on June 12, 2026 at 3:49 am

    You can perform automatic edits using sed like this:

     sed -i "s/^wpa-ssid.*\$/wpa-ssid \"$ssid\"/" /etc/network/interfaces
    

    This command matches any complete line beginning with wpa-ssid and replaces it with “wpa-ssid” followed by the contents of the $ssid variable. ^wpa-ssid.*\$ is the match pattern. ^ means the beginning of the line, .* means match anything, and \$ means the end of the line. wpa-ssid \"$ssid\"/ is what to replace it with.
    The -i option means to edit the file, rather than print the result to standard out.

    psk will work the same way.

    You should consider having your script back up the file the first time it is run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there are others posts with solutions on the site but i
I know there are other questions that have similar issues, but I have read
Does anyone know there have any other way that (by not using json_encode and
Or any other normal scripting language for that matter. I know there is VBScript
I want to know if there are any methods other than file.open(); in <windows.h>
I know and have Xcode, but I was wondering if there were any other
I know there are many posts about this issue however I cannot find one
I know this is a basic question, but I can't find other StackOverflow posts
I know there are so many posts about issues like this, but I can
I know there are other answers concerning this topic, and I've been through them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.