Results 1 to 4 of 4

Thread: Accepting input for vi script

  1. #1
    Join Date
    Oct 2012
    Beans
    2

    Question Accepting input for vi script

    I've been trying to write the simplest script, but I can only find a few sources that may or may not be right for what I'm looking for... I have an old textbook as well, but even my teacher had said it was useless.
    Basically I'm trying to write a script that clears the screen,
    says "Hello!",
    has 5 blank lines before that sentence,
    asks a question,
    accepts an input,
    touches a file in the home directory,
    clears the screen again,
    and long lists the home directory contents of the user.

    This is what I have:

    Code:
     clear
    echo "Hello!"
    echo
    echo
    echo
    echo
    echo
    echo -n "What do you want to call the file?"
    read -e INPUT
    touch INPUT.txt
    clear
    ls
    ...please help me. So which parts have I got wrong?

  2. #2
    Join Date
    Oct 2012
    Beans
    9

    Re: Accepting input for vi script

    Hi,

    It is unclear what your problem is. Please elaborate.
    The only thing I can see that does not make sense to me in your script is that you do not do anything with the input from the "read" command.
    [Edit: or maybe you are trying to do something with it that I can't understand, why does your input variable (INPUT) and the .txt file have the same name?]

    Regards,
    Mahdi

  3. #3
    Join Date
    Oct 2012
    Beans
    2

    Re: Accepting input for vi script

    Well firstly I want to know if I even have the coding right, haha

    And yes, I would like to make the input taken from the user to that INPUT.txt file.
    (I think I meant to have that initial INPUT as $INPUT or $ANSWER variable...) So maybe
    Code:
    read -e INPUT as $ANSWER
    ....?
    And then some more code for actually creating the INPUT.txt file that I have no idea what it could be.

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,423
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Accepting input for vi script

    Code:
    $ ls
    $ read -p "name of file: " file
    name of file: new_file
    $ > "$file.txt"
    $ ls
    new_file.txt
    if you want to use the contents of the input variable, use it with $ in front of it.
    > "$file.txt" redirects nothing to $file.txt - in other words creates it with 0 size
    but touch "$file.txt" is ok too.
    if you want to have some initial content there:
    Code:
    echo "content" > "$file.txt"  # to overwrite
    echo "content" >> "$file.txt"  # to append

    Besides generally avoid all-caps names, there are plenty all-caps environment variables that you can overwrite by accident if you accidentally pick the 'right' name.
    Last edited by Vaphell; October 29th, 2012 at 11:24 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Tags for this Thread

Bookmarks

Posting Permissions

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