[SOLVED] Bash script fails to read the "xclip -o" output's lenght
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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.
^ 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.
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.
^ & ^^ 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.