LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Word count script for medit - help troubleshoot please (https://www.linuxquestions.org/questions/programming-9/word-count-script-for-medit-help-troubleshoot-please-877740/)

Chriswaterguy 04-28-2011 11:38 PM

Word count script for medit - help troubleshoot please
 
I love medit for text editing, but it's missing a word count feature.

I found a "Statistics for medit" script on the Arch Linux Forums, but after installing as instructed, there was no popup.

So I set it to output to a pane instead, and I got:

Code:

[/home/cwg43/pwb] LENGTH=${#DOC}
VAR1=`wc -c $DOC`
VAR2=`wc -m $DOC`
VAR3=`wc -w $DOC`
VAR4=`wc -l $DOC`
VAR5=`wc -L $DOC`

zenity --list --title="Statistics" --width=200 --height=300 --text="Powered by wc and zenity" --column "Sort" "File size: "${VAR1:0:${#VAR1}-LENGTH} "Characters: "${VAR2:0:${#VAR2}-LENGTH} "Words: "${VAR3:0:${#VAR3}-LENGTH} "Lines: "${VAR4:0:${#VAR4}-LENGTH} "Longest Line: "${VAR5:0:${#VAR5}-LENGTH}
/bin/sh: Bad substitution
*** Exited with status 2 ***

(Last two lines in red.)

My scripting skill is extremely basic, so I'd appreciate any troubleshooting help. Thanks!

(I'm using CrunchBang Statler, which is Debian Squeeze based - so I didn't ask for help on the Arch Linux Forums.

archtoad6 04-29-2011 07:08 AM

I presume you are a member of bbs.archlinux.org, I think you should post this there asking ConnorBehan for help. I don't see why this problem is related to using Debian instead of Arch. Even if it is, the author may be interested in making the add on more universal.

What Google, especially Google Linux, searches have you done?

I just installed medit-0.9.2 which is the latest Debian package available to me in SimplyMEPIS 8.0, including python & zenity. Which ver. of medit are you using? I ask because I can't find the "Settings > Preferences > Tools" mentioned in the wiki article you linked to. So I don't know how to install this add on.

I think this line in the output is the key to your problem (it was red, after all :)):
Code:

/bin/sh: Bad substitution
I ran the following test in a Konsole (KDE's console):
Code:

/bin/sh
DOC=<filename>
sh-3.2$ LENGTH=${#DOC}
sh-3.2$ VAR1=`wc -c $DOC`
sh-3.2$ VAR2=`wc -m $DOC`
sh-3.2$ VAR3=`wc -w $DOC`
sh-3.2$ VAR4=`wc -l $DOC`
sh-3.2$ VAR5=`wc -L $DOC`

sh-3.2$ echo "File size: "${VAR1:0:${#VAR1}-LENGTH} "Characters: "${VAR2:0:${#VAR2}-LENGTH} "Words: "${VAR3:0:${#VAR3}-LENGTH} "Lines: "${VAR4:0:${#VAR4}-LENGTH} "Longest Line: "${VAR5:0:${#VAR5}-LENGTH}
File size: 1850 Characters: 1850 Words: 185 Lines: 71 Longest Line: 61

Because my true shell is bash, "/bin/sh" is bash in sh mode; it is not necessarily a perfect simulation. However, it looks to me like that distinction is not the problem.

HTH, at least you know someone has looked at your post. :)

David the H. 04-29-2011 08:16 AM

I also just installed the Debian package version of medit to test this out, and was very frustrated because it completely failed to install any of the documentation that was supposed to come with it (no info or help pages; only the man page, which gives no useful info about scripting).

So I downloaded the newest medit package and extracted it's help files, and I think I worked it out.

The problem with the above is that the $DOC variable holds the current file name. But if the document hasn't been saved yet, then there's no file for it to work on, so you get empty variables.

Apparently the newest versions of medit have a new input item called "Document Copy", which makes a temporary duplicate of the file for the script to work on. If your install has that option, then switching to it, and changing every instance of "DOC" to "INPUT_FILE", should make it work.

Since Debian's version doesn't have that yet, I improvised a different solution. Since the other input options all feed to stdin, I modified the script like so:
Code:

Command type: shell script
Input: whole document
Output: none:asynchronous
Filter: none
=====
input="$(cat)"
bytes=$( wc -c <<<"$input" )
chars=$( wc -m <<<"$input" )
words=$( wc -w <<<"$input" )
lines=$( wc -l <<<"$input" )
lngst=$( wc -L <<<"$input" )

zenity --list --title="Statistics" \
--width=200 --height=300 \
--text="Powered by wc and zenity" \
--column "Sort" \
"File size: ${bytes%% *}" \
"Characters: ${chars%% *}" \
"Words: ${words%% *}" \
"Lines: ${lines%% *}" \
"Longest Line: ${lngst%% *}"

cat takes input from stdin, allowing the document contents to be stored in a variable for processing by wc.

Incidentally, you can copy this script to another filter and change the Input to "selection", and it will work on partial selections too.

Chriswaterguy 04-30-2011 07:09 PM

Quote:

Originally Posted by David the H. (Post 4340615)
Since Debian's version doesn't have that yet, I improvised a different solution. Since the other input options all feed to stdin, I modified the script like so:

Thanks David. I tried this, and got the error:
Code:

/bin/sh: Syntax error: redirection unexpected
*** Exited with status 2 ***

(When output is changed to pane - otherwise nothing visible
happened.)

But it's good to know that the original code works in a newer version of medit, so I can wait for that if necessary.

Chriswaterguy 04-30-2011 07:13 PM

Quote:

Originally Posted by archtoad6 (Post 4340536)
I presume you are a member of bbs.archlinux.org, I think you should post this there asking ConnorBehan for help. I don't see why this problem is related to using Debian instead of Arch. Even if it is, the author may be interested in making the add on more universal.

What Google, especially Google Linux, searches have you done?

archtoad6, I'm not a member of bbs.archlinux.org - I discovered that page through Google. I find that asking about a problem on platform X on a platform Y forum doesn't always get popular reactions, so here seemed more appropriate.

Can't remember the exact searches I used, but I do use http://www.google.com/linux - awesome tool. Thanks.

David the H. 05-01-2011 01:54 AM

It works for me. Perhaps yours isn't using bash to interpret the script? I used bash-specific here strings above.

I wonder if adding a #!/bin/bash shebang as the first line will work?

Or try replacing the wc lines with a more portable version. e.g.
Code:

bytes=$( echo "$input" | wc -c )
Eidt: After a bit of testing, it looks like the medit script interface ignores shebang settings. But I'm convinced now that you're system is using dash as the shell; I get the same error when I switch to it. So just change the wc lines as above and I'm sure it will work.

Chriswaterguy 05-02-2011 02:53 AM

Solved, thanks!
 
Quote:

Originally Posted by David the H. (Post 4342520)
So just change the wc lines as above and I'm sure it will work.

It did indeed work - thanks!

For the record, the full solution that works for me, with medit-0.10.4 running on CrunchBang Statler (Debian Squeeze-based) is:

Files: [leave blank]
Options: needs-doc
Command type: shell command
Input: whole document
Output: none:asynchronous
Filter: none [Default also seemed to work]

And the script:

Code:

input="$(cat)"
bytes=$( echo "$input" | wc -c )
chars=$( echo "$input" | wc -m )
words=$( echo "$input" | wc -w )
lines=$( echo "$input" | wc -l )
lngst=$( echo "$input" | wc -L )

zenity --list --title="Statistics" \
--width=200 --height=300 \
--text="Powered by wc and zenity" \
--column "Sort" \
"File size: ${bytes%% *}" \
"Characters: ${chars%% *}" \
"Words: ${words%% *}" \
"Lines: ${lines%% *}" \
"Longest Line: ${lngst%% *}"



All times are GMT -5. The time now is 05:16 PM.