LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 04-29-2011, 12:38 AM   #1
Chriswaterguy
Member
 
Registered: Apr 2007
Distribution: CrunchBang 10 Statler
Posts: 106

Rep: Reputation: 16
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.
 
Old 04-29-2011, 08:08 AM   #2
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
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.

Last edited by archtoad6; 04-29-2011 at 08:10 AM. Reason: give a better test example
 
Old 04-29-2011, 09:16 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.

Last edited by David the H.; 04-29-2011 at 09:22 AM. Reason: made a fix
 
Old 04-30-2011, 08:09 PM   #4
Chriswaterguy
Member
 
Registered: Apr 2007
Distribution: CrunchBang 10 Statler
Posts: 106

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by David the H. View Post
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.
 
Old 04-30-2011, 08:13 PM   #5
Chriswaterguy
Member
 
Registered: Apr 2007
Distribution: CrunchBang 10 Statler
Posts: 106

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by archtoad6 View Post
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.

Last edited by Chriswaterguy; 04-30-2011 at 08:35 PM. Reason: relevant bit of quote
 
Old 05-01-2011, 02:54 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.

Last edited by David the H.; 05-01-2011 at 04:02 AM.
 
Old 05-02-2011, 03:53 AM   #7
Chriswaterguy
Member
 
Registered: Apr 2007
Distribution: CrunchBang 10 Statler
Posts: 106

Original Poster
Rep: Reputation: 16
Thumbs up Solved, thanks!

Quote:
Originally Posted by David the H. View Post
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%% *}"
 
  


Reply

Tags
scripts, text editor, zenity


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Word Count on Kate? Raveolution Linux - Software 19 07-03-2014 12:18 PM
how to implement count word skyluke Programming 6 11-09-2010 04:06 PM
Need help in word count command grunge_1 Linux - General 4 03-20-2009 05:01 AM
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 06:21 PM
word count pantera Programming 2 08-31-2004 08:23 AM


All times are GMT -5. The time now is 07:26 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration