Re: One last line - one last time
- From: "Chris F.A. Johnson" <cfajohnson@xxxxxxxxx>
- Date: Thu, 22 Jun 2006 17:09:25 -0400
On 2006-06-22, Todd wrote:
Chris F.A. Johnson wrote:
On 2006-06-22, Todd wrote:That's what I am wondering - now I am getting '4'
Chris F.A. Johnson wrote:
On 2006-06-22, Todd wrote:Thanks for the response Chris; Here is my whole script.
Thanks to all for the guidance, and I am one line away from success.Where do the slashes come from? Please post the exact code you are
sed "$1"d myfile.txt > /tmp/myfile.txt
If I echo $1 I receive the correct parameter for example /4/
using, the exact command line you use to call it, and the exact
output.
if I useCorrect for what? Your first line look all right.
sed 4d myfile.txt > /tmp/myfile.txt 'removes line 4
What is the correct syntax?
In debug mode I get the errorWhat debug mode? Do you mean with "set -x"?
sed ''\''9'\''d' myfileThen how *are* you passing the argument? Are you adding single
sed: -e expression #1, char 1: unknown command:`''
It has to be in the way the argument is getting passed to sed right?
quotes that are not stripped by the shell? Which shell?
All I really needed was the part that is causing problems, and (as
I asked before) the exact command line you used to call it, and the
exact output (or contents of your .dolist file).
I think I have tried every variation of the sed command with either
0 lines being removed or all lines being removed,- but never just
that one line! If function del_line was simply {echo "$DELME"} then
it echos simply /4/.
It does? Where do the slashes come from?
running the command
#todo -r 4 'when the del_line function is called, I currently receive
'4' 'from the echo line but line 4 is not removed from the file.
Yes it *is* removed!
You are not putting the file back where you found it.
debugging with set -x shows that sed expression error.
What error?
What is your command line when it gives you a problem?I am following conventions posted by my professor
I have to be done with this tonight so please nobody hack my script to
death!
#!/bin/bash
# An attempt to create a todo shellscript
# butchered by Todd D. Patton
# $Id: todo,v 1.1 2006/06/22 03:46:31 ua04 Exp $
SCRIPTNAME=${0##*/}
function show_list() {
Don't use this bash-only hybrid. Use either the portable form of
definition (preferably):
Get a new professor, one who knows what he's talking about.
show_list() {It was "suggested" to use nl - which was posted in my first request for
Or the ksh style:
function show_list {
cat -n .dolist
The -n option to cat is non-standard.
help I found cat -n easier to comprehend. Or at least to me -a non
programmer
Good suggestion - thanks}
function del_line() {
echo "$delme"
sed "$delme"d .dolist > /tmp/dolist
mv /tmp/dolist ~/bin/.dolist
You should only perform the move if sed was successful:
sed "$delme"d .dolist > /tmp/dolist &&
mv /tmp/dolist ~/bin/.dolist
Why are you sending the file to a different location? You use sedSchool computer using ssh. I am running this from ~/bin and wanted to
on ./.dolist, and then move the modified file to ~/bin/.dolist.
(Why would you put a data file in ~/bin?)
The location of the program has nothing to do with the location of
data files.
make sure the output goes to the same location.
You are not using the same location, and that is the source of
your problem. You are reading .dolist in the current directory
(whatever that is), and putting the modified file in $HOME/bin.
I should just add it to my path.
Your PATH has nothing to do with data files.
Define the file in a variable and use that for all references toEven better
it:
DOLIST=$HOME/.dolist
del_line() {
echo "$delme"
sed "$delme"d "$DOLIST" > /tmp/dolist
mv /tmp/dolist "$DOLIST"
}
function show_help() {
echo "Usage: todo -[hrl:] "
echo " 'something' (adds to da' list)"
echo " with no options (displays da' list)"
echo " -h (prints usage)"
echo " -r (removes line number specified)"
exit 0
}
#***********************************************************************
set -x
if [ "$1" != "-r" -a "$1" != "-h" -a "$1" != "" -a "$1" != "--help" ]
then
echo "$*" >> .dolist
fi
ARGS=`getopt -o hr: -l help -- "$@"`
You have been advised not to use getopt. You are making the script
far more complicated than it need be.
Again -"suggested" by professor
There's no need to copy a prof's bad habits.
if [ $? -ne 0 ]
then
echo "crap"
exit 1;
fi
set -- $ARGS
DONE=false
while [ "$DONE" != "true" ]
do
case "$1" in
-h | --help)show_help ;;
-r)delme=$2;del_line ;;
--)DONE=true ;;
esac
shift
done
show_list
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.
- References:
- One last line - one last time
- From: Todd
- Re: One last line - one last time
- From: Chris F.A. Johnson
- Re: One last line - one last time
- From: Todd
- Re: One last line - one last time
- From: Chris F.A. Johnson
- Re: One last line - one last time
- From: Todd
- One last line - one last time
- Prev by Date: Re: One last line - one last time
- Next by Date: Re: why "find ... ! -fstype nfs .. " does not work?
- Previous by thread: Re: One last line - one last time
- Next by thread: Unix - Need to Find & Replace based on a specific string
- Index(es):
Relevant Pages
|