Assign command + options to a single variable?

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
User avatar
mikewalsh
Moderator
Posts: 6163
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 795 times
Been thanked: 1981 times

Assign command + options to a single variable?

Post by mikewalsh »

Morning, gang,

I'm attempting to tidy-up some of my portable-browser 'LAUNCH' scripts by using the case statement. At present, depending on the Puppy in question, various options - including whether the included 'lib' directory is required or not - are handled by long strings of "if....then....else" statements. In many cases, the required command (plus "switches) for the Chromium-based browsers is identical, and doesn't really need to be duplicated over & over again (AND again!).....but of course, we all work within the limits of our personal knowledge.

So; to the object of this query. The executable command + option "--switches" for, say, Brave-portable looks like this:-

Code: Select all

run-as-spot "$HERE/brave/brave" --user-data-dir=$HERE/PROFILE/spot/Brave-Browser --disk-cache-size=50000000 --media-cache-size=50000000 --allow-outdated-plugins --force-device-scale-factor=1.2

This same identical executable line is presently duplicated multiple times. How could I go about assigning this entire line of code to a single variable?

I'm assuming brackets are required, but.....which ones? Round ("()"); square ("[]"); curly ("{}")? Single.....or double? What would be the simplest way to achieve the desired outcome.....so that a single variable would execute that entire line of code?

(I know how to assign single statements/expressions/commands to a variable, but I'm not sure how you go about assigning not just a command but also a bunch of options all at the same time... Can it BE done? Would it then work as desired?)

Obviously, part of one's Bash 'journey' involves learning new ways of doing things as you progress.....but a few "pointers" would be appreciated! Many of the online Linux tutorial blogs don't make a lot of this stuff very clear. :D

.....or perhaps I'm jumping in at the deep end without first learning the basics.... (*shrug...*)

TIA.

Mike. ;)

User avatar
MochiMoppel
Posts: 1236
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 21 times
Been thanked: 439 times

Re: Assign command + options to a single variable?

Post by MochiMoppel »

mikewalsh wrote: Wed Jul 31, 2024 11:03 am

How could I go about assigning this entire line of code to a single variable?

I'm assuming brackets are required, but.....which ones? Round ("()"); square ("[]"); curly ("{}")? Single.....or double? What would be the simplest way to achieve the desired outcome.....so that a single variable would execute that entire line of code?

The simplest way to achieve the desired outcome is to forget your "single variable" idea. If your real goal is to avoid the repetition of the same long command then there other ways to achieve this (e.g. functions, flags, case conditions etc.). Depends on your code, so an example would help. A variable is not impossible but the use of such variable will also require the use of the eval command and other precautions. Keep it simple ;)

User avatar
Trapster
Posts: 182
Joined: Sat Aug 01, 2020 7:44 pm
Has thanked: 1 time
Been thanked: 53 times

Re: Assign command + options to a single variable?

Post by Trapster »

You could keep the "switches" in a config file and just reference that.
I'm not sure what you're trying to make efficient. If the switches are virtually the same, it seems a copy/paste is fairly simple.

ie.

Code: Select all

#!/bin/sh

file="$(cat /opt/config.file)"
echo brave $file

Result:

Code: Select all

~$ test1
brave --user-data-dir=$HERE/PROFILE/spot/Brave-Browser' '--disk-cache-size=50000000' '--media-cache-size=50000000' '--allow-outdated-plugins' '--force-device-scale-factor=1.2
~$ 
User avatar
mikewalsh
Moderator
Posts: 6163
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 795 times
Been thanked: 1981 times

Re: Assign command + options to a single variable?

Post by mikewalsh »

@MochiMoppel :-

Hallo, Mochi. Okay. Well, 'functions' are a new one for me; I've seen them used in various scripts over the years, but at those times didn't really know what they were OR what they were supposed to be doing.

Now; following this tutorial here:-

https://linuxize.com/post/bash-functions/

.....'functions' seem to be the easier option for me at this stage. (I accept what you say about 'conditions' WITHIN the 'case' statement; fair comment, but since I haven't yet even tried using 'case', I'd rather keep things as simple as I can for now. One thing at a time; I learn better when I get my head round one thing first, before trying to combine it with something else....) :)

Okay. From what I understand so far, it's the bit between the 'curly braces' that gets executed, correct? Since this IS an 'exec' command, have I got this right? I've got two examples (both of which WILL work, apparently).

We have either this:-

Code: Select all

function EXEC_LINE () {
	run-as-spot "$HERE/brave/brave" --user-data-dir=$HERE/PROFILE/spot/Brave-Browser --disk-cache-size=50000000 --media-cache-size=50000000 --allow-outdated-plugins --force-device-scale-factor=1.2 "$@"
}

...OR this (a 'simplified' version?):-

Code: Select all

EXEC_LINE () {
	run-as-spot "$HERE/brave/brave" --user-data-dir=$HERE/PROFILE/spot/Brave-Browser --disk-cache-size=50000000 --media-cache-size=50000000 --allow-outdated-plugins --force-device-scale-factor=1.2 "$@"
}

I'm thinking the first example is probably 'better'? All the times I've seen this stuff used, the word 'function' is usually there first.....but is it strictly necessary? The tutorial would seem to suggest not, but....

What do you think? Look OK, so far? Am I right in thinking that once a function has been defined, it can be used AS a 'condition' within a 'case' statement?

Mike. :?

User avatar
mikewalsh
Moderator
Posts: 6163
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 795 times
Been thanked: 1981 times

Re: Assign command + options to a single variable?

Post by mikewalsh »

@Trapster :-

Have a squizz through the attached 'LAUNCH' script. I've got several others that are similar; this is just an example.

You'll see the same identical clause repeated several times.....once for each 'if...then' condition. (The '--switches' aren't just 'virtually the same', they are the same. Identical every time, in fact.)

What I'm after is a way to re-use the same exec line, without having to write all that out every time. I'm sure there's a way to make it more compact, while achieving the same functionality. This, for me, is a step towards learning efficient use of the 'case' statement, y'see.

Mike. ;)

Attachments
LAUNCH.gz
Brave-portable 'LAUNCH' script.....remove the 'fake' .gz.
(2.21 KiB) Downloaded 32 times
User avatar
fredx181
Posts: 3074
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 374 times
Been thanked: 1309 times
Contact:

Re: Assign command + options to a single variable?

Post by fredx181 »

mikewalsh wrote: Wed Jul 31, 2024 2:49 pm

@Trapster :-

Have a squizz through the attached 'LAUNCH' script. I've got several others that are similar; this is just an example.

You'll see the same identical clause repeated several times.....once for each 'if...then' condition. (The '--switches' aren't just 'virtually the same', they are the same. Identical every time, in fact.)

What I'm after is a way to re-use the same exec line, without having to write all that out every time. I'm sure there's a way to make it more compact, while achieving the same functionality. This, for me, is a step towards learning efficient use of the 'case' statement, y'see.

Mike. ;)

Like this to replace all repeating with function name :
edited code, use export for the library set function

Code: Select all

#!/bin/sh
#
# Launcher for 'portable' Brave browser
#
HERE="$(dirname "$(readlink -f "$0")")"
#
mkdir "$HERE/PROFILE" 2> /dev/null
mkdir "$HERE/PROFILE/spot" 2> /dev/null
mkdir "$HERE/PROFILE/spot/Brave-Browser" 2> /dev/null
#
chown -R spot:spot "$HERE/brave"
chown -R spot:spot "$HERE/lib"
chown -R spot:spot "$HERE/PROFILE/spot"
#
# edited, with using 'export'
set_library_path () {
	export LD_LIBRARY_PATH=$HERE/:$HERE/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
}

run_brave () {
	run-as-spot "$HERE/brave/brave" --user-data-dir=$HERE/PROFILE/spot/Brave-Browser --disk-cache-size=50000000 --media-cache-size=50000000 --allow-outdated-plugins --force-device-scale-factor=1.2 "$@"
}

if  grep -q 'Quirky April64' /etc/DISTRO_SPECS
then
	set_library_path
	run_brave
elif grep -q 'puppy_tahr64' /etc/os-release
then
	set_library_path
	run_brave
elif grep -q 'puppy_xenialpup' /etc/os-release
then
	set_library_path
	run_brave
elif grep -q 'puppy_xenialpup64' /etc/os-release
then
	set_library_path
	run_brave
else
	run_brave
fi
#
yad --undecorated --center --text="                        Do you wish to clear the Brave cache? " \
--button="YES PLEASE - clear cache":2 \
--button="NO THANKS - don't clear cache":1 \
#
foo=$?
#
[[ $foo -eq 1 ]] && exit 0
#
if [[ $foo -eq 2 ]]; then
rm -rf $HERE/PROFILE/spot/Brave-Browser/Default/Cache/*
/usr/lib/gtkdialog/box_splash -placement top -bg "#07FE18" -fg black -timeout 5 -text "   ~~~ CACHE CLEARED! ~~~" && exit 0
fi

Also, as I see often in your setups, calling another script from the 'base' script, it can be replaced by adding a function (with contents of that 'another script') in the base script.
EDIT : about setting library path, it might be better to use 'export' (but atm I'm uncertain about it) so;
export LD_LIBRARY_PATH=$HERE/:$HERE/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
(edit: but this is besides the 'function' thingy)

User avatar
mikewalsh
Moderator
Posts: 6163
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 795 times
Been thanked: 1981 times

Re: Assign command + options to a single variable?

Post by mikewalsh »

@fredx181 :-

Fred; thank you, mate. That's EXACTLY what I wanted to know! :thumbup:

Just a couple of points. Would it be okay to combine the LD_LIBRARY_PATH stuff with the exec line, all in the same function.......or would it be technically more correct to keep them separate (as in your example)? What's your view on this?

As to your last point; yeah, that final bit came from a contribution by Marv. He's used it for a while in Ungoogled Chromium, because that one, apparently, doesn't possess the ability to clear its cache. Of course, it'll work for any Chromium-based 'clone', and I've been gradually adding it in to most of the portables; that wee YAD script just gives the user the option to clear or not when the browser shuts down.

So; that, too, could be assigned to a function....yes?

Mike. ;)

User avatar
fredx181
Posts: 3074
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 374 times
Been thanked: 1309 times
Contact:

Re: Assign command + options to a single variable?

Post by fredx181 »

mikewalsh wrote:

Would it be okay to combine the LD_LIBRARY_PATH stuff with the exec line, all in the same function.......or would it be technically more correct to keep them separate (as in your example)? What's your view on this?

Yes, but in this case, the one after 'else' is different from the others (has no library path set).
(edit: but you can make that one only as you originally had and all the others combine as you say)

EDIT:

As to your last point; yeah, that final bit came from a contribution by Marv. He's used it for a while in Ungoogled Chromium, because that one, apparently, doesn't possess the ability to clear its cache. Of course, it'll work for any Chromium-based 'clone', and I've been gradually adding it in to most of the portables; that wee YAD script just gives the user the option to clear or not when the browser shuts down.

So; that, too, could be assigned to a function....yes?

I see no repeating of that YAD command, so why in a function then ?

User avatar
mikewalsh
Moderator
Posts: 6163
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 795 times
Been thanked: 1981 times

Re: Assign command + options to a single variable?

Post by mikewalsh »

@fredx181 :-

Yup; take your point about the final exec clause. The LD_LIBRARY_PATH is there for older Pups that need the additional dependencies; the final 'catch-all' is for newer Pups that already have it all built-in.

No, the YAD script doesn't need a 'function'. You're right about that...

Mike. ;)

Burunduk
Posts: 252
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 7 times
Been thanked: 127 times

Re: Assign command + options to a single variable?

Post by Burunduk »

fredx181 wrote: Wed Jul 31, 2024 6:23 pm

I see no repeating of that YAD command, so why in a function then ?

No repeating here too:

Code: Select all

if  grep -q 'Quirky April64' /etc/DISTRO_SPECS ||
    grep -q 'puppy_tahr64' /etc/os-release ||
    grep -q 'puppy_xenialpup' /etc/os-release ||
    grep -q 'puppy_xenialpup64' /etc/os-release
then
    set_library_path
fi

run_brave

Maybe functions with descriptive names make the code more readable even if they run only once.

User avatar
MochiMoppel
Posts: 1236
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 21 times
Been thanked: 439 times

Re: Assign command + options to a single variable?

Post by MochiMoppel »

@mikewalsh Good moring. It's 6:30 and have my ☕ ready. Haven't looked at your code yet, so just to answer your questions:

mikewalsh wrote: Wed Jul 31, 2024 2:38 pm

Okay. From what I understand so far, it's the bit between the 'curly braces' that gets executed, correct?

Correct

I've got two examples (both of which WILL work, apparently).

Apparently? You've got "$@" parameters at the end of your command. Be aware that these are NOT the same parameters as you would use in your main script. Unless you "translate" the "main" parameters to new ones and pass them to the function, your function will not work.

We have either this:-

Code: Select all

function EXEC_LINE () {
	run-as-spot "$HERE/brave/brave" --user-data-dir=$HERE/PROFILE/spot/Brave-Browser --disk-cache-size=50000000 --media-cache-size=50000000 --allow-outdated-plugins --force-device-scale-factor=1.2 "$@"
}

...OR this (a 'simplified' version?):-

Code: Select all

EXEC_LINE () {
	run-as-spot "$HERE/brave/brave" --user-data-dir=$HERE/PROFILE/spot/Brave-Browser --disk-cache-size=50000000 --media-cache-size=50000000 --allow-outdated-plugins --force-device-scale-factor=1.2 "$@"
}

I'm thinking the first example is probably 'better'? All the times I've seen this stuff used, the word 'function' is usually there first.....but is it strictly necessary?

I normally use the reserved word 'function' because Geany's syntax highlighting displays it in bold, so function are easy to spot in long code. You don't need the () braces when you use 'function'. But the word 'function' is a bash thing, so if you intend to use your script in non-bash shells you must use the POSIX compliant functionname() {...} syntax

What do you think? Look OK, so far?

More or less :lol:

Am I right in thinking that once a function has been defined, it can be used AS a 'condition' within a 'case' statement?

You are probably wrong in thinking, but I don't know exactly what you mean.

Burunduk
Posts: 252
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 7 times
Been thanked: 127 times

Re: Assign command + options to a single variable?

Post by Burunduk »

MochiMoppel wrote: Wed Jul 31, 2024 10:43 pm

You've got "$@" parameters at the end of your command. Be aware that these are NOT the same parameters…

You've a point here! I was too lazy to actually test the functions (or even read the code carefully).

User avatar
mikewalsh
Moderator
Posts: 6163
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 795 times
Been thanked: 1981 times

Re: Assign command + options to a single variable?

Post by mikewalsh »

@MochiMoppel :-

Good morning, Mochi. Image

MochiMoppel wrote: Wed Jul 31, 2024 10:43 pm
mikewalsh wrote: Wed Jul 31, 2024 2:38 pm

I've got two examples (both of which WILL work, apparently).

Apparently? You've got "$@" parameters at the end of your command. Be aware that these are NOT the same parameters as you would use in your main script. Unless you "translate" the "main" parameters to new ones and pass them to the function, your function will not work.

Okay.....gotcha. No "$@" inside a function..! Just tried it with that bit commented out; starts & runs happily, so....fair enough. Despite all the stuff I've churned out over the last decade, I'll be the first to admit I'm still a raw neophyte when it comes to scripting. Most of mine are usually repetitive strings of pretty simple commands, if I'm honest.....but I'm wise enough to realise they come across as pretty "raw", too.

I like the look of @Burunduk 's code, above. Didn't know it was possible to pipe together multiple conditions within a single 'if...then' statement like that. That's neat. I've tried it; it works well....and together with what I've learnt about functions today (thanks, y'all!), it's really compacted & tidied-up this 'LAUNCH' script without detracting from functionality.

So; along with one or two other little 'tricks' I've learnt in recent months, this is what I now have ATM:-

Code: Select all

#!/bin/sh
#
# Launcher for 'portable' Brave browser...
#
HERE="$(dirname "$(readlink -f "$0")")"
#
mkdir -p "$HERE/PROFILE/spot/Brave-Browser" 2> /dev/null
#
chown -R spot:spot "$HERE/brave"
chown -R spot:spot "$HERE/lib"
chown -R spot:spot "$HERE/PROFILE/spot"
#
function set_library_path {
	export LD_LIBRARY_PATH=$HERE/:$HERE/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
}
#
function run_brave {
	run-as-spot "$HERE/brave/brave" --user-data-dir=$HERE/PROFILE/spot/Brave-Browser --disk-cache-size=50000000 --media-cache-size=50000000 --allow-outdated-plugins --force-device-scale-factor=1.2
}
#
if  grep -q 'Quirky April64' /etc/DISTRO_SPECS ||
	grep -q 'puppy_tahr64' /etc/os-release ||
	grep -q 'puppy_xenialpup' /etc/os-release ||
	grep -q 'puppy_xenialpup64' /etc/os-release
then
	set_library_path
	run_brave
else
	run_brave
fi
#
yad --undecorated --center --text="                        Do you wish to clear the Brave cache? " \
--button="YES PLEASE - clear cache":2 \
--button="NO THANKS - don't clear cache":1 \
#
foo=$?
#
[[ $foo -eq 1 ]] && exit 0
#
if [[ $foo -eq 2 ]]; then
rm -rf $HERE/PROFILE/spot/Brave-Browser/Default/Cache/*
/usr/lib/gtkdialog/box_splash -placement top -bg "#07FE18" -fg black -timeout 5 -text "   ~~~ CACHE CLEARED! ~~~" && exit 0
fi

I'm quite pleased with that! Tidier, neater.....it just LOOKS much more 'professional' than it did previously. And TBH, it's one HELL of a lot simpler to read as well. :v:

Trouble is, it now makes that final YAD script look 'scruffy'..! I've got one or two ideas about how I can tidy that up, though... :) (And before you say it, yeah...I KNOW; I use way too many 'comment' hashes. I've got into the habit of using them as 'line spacers', would you believe..? :roll: )

Further comments/suggestions are still most welcome. So:-

@MochiMoppel
@fredx181
@Burunduk

....cheers, guys! Thanks for the pointers. Appreciated.

Mike. ;)

User avatar
MochiMoppel
Posts: 1236
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 21 times
Been thanked: 439 times

Re: Assign command + options to a single variable?

Post by MochiMoppel »

mikewalsh wrote: Wed Jul 31, 2024 11:56 pm

Okay.....gotcha. No "$@" inside a function..!

That's NOT what I suggested

Just tried it with that bit commented out; starts & runs happily, so....fair enough

Runs happily? Not as happy as before, because now you can't start your script with a passed URL anymore.

User avatar
mikewalsh
Moderator
Posts: 6163
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 795 times
Been thanked: 1981 times

Re: Assign command + options to a single variable?

Post by mikewalsh »

MochiMoppel wrote: Thu Aug 01, 2024 12:07 am
mikewalsh wrote: Wed Jul 31, 2024 11:56 pm

Okay.....gotcha. No "$@" inside a function..!

That's NOT what I suggested

Just tried it with that bit commented out; starts & runs happily, so....fair enough

Runs happily? Not as happy as before, because now you can't start your script with a passed URL anymore.

Heh. O-kayyy... One step forward, two steps backward. That's me all over. :o :oops:

Would you mind very much clarifying this for me? What EXACTLY is it that this final "$@" does? I'll be honest, I've always used it mostly because everybody else seems to.....regardless of whether it's really necessary. :lol:

Mike. ;)

User avatar
MochiMoppel
Posts: 1236
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 21 times
Been thanked: 439 times

Re: Assign command + options to a single variable?

Post by MochiMoppel »

mikewalsh wrote: Thu Aug 01, 2024 12:17 am

Would you mind very much clarifying this for me? What EXACTLY is it that this final "$@" does

When you launched your old script with LAUNCH www.google.com www.bing.com , the browser (hopefully) would start with a Google and a Bing tab. You passed the URLs to your script and the script automatically keeps them in a $@ variable. In your script you can use this variable for your run-as-spot and thus pass it on to your Brave browser. If you start LAUCH without parameters, $@ remains empty and Brave starts without additional tabs.
How-To Use Command-Line Parameters ($@) in Bash

User avatar
mikewalsh
Moderator
Posts: 6163
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 795 times
Been thanked: 1981 times

Re: Assign command + options to a single variable?

Post by mikewalsh »

MochiMoppel wrote: Thu Aug 01, 2024 12:52 am
mikewalsh wrote: Thu Aug 01, 2024 12:17 am

Would you mind very much clarifying this for me? What EXACTLY is it that this final "$@" does

When you launched your old script with LAUNCH www.google.com www.bing.com , the browser (hopefully) would start with a Google and a Bing tab. You passed the URLs to your script and the script automatically keeps them in a $@ variable. In your script you can use this variable for your run-as-spot and thus pass it on to your Brave browser. If you start LAUCH without parameters, $@ remains empty and Brave starts without additional tabs.
How-To Use Command-Line Parameters ($@) in Bash

@MochiMoppel :-

A-ha. So it is in fact another variable? Makes sense.....

I see what you're saying, yes; though in my case, I tend to have a set number of tabs open all the time, and have each of the many browsers I run set to launch with whatever tabs were open when it was shut down. But the point is well-taken.....

Thanks for the explanation. I'll have a look at that link tomorrow; my jaw's cracking with yawning ATM. Time for beddy-byes, methinks...

Cheers, Mochi. G'night!

Mike. ;)

User avatar
MochiMoppel
Posts: 1236
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 21 times
Been thanked: 439 times

Re: Assign command + options to a single variable?

Post by MochiMoppel »

@mikewalsh I hope you will sleep better when I tell you that your original script needs no functions at all. Just a bit clean up.

This has a lot of repetitions:

Code: Select all

if  grep -q 'Quirky April64' /etc/DISTRO_SPECS
then
	LD_LIBRARY_PATH=$HERE/:$HERE/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
	run-as-spot "$HERE/brave/brave" --user-data-dir=$HERE/PROFILE/spot/Brave-Browser --disk-cache-size=50000000 --media-cache-size=50000000 --allow-outdated-plugins --force-device-scale-factor=1.2 "$@"
elif grep -q 'puppy_tahr64' /etc/os-release
then
	LD_LIBRARY_PATH=$HERE/:$HERE/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
	run-as-spot "$HERE/brave/brave" --user-data-dir=$HERE/PROFILE/spot/Brave-Browser --disk-cache-size=50000000 --media-cache-size=50000000 --allow-outdated-plugins --force-device-scale-factor=1.2 "$@"
elif grep -q 'puppy_xenialpup' /etc/os-release
then
	LD_LIBRARY_PATH=$HERE/:$HERE/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
	run-as-spot "$HERE/brave/brave" --user-data-dir=$HERE/PROFILE/spot/Brave-Browser --disk-cache-size=50000000 --media-cache-size=50000000 --allow-outdated-plugins --force-device-scale-factor=1.2 "$@"
elif grep -q 'puppy_xenialpup64' /etc/os-release
then
	LD_LIBRARY_PATH=$HERE/:$HERE/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
	run-as-spot "$HERE/brave/brave" --user-data-dir=$HERE/PROFILE/spot/Brave-Browser --disk-cache-size=50000000 --media-cache-size=50000000 --allow-outdated-plugins --force-device-scale-factor=1.2 "$@"
else
	run-as-spot "$HERE/brave/brave" --user-data-dir=$HERE/PROFILE/spot/Brave-Browser --disk-cache-size=50000000 --media-cache-size=50000000 --allow-outdated-plugins --force-device-scale-factor=1.2 "$@"
fi

The important point here is that IN ANY CASE the run-as-spot command is executed, so why keep it in the if command at all?
Instead try

Code: Select all

if  grep -Eq 'Quirky April64|puppy_tahr64|puppy_xenialpup|puppy_xenialpup64' /etc/os-release /etc/DISTRO_SPECS
then
	LD_LIBRARY_PATH=$HERE/:$HERE/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
fi
run-as-spot "$HERE/brave/brave" --user-data-dir=$HERE/PROFILE/spot/Brave-Browser --disk-cache-size=50000000 --media-cache-size=50000000 --allow-outdated-plugins --force-device-scale-factor=1.2 "$@"

You already turned your 3 mkdir calls into 1 by using the -p option
I *assume* that you also can turn 3 chown calls into 1 by using

Code: Select all

chown -R spot:spot "$HERE/brave" "$HERE/lib" "$HERE/PROFILE/spot"

but I could be wrong. Never used chown. Only get this idea by reading the usage notes.

[Edit] Changed the if line from
if grep -q 'Quirky April64' /etc/DISTRO_SPECS || grep -Eq 'puppy_tahr64|puppy_xenialpup|puppy_xenialpup64' /etc/os-release
to
if grep -Eq 'Quirky April64|puppy_tahr64|puppy_xenialpup|puppy_xenialpup64' /etc/os-release /etc/DISTRO_SPECS
Shorter and maybe more readable. Meaning: Let grep check the files /etc/os-release and /etc/DISTRO_SPECS. If grep finds any of the strings in the list separated by '|' in any of the 2 files, let the if condition be true and consequently change the variable $LD_LIBRARY_PATH

User avatar
fredx181
Posts: 3074
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 374 times
Been thanked: 1309 times
Contact:

Re: Assign command + options to a single variable?

Post by fredx181 »

@mikewalsh Off-topic (mostly), but just in case you share another portable browser setup, I think it's good to have a check for 'posix' compatibility on top of the LAUNCH script.
So then, when launching from e.g. FAT32 filesystem it gives an error message instead of (silently) failing to work as expected (due to chmod/chown error or contains symlinks), this should work:

Code: Select all

HERE="$(dirname "$(readlink -f "$0")")"

### fredx181, 2024-08-01  Test first if we are not on e.g. FAT32 or NTFS
touch "$HERE"/._test1
echo "testing filesystem on "$HERE" for posix compatibility"
ln -s "$HERE"/._test1 "$HERE"/._test2 2>/dev/null && chmod +x "$HERE"/._test1 2>/dev/null && [ -x "$HERE"/._test1 ] && chmod -x "$HERE"/._test1 2>/dev/null && [ ! -x "$HERE"/._test1 ] && rm -f "$HERE"/._test1 "$HERE"/._test2

if [ $? -ne 0 ]; then
echo -e "This program needs to run from a Linux filesystem\n e.g ext2/ext3/ext4\n Exiting..."
gxmessage -center -geometry 520x230 "$(echo -e "It is required to run this program from a Linux filesystem\n e.g ext2/ext3/ext4\n Exiting...")"
rm -f "$HERE"/._test1 2>/dev/null
exit
else
echo "OK, filesystem on "$HERE" is posix compatible, continue..."
fi

Also, better to add double quotes around the user-data-dir path too, e.g. --user-data-dir="$HERE/PROFILE/spot/Brave-Browser"

Post Reply

Return to “Programming”