Article 5M2PV Bash script - too many arguments

Bash script - too many arguments

by
salmanahmed
from LinuxQuestions.org on (#5M2PV)
Hi
I am working on following script:
Code:#!/bin/bash
wordfile=wordcount_file
if [ -f *.txt ]
then
for i in *.txt;do wc -w $i >> $wordfile;done
else
for filename in $(ls)
do
ext=${filename##*\.}
case "$ext" in
docx) docx2txt $filename
;;
odt) odt2txt $filename --output=$filename.txt
;;
pdf) pdf2txt -o $i.txt $filename
for i in *.txt;do wc -w $i >> $wordfile;done
esac
done
fiThis script is intended to work in the following way:
1. First it checks that whether .txt files exist in the current directory. If yes then create a wordcount report.
2. If .txt files do not exist then run a case statement to first convert three file-types (docx, odt and pdf) to .txt, and then create a wordcount report.

Although the script is working fine, but I am facing following two problem:
1. If the .txt files already exist then the script gives, following error:
Quote:
line 3: [: too many arguments
An Internet search shows that this error happens if the $variable contains spaces, which is not the case here.

2. Only for the first file in the directory (e.g, my-file-01.docx) it creates two text files (e.g, my-file-01.txt and my-file-01.txt.txt). Rest of the files are converting fine (only once).

Can you guide me that what mistake I am making here?latest?d=yIl2AUoC8zA latest?i=WQc9BIGLl9Y:hRH7guCecSg:F7zBnMy latest?i=WQc9BIGLl9Y:hRH7guCecSg:V_sGLiP latest?d=qj6IDK7rITs latest?i=WQc9BIGLl9Y:hRH7guCecSg:gIN9vFwWQc9BIGLl9Y
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments