monapi
Posts: 45
Joined: Tue Aug 07, 2018 7:53 am

writing script with using sudo nano inside it

Tue Aug 28, 2018 8:05 am

hello, i'm trying to write a shell script for turning raspi to an access point. is there someone who know how can i use "sudo nano" command to make a new file or edit a file in side a shell script and write and put some data into it and save and close it and go no another command in the script?
thanks.

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: writing script with using sudo nano inside it

Tue Aug 28, 2018 8:14 am

How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: writing script with using sudo nano inside it

Tue Aug 28, 2018 8:37 am

+1, sed is designed for the purpose.

Also, you can use the shell for simple things:-

To make a new file (easiest way):

Code: Select all

cat <<EOF >myfile
text line 1
text line 2
text line 3
EOF
To create a single line file:

Code: Select all

echo "line of text" >myfile
To add a line to the end of a file called "myfile":

Code: Select all

echo "Extra line of text" >>myfile
The same applies to "shar" files:

Code: Select all

cat <<EOF >>myfile 
text line 1
text line 2
text line 3
EOF
appends a block of text to file.

monapi
Posts: 45
Joined: Tue Aug 07, 2018 7:53 am

Re: writing script with using sudo nano inside it

Tue Aug 28, 2018 9:46 am

how can i write
To configure the static IP address, edit the dhcpcd configuration file with:

sudo nano /etc/dhcpcd.conf
Go to the end of the file and edit it so that it looks like the following:

interface wlan0
static ip_address=192.168.4.1/24
nohook wpa_supplicant
Now restart the dhcpcd daemon and set up the new wlan0 configuration:

sudo service dhcpcd restart
in the shell script?
i try writing
echo "interface wlan0
static ip_address=192.168.5.5/24
nohook wpa_supplicant" >> /etc/dhcpcd.conf
sudo service dhcpcd restart
but i see
/etc/dhcpcd.conf: Permission denied
error. how should i use sed to solve it?

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: writing script with using sudo nano inside it

Tue Aug 28, 2018 10:20 am

Prefix the command with sudo.
This system config file requires admin permission to alter it.

Also although your echo command does work for a multiple line file in bash, the more common way is the "shar" method.

Code: Select all

sudo cat <<EOF >>/etc/dhcpcd.conf
interface wlan0
static ip_address=192.168.5.5/24
nohook wpa_supplicant
EOF

monapi
Posts: 45
Joined: Tue Aug 07, 2018 7:53 am

Re: writing script with using sudo nano inside it

Tue Aug 28, 2018 11:05 am

jahboater wrote:
Tue Aug 28, 2018 10:20 am
Prefix the command with sudo.
This system config file requires admin permission to alter it.

Also although your echo command does work for a multiple line file in bash, the more common way is the "shar" method.

Code: Select all

sudo cat <<EOF >>/etc/dhcpcd.conf
interface wlan0
static ip_address=192.168.5.5/24
nohook wpa_supplicant
EOF
thank you
i use code but it give the same error and add sudo for my code and it didn't help.
what is wrong?

User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: writing script with using sudo nano inside it

Tue Aug 28, 2018 11:43 am

jahboater wrote:
Tue Aug 28, 2018 10:20 am
Prefix the command with sudo.
This system config file requires admin permission to alter it.

Also although your echo command does work for a multiple line file in bash, the more common way is the "shar" method.

Code: Select all

sudo cat <<EOF >>/etc/dhcpcd.conf
interface wlan0
static ip_address=192.168.5.5/24
nohook wpa_supplicant
EOF
Running cat with sudo like that won't solve the permission problem, the file redirection is handled by the shell and isn't affected by sudo. You need to tell sudo to run a shell which runs the command.

Code: Select all

sudo /bin/sh -c "cat <<EOF >>/etc/dhcpcd.conf
interface wlan0
static ip_address=192.168.5.5/24
nohook wpa_supplicant
EOF"
She who travels light — forgot something.

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: writing script with using sudo nano inside it

Tue Aug 28, 2018 1:38 pm

Paeryn wrote:
Tue Aug 28, 2018 11:43 am
Running cat with sudo like that won't solve the permission problem, the file redirection is handled by the shell and isn't affected by sudo. You need to tell sudo to run a shell which runs the command.
Yes, thats what I thought - but strangely it does work.
Seems user Pi is in the same group (netdev), and group write is enabled.

Code: Select all

pi@Pi3:~ $ id
uid=1000(pi) gid=1000(pi) groups=1000(pi),4(adm),20(dialout),24(cdrom),27(sudo),29(audio),44(video),46(plugdev),60(games),100(users),101(input),108(netdev),997(gpio),998(i2c),999(spi)
pi@Pi3:~ $ ls -l /etc/dhcpcd.conf
-rw-rw-r-- 1 root netdev 1842 Aug 28 14:30 /etc/dhcpcd.conf
pi@Pi3:~ $ 
There is no permission problem for user pi.

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: writing script with using sudo nano inside it

Tue Aug 28, 2018 2:50 pm

jahboater wrote:
Tue Aug 28, 2018 1:38 pm
Yes, thats what I thought - but strangely it does work.
Seems user Pi is in the same group (netdev), and group write is enabled.
In that case sudo isn't needed at all.
Paeryn wrote:
Tue Aug 28, 2018 11:43 am
Running cat with sudo like that won't solve the permission problem, the file redirection is handled by the shell and isn't affected by sudo. You need to tell sudo to run a shell which runs the command.
Alternatively use tee

Code: Select all

sudo tee -a /etc/dhcpcd.conf <<EOF
interface wlan0
static ip_address=192.168.5.5/24
nohook wpa_supplicant
EOF
(you may also want a > /dev/null in there otherwise tee will output what it's written to the file as well as writing it)

monapi
Posts: 45
Joined: Tue Aug 07, 2018 7:53 am

Re: writing script with using sudo nano inside it

Wed Sep 05, 2018 1:41 pm

hello,
how can i write an script for thes lines?
these are for making acess point from a raspberry pi and really hard to write.
i don't know how to add lines to an specific part of a file or uncomment a line with script.
Find the line with #DAEMON_CONF, and replace it with this:

Code: Select all

DAEMON_CONF="/etc/hostapd/hostapd.conf"
and
Edit /etc/sysctl.conf and uncomment this line:

Code: Select all

net.ipv4.ip_forward=1
and
Add a masquerade for outbound traffic on eth0:

Code: Select all

sudo iptables -t nat -A  POSTROUTING -o eth0 -j MASQUERADE
Save the iptables rule.
and
Edit /etc/rc.local and add this just above "exit 0" to install these rules on boot.

Code: Select all

iptables-restore < /etc/iptables.ipv4.nat
thanks for helping me.

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: writing script with using sudo nano inside it

Wed Sep 05, 2018 1:51 pm

see sed
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

Ernst
Posts: 1334
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: writing script with using sudo nano inside it

Wed Sep 05, 2018 1:53 pm

RaTTuS wrote:
Wed Sep 05, 2018 1:51 pm
see sed
you beat me :(
The road to insanity is paved with static ip addresses

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: writing script with using sudo nano inside it

Wed Sep 05, 2018 2:00 pm

Ernst wrote:
Wed Sep 05, 2018 1:53 pm
RaTTuS wrote:
Wed Sep 05, 2018 1:51 pm
see sed
you beat me :(


Trouble is, that was the advice given in the first two replies to this thread. And that was over a week ago...

In the absence of an offer to write the entire solution, perhaps a little example would be useful? (I've never been fluent in sed, but I know it has been part of the Unix programming environment since forever. Deliberate reference there to a book which contains relevant documentation... )

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: writing script with using sudo nano inside it

Wed Sep 05, 2018 2:04 pm

well - rather than do exaples check out
https://linuxconfig.org/learning-linux-commands-sed
it will help in the future
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

Ernst
Posts: 1334
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: writing script with using sudo nano inside it

Wed Sep 05, 2018 2:06 pm

B.Goode wrote:
Wed Sep 05, 2018 2:00 pm
In the absence of an offer to write the entire solution, perhaps a little example would be useful?
As an alternative a google search could help the op:
https://www.google.de/search?q=site%3Aw ... e&ie=UTF-8
The road to insanity is paved with static ip addresses

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: writing script with using sudo nano inside it

Wed Sep 05, 2018 2:39 pm

perhaps a little example would be useful? (I've never been fluent in sed, but I know it has been part of the Unix programming environment since forever. Deliberate reference there to a book which contains relevant documentation... )


Sorry. Obviously way too obscure.

To be clear. This book, The Unix Programming Environment - Brian w. Kernighan Rob Pike , has a section of about 6 pages devoted to sed.

It can be read/downloaded here, amongst other places - https://www.scribd.com/doc/78859016/Uni ... n-Rob-Pike

Return to “Beginners”