User avatar
WillBoxOwO
Posts: 122
Joined: Wed Jun 27, 2018 10:24 pm

Keyboard shortcut doesnt output random numbers

Sat Aug 04, 2018 1:50 am

Hello there.
Im trying to configure a keyboard shortcut on Raspbian using the default GUI environnement, Openbox.

Im using the following piece of code I wrote to take screenshots using the raspi2png library wich supports OpenGL capture.
I modified the following configuration file: /home/pi/.config/openbox/lxde-pi-rc.xml

My goal is simple; When I hit CTRL+Alt+P, I want it to take a screenshot named with a random number.

Here is the code:

Code: Select all

    </keybind>
    <keybind key="C-A-P">
      <action name="Execute">
        <command>raspi2png -p Desktop/Screenshots/snap${RANDOM}.png</command>
      </action>
    </keybind>
    
Dont get me wrong; it works. But the "${RANDOM}" doesnt output a random number; it simply outputs "${RANDOM}".

Buut I tested the command in lxterminal and it outputs the random number...

What am I doing wrong?
Cheers.
I like cats

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Keyboard shortcut doesnt output random numbers

Sat Aug 04, 2018 5:07 am

Try changing it from

Code: Select all

        <command>raspi2png -p Desktop/Screenshots/snap${RANDOM}.png</command>
to

Code: Select all

        <command>raspi2png -p Desktop/Screenshots/snap$(shuf -i 0-32767 -n 1).png</command>
Note: I've not tested that.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
WillBoxOwO
Posts: 122
Joined: Wed Jun 27, 2018 10:24 pm

Re: Keyboard shortcut doesnt output random numbers

Sat Aug 04, 2018 4:39 pm

DougieLawson wrote:
Sat Aug 04, 2018 5:07 am

Code: Select all

        <command>raspi2png -p Desktop/Screenshots/snap$(shuf -i 0-32767 -n 1).png</command>
Note: I've not tested that.
Thanks for the reply! Sadly, it doesnt work. It works only in the terminal. The keyboard shortcut doesnt even take the screenshot using this command!

I then modified the command as follow:

Code: Select all

raspi2png -p Desktop/Screenshots/snap.png
And it works flawlessly using the keyboard shortcut- but theres no random number at the end of the file...
Would there be a way to launch the terminal in the background and make it run the command?
I like cats

klricks
Posts: 7154
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Keyboard shortcut doesnt output random numbers

Sat Aug 04, 2018 4:57 pm

WillBoxOwO wrote:
Sat Aug 04, 2018 4:39 pm
.....
Would there be a way to launch the terminal in the background and make it run the command?
Try:

Code: Select all

lxterminal -e raspi2png -p Desktop/Screenshots/snap$(shuf -i 0-32767 -n 1).png
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

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

Re: Keyboard shortcut doesnt output random numbers

Sat Aug 04, 2018 4:58 pm

The command isn't being run by a shell so ${RANDOM} is being used literally, on the command line bash will replace that with a random number but since bash isn't invovled here it doesn't happen.

If you want to use shell features you can run it via bash yourself, something along the lines of

Code: Select all

<command>bash -c 'raspi2png -p Desktop/Screenshots/snap${RANDOM}.png'</command>
She who travels light — forgot something.

User avatar
WillBoxOwO
Posts: 122
Joined: Wed Jun 27, 2018 10:24 pm

Re: Keyboard shortcut doesnt output random numbers

Sat Aug 04, 2018 5:34 pm

klricks wrote:
Sat Aug 04, 2018 4:57 pm
Try:

Code: Select all

lxterminal -e raspi2png -p Desktop/Screenshots/snap$(shuf -i 0-32767 -n 1).png
Again, it only works using the terminal... No screenshots are taken!
Plus, even if it worked, the screenshots wouldnt have worked out since it pops an lxterminal window everytime. Thanks tho!

Paeryn wrote:
Sat Aug 04, 2018 4:58 pm
If you want to use shell features you can run it via bash yourself, something along the lines of

Code: Select all

<command>bash -c 'raspi2png -p Desktop/Screenshots/snap${RANDOM}.png'</command>
Yay! This one seems to works! Thanks alot my friend!
I like cats

Return to “Beginners”