DSL Ideas and Suggestions :: Suggestion for new MS-Word viewer for DSL - script



I have played around with some bash scripting lately, and I have made a suggestion for a new (alternative) MS Word viewer for DSL. It is simply a script, and it works solely on the commandline, interacting with antiword etc. If you find it interesting, try it out! What I like most about it is that it is very small! Only about 4 K, and that's not bad for a Word-viewer, I think...

(Sorry if the post is rather long, but it is a rather large script...) Hope you like it:

Code Sample

#!/bin/bash
# This is another DSL MS-Word Viewer.
# Made by Reidar Mosvold.

clear
while test "$answer" != "0"
do
clear
echo ""
echo " ************************************************"
echo " DSL Word Viewer "
echo " ************************************************"
echo ""
echo " 1 - List files in directory "
echo " 2 - Change directory "
echo " 3 - View MS-Word file "
echo " 4 - Edit MS-Word file "
echo " 5 - Convert MS-Word file to pdf "
echo " 6 - View pdf file "
echo " 7 - Remove a file "
echo " 8 - Quit "
echo ""
echo -n " Choose a function (1-8) and press <ENTER> key :"
read answer
# cd $HOME
#
# 1 - List files
#
if test "$answer" = "1";then
clear
echo ""
echo "Current directory is :"
pwd
echo ""
echo "List files and directories"
ls -la | more
echo ""
echo -n "Press <ENTER> to continue :"
read stop
fi
#
# 2 - Change directory
#
if test "$answer" = "2";then
clear
echo ""
echo -n "Current directory is :"
pwd
echo ""
echo "To continue in current directory answer with '.' (period) and press <ENTER>"
echo "If you want to move one level up, answer with '..' (two periods) and press <ENTER>"
echo ""
echo "If you want to move back to your home-directory press <ENTER>"
echo ""
echo "If you want to move to a child-directory, write the name of the directory and press <ENTER>"
echo ""
echo -n "Which directory would you like to use :"
read dir
cd $dir
echo ""
echo -n "New active directory is :"
pwd
echo ""
echo "List files in current directory :"
ls
echo ""
echo -n "Press <ENTER> to continue :"
read stop
fi
#
# 3 - View MS-Word document
#
if test "$answer" = "3";then
clear
echo ""
echo -n "Here are the files in "
pwd
echo ""
ls
echo ""
echo "If the file is more than one page you may continue by pressing <ENTER>"
echo ""
echo -n "Which file would you like to view on your screen :"
read file
if test -s "$file";then
antiword "$file" | more
echo ""
echo -n "Press <ENTER> to continue :"
read stop
else
echo ""
echo "The file does not exist or is not MS-Word format"
echo ""
echo -n "Press <ENTER> to continue :"
read stop
fi
fi
#
# 4 - Edit MS-Word document
#
if test "$answer" = "4";then
clear
echo ""
echo -n "Here are the files in "
pwd
echo ""
ls
echo ""
echo -n "Which file would you like to edit :"
read editwordfile
echo ""
echo -n "What new filename would you like to give the document (*.txt) :"
read newfilename
echo ""
echo -n "Which editor would you like to use :"
read editortouse
if test -s "$editwordfile";then
antiword "$editwordfile" > "$newfilename"
"$editortouse" "$newfilename"
echo ""
echo -n "Press <ENTER> to continue :"
read stop
else
echo "The file does not exist or is not in MS-Word format"
echo ""
echo -n "Press <ENTER> to continue :"
read stop
fi
fi
#
# 5 - Convert MS-Word file to pdf
#
if test "$answer" = "5";then
clear
echo ""
echo -n "Here are the files in "
pwd
echo ""
ls
echo ""
echo -n "Which file (*.doc) would you like to convert to pdf :"
read convertfile
echo -n "Insert a new filename (*.pdf) :"
read newfile
if test -s "$convertfile";then
antiword -p a4 "$convertfile" > testfil.ps
ps2pdf testfil.ps "$newfile"
rm -f testfil.ps
echo ""
echo -n "Press <ENTER> to continue :"
read stop
else
echo ""
echo "The file does not exist or is not in MS-Word format"
echo ""
echo -n "Press <ENTER> to continue :"
read stop
fi
fi

#
# 6 - View pdf file
#
if test "$answer" = "6";then
clear
echo ""
echo -n "Here are the files in "
pwd
echo ""
ls
echo ""
echo -n "Which pdf file would you like to view :"
read viewpdffile
if test -s "$viewpdffile";then
xpdf "$viewpdffile" &
echo ""
echo -n "Press <ENTER> to continue :"
read stop
else
echo ""
echo "The file does not exist or is empty"
echo ""
echo -n "Press <ENTER> to continue :"
read stop
fi
fi

#
# 7 - Remove a file
#
if test "$answer" = "7";then
clear
echo ""
echo -n "Here are the files in "
pwd
echo ""
ls
echo ""
echo "(If you don't want to remove a file, press <ENTER>)"
echo -n "Which file would you like to remove :"
read removefile
if test -s "$removefile";then
rm "$removefile"
echo ""
echo -n "Press <ENTER> to continue :"
read stop
else
echo ""
echo "The file does not exist or is empty"
echo ""
echo -n "Press <ENTER> to continue :"
read stop
fi
fi

#
# 8 - Quit
#
if test "$answer" = "8";then
exit;
fi

done


-r

just a couple of things that may shorten the bytecount of your script...
Instead of using mutliple "test" commands, try "case":
Code Sample
case $answer in
1)
clear
echo "
Current directory is :"
pwd
echo "
List files and directories"
ls -la | more
echo -n "
Press <ENTER> to continue :"
read stop
;;
2) stuff for 2;;
3) stuff for 3;;
blah blah blah
esac

Using an echo command just to make a new line is redundant in Bash.  Just add the newline within the quotes.

I'm not sure why the "while" is being used, but i haven't read the script thoroughly.

A shell script can usually be made more useful when you allow the use of commandline parameters, such as
"scriptname 5 input.doc output.pdf"

Nice to see more interest in scripting...keep posting your stuff here.
:)

Thanks a lot for your feedback!

I am just learning shell scripting, so I am sure that my script is not at all perfect. So I am grateful for all advice. I will keep working on this and other scripts, because I believe in powerful commandline scripts/programs and not only gui programs to get the job done. Thanks again for your advice :-)

-r

FYI,

I am anything but an expert on shell scripting, but much of what I learned was from this guide:

http://www.tldp.org/LDP/abs/html/


original here.