(SOLVED) Need help entering a cp command into another script

Forum rules
Before you post please read how to get help
User avatar
Larry78723
Level 6
Level 6
Posts: 1397
Joined: Wed Jan 09, 2019 7:01 pm
Location: Jasper County, SC, USA

(SOLVED) Need help entering a cp command into another script

Post by Larry78723 » Sat Oct 12, 2019 5:47 pm

I have this script which creates another bash script. I'm trying to add a section which will add code in the other script to copy *.pref files to /etc/apt/preferences.d but I'm stumped. Do I need to create a sed loop or what? Also, can I replace "x=~/Restore/Repos" in the script with x=pwd?

Code: Select all

#!/bin/bash

x=~/Restore/Repos
echo \#\!/bin/bash > $x
chmod u+x $x
(
 for APT in $( find /etc/apt/ -name \*.list )
    do sed -n -e '/^deb /{
     /ppa\.launchpad/s/\(.*\/\/[^\/]*.\)\([^ \t]*\)\(.*$\)/sudo apt-add-repository ppa:\2/p;
     /ppa\.launchpad/!s/\(deb[ \t]*\)\(.*$\)/sudo apt-add-repository \2/p;
    }' $APT
 done
) | sort | uniq | tee -a $x/restore_repos.sh


(
  cd /etc/apt/preferences.d
  cp *.pref $x
 
# Now I want to enter a command as the first line in $x/restore_repos.sh to copy *.pref into /etc/apt/preferences.d/
)
Last edited by Larry78723 on Mon Oct 14, 2019 3:46 pm, edited 1 time in total.
Image

User avatar
Larry78723
Level 6
Level 6
Posts: 1397
Joined: Wed Jan 09, 2019 7:01 pm
Location: Jasper County, SC, USA

Re: Need help entering a cp command into another script

Post by Larry78723 » Sat Oct 12, 2019 9:48 pm

I've kind of found a workaround by creating the file by reading from another file. Here's what I'm doing:

Code: Select all

#!/bin/bash

x=~/Recovery/Repos
echo \#\!/bin/bash > $x    #---gives an error
chmod u+x $x

#i="cp *.pref /etc/apt/preferences.d/"
sed -n '1w restore_repos.sh' sigs.txt    #<---reads 1st line from this file (see line above) and creates restore_repos.sh


(
 for APT in $( find /etc/apt/ -name \*.list )
    do sed -n -e '/^deb /{
     /ppa\.launchpad/s/\(.*\/\/[^\/]*.\)\([^ \t]*\)\(.*$\)/sudo apt-add-repository ppa:\2/p;
     /ppa\.launchpad/!s/\(deb[ \t]*\)\(.*$\)/sudo apt-add-repository \2/p;
    }' $APT
 done
) | sort | uniq | tee -a restore_repos.sh


(
 for APT in $( find /etc/apt/ -name \*.list )
    do sed -n -e '/^deb /{
     /ppa\.launchpad/s/\(.*\/\/[^\/]*.\)\([^ \t]*\)\(.*$\)/sudo apt-add-repository ppa:\2/p;
     /ppa\.launchpad/!s/\(deb[ \t]*\)\(.*$\)/sudo apt-add-repository \2/p;
    }' $APT
 done
) | sort | uniq | tee -a restore_repos.sh
Image

rene
Level 12
Level 12
Posts: 4171
Joined: Sun Mar 27, 2016 6:58 pm

Re: Need help entering a cp command into another script

Post by rene » Sun Oct 13, 2019 8:37 am

It is not entirely clear what you're aiming for given that your ~/Recovery/Repos seems to be a directory instead of a file (as in, yes, you certainly can't echo something into a directory) and what with the two equal loops in the latter, but it seems that the most direct answer to your question is:

Code: Select all

#!/bin/sh

SCRIPT=restore_repos.sh

{ echo "#!/bin/sh"
  echo "cp *.prefs /etc/apt/preferences.d/"

  for LIST in /etc/apt/sources.list /etc/apt/sources.list.d/*.list; do
	sed -n -e "/^deb[[:space:]]/{
		s/^deb[[:space:]]\+/sudo apt-add-repository /p;
		s/https\?:\/\/ppa\.launchpad\.net\/\([^[:space:]]*\)[[:space:]].*/ppa:\1/p;
	}" "$LIST"
  done | sort | uniq

} >"$SCRIPT"

chmod +x "$SCRIPT"
HOWEVER.

It's not also a good answer. Specifically, Mint broke apt-add-repository and stumbles over for example the "[arch=amd64]" syntax that many current repositories require. I.e., my /etc/apt/sources.list.d/virtualbox-bionic.list:

Code: Select all

deb [arch=amd64] https://download.virtualbox.org/virtualbox/debian bionic contrib
which the script turns into

Code: Select all

sudo apt-add-repository [arch=amd64] https://download.virtualbox.org/virtualbox/debian bionic contrib
Probably works with Ubuntu's version of apt-add-repository but not with Mint's.

Furthermore, my /etc/apt/sources.list.d/neovim-ppa-stable-bionic.list looks like

Code: Select all

deb http://ppa.launchpad.net/neovim-ppa/stable/ubuntu bionic main
deb-src http://ppa.launchpad.net/neovim-ppa/stable/ubuntu bionic main
which is transformed into

Code: Select all

sudo apt-add-repository ppa:neovim-ppa/stable/ubuntu
That is however not the correct manner in which to add the PPA; it should be sudo apt-add-repository ppa:neovim-ppa/stable. Additionally, your proposed method loses deb-src lines.

So, a better answer is: don't do that; just backup /etc/apt/sources.list, /etc/apt/sources.list.d/*.list and /etc/apt/trusted, /etc/apt/trusted.d/*.gpg, in the latter case except the ubuntu*.gpg ones and in former possibly except /etc/apt/sources.list itself.

gm10
Level 19
Level 19
Posts: 9245
Joined: Thu Jun 21, 2018 5:11 pm

Re: Need help entering a cp command into another script

Post by gm10 » Sun Oct 13, 2019 8:43 am

Larry78723 wrote:
Sat Oct 12, 2019 5:47 pm
I have this script which creates another bash script. I'm trying to add a section which will add code in the other script to copy *.pref files to /etc/apt/preferences.d but I'm stumped.
Here you create a file $x:

Code: Select all

echo \#\!/bin/bash > $x
but in the rest of your script you assume $x is a directory.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

User avatar
Larry78723
Level 6
Level 6
Posts: 1397
Joined: Wed Jan 09, 2019 7:01 pm
Location: Jasper County, SC, USA

Re: Need help entering a cp command into another script

Post by Larry78723 » Sun Oct 13, 2019 9:58 am

Thanks folks, got it fixed. Found that I didn't need to do the copy.
Image

rene
Level 12
Level 12
Posts: 4171
Joined: Sun Mar 27, 2016 6:58 pm

Re: Need help entering a cp command into another script

Post by rene » Sun Oct 13, 2019 10:03 am

Note the stuff I said for both PPA and non-PPA repositories; your restore script will be very buggy if you leave it as per the sed-transformations that you posted.

gm10
Level 19
Level 19
Posts: 9245
Joined: Thu Jun 21, 2018 5:11 pm

Re: Need help entering a cp command into another script

Post by gm10 » Sun Oct 13, 2019 10:08 am

rene wrote:
Sun Oct 13, 2019 10:03 am
Note the stuff I said for both PPA and non-PPA repositories; your restore script will be very buggy if you leave it as per the sed-transformations that you posted.
He's using my PPA which doesn't have Mint's problems. But, of course, he needs to get that added before the rest will work. :lol: But with the example you gave he'd still need to prefix with deb, I doubt that would work on Ubuntu like that, either.

Larry don't forget to mark the thread as solved btw.
Last edited by gm10 on Tue Oct 15, 2019 10:46 am, edited 2 times in total.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

rene
Level 12
Level 12
Posts: 4171
Joined: Sun Mar 27, 2016 6:58 pm

Re: Need help entering a cp command into another script

Post by rene » Sun Oct 13, 2019 10:10 am

Okay, that then undoubtedly helps with the e.g. [arch=amd64] stuff, but is that "alternative" PPA URL as mentioned for for example neovim a valid alternative? Certainly still loses deb-src...

[EDIT] Definitely not on Mint's software versions:

Code: Select all

$ sudo apt-add-repository ppa:neovim-ppa/stable/ubuntu
Cannot add PPA: ''No supported PPA of this name was found.''.

gm10
Level 19
Level 19
Posts: 9245
Joined: Thu Jun 21, 2018 5:11 pm

Re: Need help entering a cp command into another script

Post by gm10 » Sun Oct 13, 2019 10:14 am

rene wrote:
Sun Oct 13, 2019 10:10 am
Okay, that then undoubtedly helps with the e.g. [arch=amd64] stuff, but is that "alternative" PPA URL as mentioned for for example neovim a valid alternative?
You mean the one with the /stable/ubuntu? No, that's not valid anywhere. I do however support adding them via the launchpad URL, if you want alternative. ;)
rene wrote:
Sun Oct 13, 2019 10:10 am
Certainly still loses deb-src...
It won't, or rather it depends on your configuration. To quote my man page:
-s, --enable-source
Allow downloading of the source packages from the repository/PPA. Can be omitted if source code repositories are
enabled in mintsources.
It's basically a bug in vanilla Mint that the deb-src always gets added even if you disabled source code repositories. I don't have a list of the Mint bugs I fixed in my PPA but it would be rather long... And that's in addition to the Mint bugs I already fixed while still doing Mint (and some of them I introduced, creating incentive to use the PPA :lol: :twisted: ).
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

rene
Level 12
Level 12
Posts: 4171
Joined: Sun Mar 27, 2016 6:58 pm

Re: Need help entering a cp command into another script

Post by rene » Sun Oct 13, 2019 10:19 am

gm10 wrote:
Sun Oct 13, 2019 10:14 am
You mean the one with the /stable/ubuntu? No, that's not valid anywhere.
If it's just a simple matter of stripping "ubuntu" that's then clearly not much of an issue -- but is always? Probably not...

Going to repeat my advise of not doing this and trying to get it bug-for-bug compatible with the underlying toolset du jour, but just backup the .list files and keys themselves.

gm10
Level 19
Level 19
Posts: 9245
Joined: Thu Jun 21, 2018 5:11 pm

Re: Need help entering a cp command into another script

Post by gm10 » Sun Oct 13, 2019 10:22 am

rene wrote:
Sun Oct 13, 2019 10:19 am
gm10 wrote:
Sun Oct 13, 2019 10:14 am
You mean the one with the /stable/ubuntu? No, that's not valid anywhere.
If it's just a simple matter of stripping "ubuntu" that's then clearly not much of an issue -- but is always? Probably not...
Yep, it's that simple.
rene wrote:
Sun Oct 13, 2019 10:19 am
Going to repeat my advise of not doing this and trying to get it bug-for-bug compatible with the underlying toolset du jour, but just backup the .list files and keys themselves.
Well, his approach has got the advantage of working properly on the next release, too, at least for the PPAs, for they would automatically get added for the correct Ubuntu codename. And it's certainly easier for the PPA keys (unlike the official version my code adds the keys without delay or the frequent keyserver problems plaguing their code).
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

rene
Level 12
Level 12
Posts: 4171
Joined: Sun Mar 27, 2016 6:58 pm

Re: Need help entering a cp command into another script

Post by rene » Sun Oct 13, 2019 10:33 am

Well, if you say so; I have exactly two PPAs enabled to view behaviour and both those indeed end in "/ubuntu" whereas they shouldn't. Below tweaks his / the above posted regex to strip "/ubuntu" as well; the assumption, then, is, that this must always be done.

Code: Select all

#!/bin/sh

SCRIPT=restore_repos.sh
{
	echo "#!/bin/sh"
	echo "sudo cp *.prefs /etc/apt/preferences.d/"

	for LIST in /etc/apt/sources.list /etc/apt/sources.list.d/*.list; do
		sed -n -e "/^deb[[:space:]]/{
			s/^deb[[:space:]]\+/sudo apt-add-repository /p;
			s/https\?:\/\/ppa\.launchpad\.net\/\(.*\)\/ubuntu.*/ppa:\1/p;
		}" "$LIST"
	done | sort | uniq

} >"$SCRIPT"

chmod +x "$SCRIPT"
I'll leave it at that though. I wouldn't after all generally advise to replace fundamental parts of the system with external copies. Works for example in this case only just as long as you haven't grown tired of maintaining that PPA...

gm10
Level 19
Level 19
Posts: 9245
Joined: Thu Jun 21, 2018 5:11 pm

Re: Need help entering a cp command into another script

Post by gm10 » Sun Oct 13, 2019 10:43 am

rene wrote:
Sun Oct 13, 2019 10:33 am
Well, if you say so;
Of course I do, after all I wrote the code that created those entries. :P

Here's how the deb line is formed:

Code: Select all

f"deb http://ppa.launchpad.net/{ppa_owner}/{ppa_name}/ubuntu {distro_codename} main"
If you want you can also use my code to reverse it:

Code: Select all

$ cd /usr/lib/linuxmint/mintSources
$ python3 -c 'from common import ppa_utils;print(ppa_utils.parse_ppa_line("deb http://ppa.launchpad.net/neovim-ppa/stable/ubuntu bionic main"))'
ppa:neovim-ppa/stable
:lol:
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

rene
Level 12
Level 12
Posts: 4171
Joined: Sun Mar 27, 2016 6:58 pm

Re: Need help entering a cp command into another script

Post by rene » Sun Oct 13, 2019 10:47 am

I must say that running into Mint's apt-add-repository being, well, Mint's got me just a tiny bit closer again to installing KDE Neon. Basically anything Mint-specific that works I don't use, and everything I do does not, it seems...

gm10
Level 19
Level 19
Posts: 9245
Joined: Thu Jun 21, 2018 5:11 pm

Re: Need help entering a cp command into another script

Post by gm10 » Sun Oct 13, 2019 10:52 am

rene wrote:
Sun Oct 13, 2019 10:47 am
I must say that running into Mint's apt-add-repository being, well, Mint's got me just a tiny bit closer again to installing KDE Neon. Basically anything Mint-specific that works I don't use, and everything I do does not, it seems...
Well, you're welcome to join the dark side and add my PPA, then you can complain to me instead should you find a bug (with the difference that I would actually fix it). You'll note that the -s, --enable-source parameter that my version has also exists with Ubuntu's version - you surely remember the superset discussion we had in the other thread, unlike Mint I am aiming for compatibility with Ubuntu-based scripting.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

rene
Level 12
Level 12
Posts: 4171
Joined: Sun Mar 27, 2016 6:58 pm

Re: Need help entering a cp command into another script

Post by rene » Sun Oct 13, 2019 10:54 am

Slight additional tweak to make certain that a PPA with "ubuntu" in it's name does not mess up; to strip only the last ubuntu. Might be theoretical...

Code: Select all

#!/bin/sh

SCRIPT=restore_repos.sh
{
	echo "#!/bin/sh"
	echo "sudo cp *.prefs /etc/apt/preferences.d/"

	for LIST in /etc/apt/sources.list /etc/apt/sources.list.d/*.list; do
		sed -n -e "/^deb[[:space:]]/{
			s/^deb[[:space:]]\+/sudo apt-add-repository /p;
			s/https\?:\/\/ppa\.launchpad\.net\/\(.*\)\/ubuntu[[:space:]].*/ppa:\1/p;
		}" "$LIST"
	done | sort | uniq

} >"$SCRIPT"

chmod +x "$SCRIPT"

rene
Level 12
Level 12
Posts: 4171
Joined: Sun Mar 27, 2016 6:58 pm

Re: Need help entering a cp command into another script

Post by rene » Sun Oct 13, 2019 11:06 am

gm10 wrote:
Sun Oct 13, 2019 10:52 am
Well, you're welcome to join the dark side and add my PPA
In hopes of not offending you specifically, I've for a very long time now lived in the "release early, release often, then loose interest and go do something else" open-source "bazaar". Shall only ever again be "an early adopter" or "experimenter" or what have you in fields that in fact interest me. And, lordy, does apt ever not.

gm10
Level 19
Level 19
Posts: 9245
Joined: Thu Jun 21, 2018 5:11 pm

Re: Need help entering a cp command into another script

Post by gm10 » Sun Oct 13, 2019 11:19 am

rene wrote:
Sun Oct 13, 2019 11:06 am
gm10 wrote:
Sun Oct 13, 2019 10:52 am
Well, you're welcome to join the dark side and add my PPA
In hopes of not offending you specifically, I've for a very long time now lived in the "release early, release often, then loose interest and go do something else" open-source "bazaar". Shall only ever again be "an early adopter" or "experimenter" or what have you in fields that in fact interest me. And, lordy, does apt ever not.
I don't see how that could offend me. But you seemed to imply you were actually using those commands that I fixed, you even mentioned it in the context of switching distros over it, but I'll not hold you to it, I'll keep doing my thing no matter what and so should you. ;)
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

rene
Level 12
Level 12
Posts: 4171
Joined: Sun Mar 27, 2016 6:58 pm

Re: Need help entering a cp command into another script

Post by rene » Sun Oct 13, 2019 11:28 am

That's exactly right. I do use apt and it's companion tools; could hardly not use them when using Mint. The thing that I do/would not use is an external PPA for them; give me a hundred original, used-by-millions bugs over fifty customized, used-by-dozens ones any day. And yes, with that same concept in the context of Ubuntu vs. Mint, rather than Mint vs. gm10 above, that's also the reason for getting a bit fed up with Mint and its bugs itself...

gm10
Level 19
Level 19
Posts: 9245
Joined: Thu Jun 21, 2018 5:11 pm

Re: Need help entering a cp command into another script

Post by gm10 » Sun Oct 13, 2019 11:35 am

rene wrote:
Sun Oct 13, 2019 11:28 am
The thing that I do/would not use is an external PPA for them; [...] that's also the reason for getting a bit fed up with Mint and its bugs itself...
Ah, I had misunderstood. Fair point then, and still none that would offend me. I take pride in what I do so I aim to do it well, but a PPA remains a PPA, no argument, and I support being cautious about them.

Consider going back to Ubuntu, or at least ripping out Mint's tools (but what is really left of Mint without them?). For Mint's apt it's very simple, just delete /usr/local/bin/apt, for add-apt-repository you'll have to remove mintsources and install Ubuntu's software-properties-gtk instead.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

Post Reply

Return to “Scripts & Bash”