Re: One last line - one last time



On 2006-06-22, Todd wrote:
Chris F.A. Johnson wrote:
On 2006-06-22, Todd wrote:
Chris F.A. Johnson wrote:
On 2006-06-22, Todd wrote:
Thanks to all for the guidance, and I am one line away from success.

sed "$1"d myfile.txt > /tmp/myfile.txt

If I echo $1 I receive the correct parameter for example /4/
Where do the slashes come from? Please post the exact code you are
using, the exact command line you use to call it, and the exact
output.

if I use
sed 4d myfile.txt > /tmp/myfile.txt 'removes line 4

What is the correct syntax?
Correct for what? Your first line look all right.

In debug mode I get the error
What debug mode? Do you mean with "set -x"?

sed ''\''9'\''d' myfile
sed: -e expression #1, char 1: unknown command:`''

It has to be in the way the argument is getting passed to sed right?
Then how *are* you passing the argument? Are you adding single
quotes that are not stripped by the shell? Which shell?

Thanks for the response Chris; Here is my whole script.

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?
That's what I am wondering - now I am getting '4'
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 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):
I am following conventions posted by my professor

Get a new professor, one who knows what he's talking about.

show_list() {

Or the ksh style:

function show_list {

cat -n .dolist

The -n option to cat is non-standard.

It was "suggested" to use nl - which was posted in my first request for
help I found cat -n easier to comprehend. Or at least to me -a non
programmer
}


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
Good suggestion - thanks

Why are you sending the file to a different location? You use sed
on ./.dolist, and then move the modified file to ~/bin/.dolist.
(Why would you put a data file in ~/bin?)

School computer using ssh. I am running this from ~/bin and wanted to

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 to
it:

DOLIST=$HOME/.dolist
Even better

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
.



Relevant Pages

  • Re: Find is too sensitive
    ... Show us the actual commands you executed ... But here are the exact commands: ... echo "Temporary directory is $HOME/$$" ... Georgia - Music pink and Blue.jpg" ...
    (comp.unix.shell)
  • Re: One last line - one last time
    ... On 2006-06-22, Todd wrote: ... If I echo $1 I receive the correct parameter for example /4/ ... Where do the slashes come from? ... Please post the exact code you are ...
    (comp.unix.shell)
  • Re: One last line - one last time
    ... If I echo $1 I receive the correct parameter for example /4/ ... Where do the slashes come from? ... Please post the exact code you are ... Thanks for the response Chris; ...
    (comp.unix.shell)
  • Re: cannot access inc files from a function
    ... What is the exact name of the file you are trying to include. ... > echo $acrylic; ... > But it doen't print the content from $charcoal, $oil or, etc. ... Bettina ...
    (comp.lang.php)
  • Re: Saving DB to flash drive
    ... Here's some information I found about copying files on a Microsoft website: ... Todd ... Doug Steele, Microsoft Access MVP ... echo STARTING BACKUP ...
    (microsoft.public.access.gettingstarted)