Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2011
    Posts
    13

    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?

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    write a function and put it in your ~/.bashrc, e.g.:
    Code:
    myfunc(){
      arg=$1
      if [ -n "$arg" ]; then
        echo $arg >> data.txt
      fi
    }

  3. #3
    Just Joined!
    Join Date
    May 2011
    Posts
    13
    Quote Originally Posted by atreyu View Post
    write a function and put it in your ~/.bashrc, e.g.:
    Code:
    myfunc(){
      arg=$1
      if [ -n "$arg" ]; then
        echo $arg >> data.txt
      fi
    }
    So then I 'run' that by typing

    > myfunc todd

    what if I already have a function defined?
    Is there a way to rename that? I tried just making that its own bash script then creating an alias to that, but that didn't work.

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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.).

  5. #5
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by WyattOil View Post
    So then I 'run' that by typing

    > myfunc todd

    what if I already have a function defined?
    Is there a way to rename that?

    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:
    Code:
    xyx() {
     printf "%s\n" "$*" >> "$HOME/foo/bar/data.txt"
    }
    You'll probably find it more useful if you allow a filename to be specified on the command line:

    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"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •