Results 1 to 5 of 5
I am adding items to a text file (I'm using random names for the example below). Rather than opening the file in vi over and over again, I have been ...
- 08-22-2011 #1Just Joined!
- Join Date
- May 2011
- Posts
- 9
Question about redirecting.
I am adding items to a text file (I'm using random names for the example below). Rather than opening the file in vi over and over again, I have been typing
> echo Mark >> ~/foo/bar/data.txt
> echo Brad >> ~/foo/bar/data.txt
There has to be a better way. I'd like to have an alias where I could simply type...
> xyx Todd
.. and have that new text/data added to the list
Any suggestions?
- 08-23-2011 #2Linux Guru
- Join Date
- May 2011
- Posts
- 1,842
write a function and put it in your ~/.bashrc, e.g.:
Code:myfunc(){ arg=$1 if [ -n "$arg" ]; then echo $arg >> data.txt fi }
- 08-23-2011 #3Just Joined!
- Join Date
- May 2011
- Posts
- 9
- 08-23-2011 #4Linux Guru
- Join Date
- May 2011
- Posts
- 1,842
Not sure about your renaming question - do you mean you already have the "myfunc" function defined? Yeah, you can rename the function i supplied to anything you want (that's unique).
Making it a separate bash script and providing an alias should be perfectly fine, too. If it doesn't work, as you say, post your bash script, the permissions of it, your alias line, how you ran it, and any output sent to the terminal (errors, etc.).
- 08-24-2011 #5
You can define as many functions as you like.I tried just making that its own bash script then creating an alias to that, but that didn't work.
Why do you want an alias?
Either write a separate script and put it in a directory that is in your PATH (usually $HOME/bin), make it executable (chmod +x /path/to/file), then you can call it by name.
If you want a function, put it in your .bashrc file:
You'll probably find it more useful if you allow a filename to be specified on the command line:Code:xyx() { printf "%s\n" "$*" >> "$HOME/foo/bar/data.txt" }
Code:file=$HOME/foo/bar/data.txt case $1 in -f) file=$2; shift 2 ;; -f*) file=${1#-f}; shift ;; esac printf "%s\n" "$*" >> "$file"


Reply With Quote
