Phrawm48
January 17th, 2008, 11:43 PM
I'm just beginning to write some small BASH scripts.
I recently put together this little script that uses xclip and zenity to display a Gnome (GUI) info box with the number of words and characters contained in the primary clipboard.
1 #!/bin/bash
2
3 words=$(xclip -out primary | wc --words)
4 chars=$(xclip -out primary | wc --chars)
5
6 zenity --info --title='Words (Characters)' --text="Words=$words (Chars=$chars)"
7
8 # echo "echo words=$words chars=$chars"
The script works fine -- once -- but the problem I'm having is that the script doesn't produce the same output if it's run twice. More specifically, it appears that zenity is clobbering (or if you prefer, "eating") the contents of the clipboard.
If I comment out line 6, ("zenity...") and un-comment line 8 (echo...") I can run the script again and again and the script will repeatedly redisplay the number of words and characters in the clipboard. No problem.
But if I reverse the commenting so as to use zenity to display the results of the xclip command, only the first iteration works properly -- the second iteration displays 0 and 0 (zero and zero). I then have to reselect text to make the script display a non-zero value.
Everything troubleshooting method I've tried (such as unset'ing the variables in the script) produces the same result -- zenity appears to be "eating" the contents of the clipboard.
Can anyone please suggest some alternative troubleshooting methods or perhaps tell me how my script is deficient?
Cheers & thanks,
Ric
I recently put together this little script that uses xclip and zenity to display a Gnome (GUI) info box with the number of words and characters contained in the primary clipboard.
1 #!/bin/bash
2
3 words=$(xclip -out primary | wc --words)
4 chars=$(xclip -out primary | wc --chars)
5
6 zenity --info --title='Words (Characters)' --text="Words=$words (Chars=$chars)"
7
8 # echo "echo words=$words chars=$chars"
The script works fine -- once -- but the problem I'm having is that the script doesn't produce the same output if it's run twice. More specifically, it appears that zenity is clobbering (or if you prefer, "eating") the contents of the clipboard.
If I comment out line 6, ("zenity...") and un-comment line 8 (echo...") I can run the script again and again and the script will repeatedly redisplay the number of words and characters in the clipboard. No problem.
But if I reverse the commenting so as to use zenity to display the results of the xclip command, only the first iteration works properly -- the second iteration displays 0 and 0 (zero and zero). I then have to reselect text to make the script display a non-zero value.
Everything troubleshooting method I've tried (such as unset'ing the variables in the script) produces the same result -- zenity appears to be "eating" the contents of the clipboard.
Can anyone please suggest some alternative troubleshooting methods or perhaps tell me how my script is deficient?
Cheers & thanks,
Ric