LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-13-2022, 11:16 PM   #1
windowsuser
LQ Newbie
 
Registered: May 2021
Location: Poland
Distribution: Manjaro, Alpine, OpenWRT, Debian
Posts: 19

Rep: Reputation: Disabled
Bash script fails to read the "xclip -o" output's lenght


I am trying to write the following script to automatically generate and open a QR code whenever a Monero adress (i.e. a string 95 characters long) is in the clipboard.
Code:
xclip -o
correctly provides the clipboard's content. However, the following script, for which ShellCheck did not detect any issues, fails to do it's job and instead keeps the value of the variable which was supposed to store the clipboard's length set to 8, no matter what it contains:
Code:
#!/bin/bash

clipboard="xclip -o"
length=${#clipboard}
echo "$length" #this one is just for testing
if [ "$length" -eq 95 ]; then
	qrencode -o "$clipboard.png"
	xdg-open "$clipboard.png"
fi

Last edited by windowsuser; 04-13-2022 at 11:18 PM.
 
Old 04-14-2022, 12:10 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by windowsuser View Post
I am trying to write the following script to automatically generate and open a QR code whenever a Monero adress (i.e. a string 95 characters long) is in the clipboard.
Code:
xclip -o
correctly provides the clipboard's content. However, the following script, for which ShellCheck did not detect any issues, fails to do it's job and instead keeps the value of the variable which was supposed to store the clipboard's length set to 8, no matter what it contains:
Code:
#!/bin/bash

clipboard="xclip -o"
length=${#clipboard}
echo "$length" #this one is just for testing
if [ "$length" -eq 95 ]; then
	qrencode -o "$clipboard.png"
	xdg-open "$clipboard.png"
fi
"xclip -o" is upposed to be a command, and you want to read the command into a variable, yes?
In that case you have to use
Code:
clipboard="$(xclip -o)"
which I believe is called command substitution.

Other than that your script looks fine (I know nothing about QR codes though), except that bash can do arithmetics more elegantly:
Code:
if [ "$length" -eq 95 ]; then
can become
Code:
if (( length == 95 )); then
 
Old 04-14-2022, 07:33 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
Code:
	qrencode -o "$clipboard.png"
	xdg-open "$clipboard.png"
I'm not sure if you want to name your file with your address.

Code:
	qrencode -o "my-address.png "$clipboard"
	xdg-open "$my-address.png"
 
Old 04-14-2022, 10:21 AM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Code:
sudo pacman -S xclip
...
(1/1) installing xclip                              [##########################] 100%
:: Running post-transaction hooks...
https://monerodocs.org/public-address/standard-address/

Code:
xclip -o
4AdUndXHHZ6cfufTMvppY6JwXNouMBzSkbLYfpAV5Usx3skxNgYeYTRj5UzqtReoS44qo9mtmXCqY45DJ852K5Jv2684Rge

clipboard="xclip -o"

length=${#clipboard}

echo "$length"
8
Then this is never going to happen
Code:
if [ "$length" -eq 95 ]; then
Code:
if [ "$length" -eq 95 ]; then
    echo "Yes"
else
    echo "no"
fi

no
 
Old 04-14-2022, 05:41 PM   #5
windowsuser
LQ Newbie
 
Registered: May 2021
Location: Poland
Distribution: Manjaro, Alpine, OpenWRT, Debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
OK it reads the clipboard's length correctly now, though no QR code seems to be generated.
 
Old 04-14-2022, 05:44 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
Look at post #3 again
 
Old 04-16-2022, 02:23 AM   #7
windowsuser
LQ Newbie
 
Registered: May 2021
Location: Poland
Distribution: Manjaro, Alpine, OpenWRT, Debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
Apparently the problem was the incorrect placement of the quotation marks in your snippet, but thanks nevertheless. For the past couple of hours I have been tweaking my script and the single-run version of it used to look like this whereas everything seemed to run fine:
Code:
#!/bin/bash
clipboard="$(xclip -o)"
length=${#clipboard}
dircheck() {
	if [[ ! -d "$HOME/XMR Adress book" ]]; then
	mkdir -p "$HOME/XMR Address book" && cd "$HOME/XMR Address book"
	return
	else
	cd "$HOME/XMR Address book"
	fi
}

if [ "$length" -eq 95 ]; then
	dircheck
	echo 'Press n for a temporary QR code (deleted after 10 minutes). To not generate a QR code, press q. Otherwise, type the filename:'
	read -r filename
	        if [ "$filename" = n ]; then
			qrencode -o tmp.png "$clipboard"
			xdg-open "tmp.png"
			xclip /dev/null
			sleep 600
			rm tmp.png
		elif [ "$filename" = q ]; then
			echo "$clipboard " > tmpclip 
			xclip tmpclip
			rm tmpclip
			exit
		else
			qrencode -o "$filename" "$clipboard"
			xdg-open "$filename"
			xclip /dev/null
		fi
	else 
	sleep 60
	exit
	fi
However I am interested in making this script usable by making it run in background. For the past few hours I have been trying to achieve this with a few loops, the hitherto result here. Unfortunately it doesn't seem to work. The bash debugger for vscode is apparently broken and shellcheck doesn't report any relevant problems. So I would highly appreciate any suggestions on how to make it usable and running in background again (I know that I can make a systemd service of it, but it still would require running
Code:
systemctl restart
after it does it's job without proper loops, wouldn't it?

Last edited by windowsuser; 04-16-2022 at 02:24 AM.
 
Old 04-16-2022, 03:06 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
^ I'd recommend to gather a little more knowledge before tackling systemd.

Let's concentrate on a looped script first.
If the script works as such, wrapping it into one(!) endless loop with one(!) sleep should suffice.
Instead you have several sleeps and waiting loops in there. Esp. idle(), which endlessly (?) checks a value that is not read in again.
 
Old 04-16-2022, 07:54 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
I assume you are manually copying the address to the clipboard so why not create a keyboard shortcut to create the QR code? Use zenity or something similar to create a popup window if desired for your filename function etc.

Using crontab or systemd timer you could run your script every minute for example but if you copied something to the clipboard and then have to wait 60 seconds for something to happen that would get tedious quickly.

You might be able to use a clipboard manager to run your script once you copy the address but no idea how or what that would take.
 
1 members found this post helpful.
Old 04-16-2022, 11:36 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Probably I misunderstood, but how do you want to run an interactive script as a service (or background process)?
 
Old 04-18-2022, 12:36 AM   #11
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
^ & ^^ both good points that I hadn't noticed/adressed before.
I was thinking a hotkey-based solution: highlight some text (?some 95 char QR code thingy?) and press a hotkey that reads the primary selection right there, asks questions, performs operations.
 
  


Reply

Tags
automation, bash, clipboard, scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Copy and paste at the Linux command line with xclip LXer Syndicated Linux News 0 07-06-2019 01:15 AM
Copy a pictures into bmp for MS Office, using xclip? Xeratul Linux - General 9 06-03-2014 06:28 AM
Paste the file content into GEDIT with xclip ? Xeratul Linux - General 2 12-20-2010 05:14 PM
LXer: xclip Does Copy-and-Paste on the Linux Command Line LXer Syndicated Linux News 0 07-03-2009 01:00 AM
LXer: xclip - Copy contents from command line to X clipboard in openSUSE LXer Syndicated Linux News 0 04-14-2009 02:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration