[solved] Need help with bash script...

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
Hultan
Level 3
Level 3
Posts: 178
Joined: Mon Aug 27, 2018 10:58 am

[solved] Need help with bash script...

Post by Hultan »

The other day I wrote a bash script that switches between two folders for the desktop background slideshow. I then bind that script to a hotkey so that I can execute it with a button click, and switch between them. This script worked fine for a day or so, but now it is super weird and does not switch folders.

Can someone see what is wrong with it, and maybe explain why?

Bonus question: Bash is the weirdest scripting language I have ever seen. Spaces are important in if statements(!?), and sometimes you need to use parenthesis and sometimes a single bracket and other times double brackets!?!? And what with the semicolon in the if-statement? Who invented that syntax? And the fi keyword!? How about using curly brackets or, since I have coded Visual Basic, I'll even accept end if!? Explain that to me and you'll get a gold star! :-)

It took me hours to get this script to work because of the weird syntax and the white space sensitive syntax, and I have been programming for roughly 30 years now in a bunch of different languages and I have seen some weird sh*t. I haven't been this confused since I coded LISP in the early 90:s.

Just as I was about to press the submit button, I realized that I upgraded to LM 19.1 earlier today. Has something changed because of that? I can see in dconf editor that it changes the picture-uri but the background is not changing.

Code: Select all

#!/bin/bash

# get the current slideshow folder
DESK_BACKGROUND=$(gsettings get org.cinnamon.desktop.background.slideshow image-source)

if [[ $DESK_BACKGROUND == *second* ]]; then
    #if its the second folder, change to the first folder, and switch to a random picture
    RANDOM_PICTURE=$(ls /home/per/Pictures/backgrounds | shuf -n 1)
    gsettings set org.cinnamon.desktop.background.slideshow image-source "directory:///home/per/Pictures/backgrounds"
    gsettings set org.cinnamon.desktop.background picture-uri $RANDOM_PICTURE
else
    #if its the first folder, change to the second folder, and switch to a random picture
    RANDOM_PICTURE=$(ls /home/per/Pictures/backgrounds_second | shuf -n 1)
    gsettings set org.cinnamon.desktop.background.slideshow image-source "directory:///home/per/Pictures/backgrounds_second"
    gsettings set org.cinnamon.desktop.background picture-uri $RANDOM_PICTURE
fi

# set random, enable slideshow and set delay to 10 minutes
gsettings set org.cinnamon.desktop.background.slideshow random-order true
gsettings set org.cinnamon.desktop.background.slideshow slideshow-enabled true
gsettings set org.cinnamon.desktop.background.slideshow delay 10
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Hultan
Level 3
Level 3
Posts: 178
Joined: Mon Aug 27, 2018 10:58 am

Re: Need help with bash script...

Post by Hultan »

Eeh, the solution was as easy as to turn of slideshow-enabled in dconf editor and set it again, which is weird because I do set it in the script?
gm10

Re: Need help with bash script...

Post by gm10 »

Hultan wrote: Fri Dec 21, 2018 2:39 pm Eeh, the solution was as easy as to turn of slideshow-enabled in dconf editor and set it again, which is weird because I do set it in the script?
You only set it to enabled, which it probably already was at that point, so it likely had no reason to reload its configuration. But I'm not on Cinnamon, I don't know how that slideshow works.
Hultan wrote: Fri Dec 21, 2018 2:35 pm Bonus question: Bash is the weirdest scripting language I have ever seen. Spaces are important in if statements(!?),
I guess you've never seen Python, either.
Hultan wrote: Fri Dec 21, 2018 2:35 pm and sometimes you need to use parenthesis and sometimes a single bracket and other times double brackets!?!?
Programming languages have different operators? Who would have thunk...
Hultan wrote: Fri Dec 21, 2018 2:35 pm And what with the semicolon in the if-statement? Who invented that syntax?
Clearly you've never seen any C-type language, either (e.g. Javascript). Unlike C, you only need the semicolon if you want to put several statements onto the same line, as you did in your case. There's no requirement to do that though, just put the "then" statement on the next line, where it belongs.
Hultan wrote: Fri Dec 21, 2018 2:35 pm And the fi keyword!? How about using curly brackets or, since I have coded Visual Basic, I'll even accept end if!? Explain that to me and you'll get a gold star! :-)
Block level statements get closed with a reverse statement. So if ... fi, case ... esac, etc. I don't understand your consternation. If you don't like this language, use another one.
Hultan wrote: Fri Dec 21, 2018 2:35 pm I have been programming for roughly 30 years now in a bunch of different languages and I have seen some weird sh*t. I haven't been this confused since I coded LISP in the early 90:s.
Given that level of experience, your confusion confuses me greatly to be honest. Much of what you ask about has been standardized as part of the POSIX specification for decades and is relevant for a large number of operating systems. Surely you must have heard of that. Bash syntax goes beyond POSIX though and thus is not fully portable (e.g. the double brackets you mentioned).
Hultan
Level 3
Level 3
Posts: 178
Joined: Mon Aug 27, 2018 10:58 am

Re: Need help with bash script...

Post by Hultan »

gm10 wrote: Fri Dec 21, 2018 3:03 pm
Hultan wrote: Fri Dec 21, 2018 2:39 pm Eeh, the solution was as easy as to turn of slideshow-enabled in dconf editor and set it again, which is weird because I do set it in the script?
You only set it to enabled, which it probably already was at that point, so it likely had no reason to reload its configuration. But I'm not on Cinnamon, I don't know how that slideshow works.
I'll change the script to this, maybe that will help in the future:

gsettings set org.cinnamon.desktop.background.slideshow slideshow-enabled false
gsettings set org.cinnamon.desktop.background.slideshow slideshow-enabled true
gm10 wrote: Fri Dec 21, 2018 3:03 pm
Hultan wrote: Fri Dec 21, 2018 2:35 pm Bonus question: Bash is the weirdest scripting language I have ever seen. Spaces are important in if statements(!?),
I guess you've never seen Python, either.
I have for a few hours, right when I switched to Linux a few months ago. I was looking for a new language in Linux, since lately I have been coding mainly c# (winforms) in Windows. I noticed that Python was kind of weird regarding tabs, so I decided not to start coding Python :-)
gm10 wrote: Fri Dec 21, 2018 3:03 pm
Hultan wrote: Fri Dec 21, 2018 2:35 pm and sometimes you need to use parenthesis and sometimes a single bracket and other times double brackets!?!?
Programming languages have different operators? Who would have thunk...
Of course they have but I saw sample bash script all over the internet, some with single brackets, some with double and some with parenthesis. THAT was what was weird to me, do they mean different things? Are the brackets and parenthesis like operators in some way? Can they be used interchangeably, I did not get that feeling when I wrote the script.
gm10 wrote: Fri Dec 21, 2018 3:03 pm
Hultan wrote: Fri Dec 21, 2018 2:35 pm And what with the semicolon in the if-statement? Who invented that syntax?
Clearly you've never seen any C-type language, either (e.g. Javascript). Unlike C, you only need the semicolon if you want to put several statements onto the same line, as you did in your case. There's no requirement to do that though, just put the "then" statement on the next line, where it belongs.
Of course, I have coded C/C++/C#/Javacsript etc, what I wondered about was the positioning of the semi colon after the condition and before the then keyword. In c-like languages semicolon marks the end of the line, which it is clearly not doing in bash. So what does it do? To me it just looks weird there, but maybe it has a purpose. Please educate me if it does.

[Edit:] I just understood what you were saying, but to treat the if-condition-part and the then-part of an if-statement as "different" statements is another thing that is just weird to me. To require either a new line or a semicolon in the middle of the if-statement I do not like.
gm10 wrote: Fri Dec 21, 2018 3:03 pm
Hultan wrote: Fri Dec 21, 2018 2:35 pm And the fi keyword!? How about using curly brackets or, since I have coded Visual Basic, I'll even accept end if!? Explain that to me and you'll get a gold star! :-)
Block level statements get closed with a reverse statement. So if ... fi, case ... esac, etc. I don't understand your consternation. If you don't like this language, use another one.
I do get that, I was just listing things that annoyed me. And I will use other languages primarily, but in this case I wanted to try a bash script.
gm10 wrote: Fri Dec 21, 2018 3:03 pm
Hultan wrote: Fri Dec 21, 2018 2:35 pm I have been programming for roughly 30 years now in a bunch of different languages and I have seen some weird sh*t. I haven't been this confused since I coded LISP in the early 90:s.
Given that level of experience, your confusion confuses me greatly to be honest. Much of what you ask about has been standardized as part of the POSIX specification for decades and is relevant for a large number of operating systems. Surely you must have heard of that. Bash syntax goes beyond POSIX though and thus is not fully portable (e.g. the double brackets you mentioned).
I have not heard of that, I just switched to Linux from Windows a few months ago. Now I know, but the fact that it is standardized won't change how weird I feel that the language is (after a full two-three hours of coding it) :-)
gm10

Re: Need help with bash script...

Post by gm10 »

Hultan wrote: Fri Dec 21, 2018 3:25 pm Of course they have but I saw sample bash script all over the internet, some with single brackets, some with double and some with parenthesis. THAT was what was weird to me, do they mean different things? Are the brackets and parenthesis like operators in some way? Can they be used interchangeably, I did not get that feeling when I wrote the script.
They do have different meaning (in the case of the brackets the double extend the single), but I'll refer you to the documentation on that, it's readily available.
gm10 wrote: Fri Dec 21, 2018 3:03 pm In c-like languages semicolon marks the end of the line, which it is clearly not doing in bash.
No, it doesn't, in C it marks the end of the statement, not the line, and it does the same in bash. But as I said, only needed if you want to place several statements onto a single line.

Code: Select all

if [ true ]; then echo "Hello World"; fi
is the same as

Code: Select all

if [ true ]
  then
    echo "Hello World"
fi
You can chain all kinds of commands that way.
Hultan wrote: Fri Dec 21, 2018 3:25 pm
gm10 wrote: Fri Dec 21, 2018 3:03 pm POSIX
I have not heard of that, I just switched to Linux from Windows a few months ago. Now I know, but the fact that it is standardized won't change how weird I feel that the language is :-)
You didn't look closely enough at your Windows then: https://en.wikipedia.org/wiki/Microsoft_POSIX_subsystem :)

Also welcome to Linux. You can still use all the programming languages you used to use (C# via Mono), no need to do weird shell scripts if they don't feel comfortable. But should be easy enough to pick up on.
Hultan
Level 3
Level 3
Posts: 178
Joined: Mon Aug 27, 2018 10:58 am

Re: Need help with bash script...

Post by Hultan »

gm10 wrote: Fri Dec 21, 2018 3:41 pm You didn't look closely enough at your Windows then: https://en.wikipedia.org/wiki/Microsoft_POSIX_subsystem :)
Lol, apparently not :-)
gm10 wrote: Fri Dec 21, 2018 3:41 pm Also welcome to Linux. You can still use all the programming languages you used to use (C# via Mono), no need to do weird shell scripts if they don't feel comfortable. But should be easy enough to pick up on.
Thank you...

I did not get the feeling that coding Mono was anything like coding c# and Winforms, and .Net core is only for the web, so I decided to freshen up my C++ skills instead (right now I am looking at QT creator). I'll eventually find my home (programming-wise) in Linux.
SuperBoby

Re: Need help with bash script...

Post by SuperBoby »

Hultan wrote: Fri Dec 21, 2018 3:25 pmOf course, I have coded C/C++/C#/Javacsript etc, what I wondered about was the positioning of the semi colon after the condition and before the then keyword. In c-like languages semicolon marks the end of the line, which it is clearly not doing in bash. So what does it do? To me it just looks weird there, but maybe it has a purpose. Please educate me if it does.

In bash (and I think this is part of POSIX), you have 3 ways of writing 2 commands in one line :

Code: Select all

$ command1 && command2
This executes command2 ONLY if command1 has succeded (eg. returned 0)

Code: Select all

$ command1 || command2
This executes command2 ONLY if command1 has failed (eg. returned another value than 0)

Code: Select all

$ command1 ; command2
This executes command2 after command1 regardless of the return value of command1. It is strictly equivalent to :

Code: Select all

$ command1
$ command2


You have to think shell scripts as one line = one command that you type + [Enter].


Also, I agree about python : I also find the syntax super-weird...
gm10

Re: Need help with bash script...

Post by gm10 »

SuperBoby wrote: Fri Dec 21, 2018 3:52 pm Also, I agree about python : I also find the syntax super-weird...
I probably dislike the most that you even have to indent comments properly. Leave my comments alone. :D
Hultan
Level 3
Level 3
Posts: 178
Joined: Mon Aug 27, 2018 10:58 am

Re: Need help with bash script...

Post by Hultan »

SuperBoby wrote: Fri Dec 21, 2018 3:52 pm You have to think shell scripts as one line = one command that you type + [Enter].
Aah, that clicked a little bit in my brain! I was thinking of it more as a normal compiled or interpreted language, that was my silly mistake...

Not that I now understand why the if-statements look the way they do, but I can kind of see them in a different way now.

So I should think of this part as a statement of its own?

Code: Select all

if [[ $DESK_BACKGROUND == *safe* ]];
SuperBoby wrote: Fri Dec 21, 2018 3:52 pm Also, I agree about python : I also find the syntax super-weird...
Good, I am not alone in feeling that :-)
gm10

Re: Need help with bash script...

Post by gm10 »

Hultan wrote: Fri Dec 21, 2018 4:05 pm So I should think of this part as a statement of its own?

Code: Select all

if [[ $DESK_BACKGROUND == *safe* ]];
Yes, because it's more than just a pure condition (which you need the brackets for), it's any command you want to run, the exit status gets evaluated. Look at this:

Code: Select all

if echo "Hello World"; then echo "Success!"; fi
Hultan
Level 3
Level 3
Posts: 178
Joined: Mon Aug 27, 2018 10:58 am

Re: [solved] Need help with bash script...

Post by Hultan »

Aah, now I get it. I was thinking about it wrong :-)

Now I just need to go to the documentation and read up on the single bracket vs double bracket vs parenthesis vs nothing at all-stuff, which I still don't get :-)

Thanks for the info...
gm10

Re: [solved] Need help with bash script...

Post by gm10 »

The square brackets are an alias for a command actually.

Code: Select all

if [ "x" == "x" ];
is the same as

Code: Select all

if test "x" == "x"; 
But I'll let you go through the documentation for the details, I think it's easier if you see all the operators in one place rather than getting fed information piece by piece.
SuperBoby

Re: [solved] Need help with bash script...

Post by SuperBoby »

Hultan wrote: Fri Dec 21, 2018 4:26 pmNow I just need to go to the documentation
I would suggest to find several UNIX shell tutorials to compare different points of view, then find yourself a cheat sheet and practice everytime you need to automate something. You will find that it is in fact a -very- powerful tool !


And yes, I said "UNIX shell" and not just "Bash", because understanding the way of thinking from the early days will make you click on a lot of other things :)
FreedomTruth
Level 4
Level 4
Posts: 443
Joined: Fri Sep 23, 2016 10:19 am

Re: [solved] Need help with bash script...

Post by FreedomTruth »

If you're more comfortable with C, there is a C-shell (apt install csh). I haven't used it in ages...
Your initial script would look something like this (not tested) :)

Code: Select all

#!/bin/csh

# get the current slideshow folder
set DESK_BACKGROUND = `gsettings get org.cinnamon.desktop.background.slideshow image-source`

if ($DESK_BACKGROUND == *second*) then
    #if its the second folder, change to the first folder, and switch to a random picture
    set RANDOM_PICTURE = `ls /home/per/Pictures/backgrounds | shuf -n 1`
    gsettings set org.cinnamon.desktop.background.slideshow image-source "directory:///home/per/Pictures/backgrounds"
    gsettings set org.cinnamon.desktop.background picture-uri $RANDOM_PICTURE
else
    #if its the first folder, change to the second folder, and switch to a random picture
    set RANDOM_PICTURE = `ls /home/per/Pictures/backgrounds_second | shuf -n 1`
    gsettings set org.cinnamon.desktop.background.slideshow image-source "directory:///home/per/Pictures/backgrounds_second"
    gsettings set org.cinnamon.desktop.background picture-uri $RANDOM_PICTURE
endif

# set random, enable slideshow and set delay to 10 minutes
gsettings set org.cinnamon.desktop.background.slideshow random-order true
gsettings set org.cinnamon.desktop.background.slideshow slideshow-enabled true
gsettings set org.cinnamon.desktop.background.slideshow delay 10
Think of scripts as being like batch files; but each shell has its own set of built-in commands and syntax, like DOS or powershell.
Faust

Re: Need help with bash script...

Post by Faust »

gm10 wrote: Fri Dec 21, 2018 3:54 pm
SuperBoby wrote: Fri Dec 21, 2018 3:52 pm Also, I agree about python : I also find the syntax super-weird...
I probably dislike the most that you even have to indent comments properly. Leave my comments alone. :D
Have you ventured down the Ruby road at all ?
Now that really is irritating when it comes to indents and spaces .... :lol:
gm10

Re: Need help with bash script...

Post by gm10 »

Faust wrote: Sat Dec 22, 2018 2:37 am Have you ventured down the Ruby road at all ?
Now that really is irritating when it comes to indents and spaces .... :lol:
I thought indentation rules in Ruby were similar to Python but no, I haven't been using Ruby myself.
Locked

Return to “Beginner Questions”