Find the answer to your Linux question:
Results 1 to 7 of 7
so i have this file genNumList.sh with this content Code: #! /bin/bash echo starting to generate numbers... cat > numList 0 1 2 3 4 5 6 7 8 9 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    2

    bash script help, trying to write to a file and close it


    so i have this file genNumList.sh with this content

    Code:
    #! /bin/bash
    echo starting to generate numbers...
    
    cat > numList
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ^C
    
    echo "should have created numList file with 0-10 inside"
    when i run this in the shell like so

    # ./genNumList.sh

    i get this output

    Code:
    starting to generate numbers...
    it just sits there. it creates an empty numList file. so it starts but never closes. I'm sure there is a way to emulate Ctrl+C in a script like this but i'm just not sure how to do it

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,456
    Try this instead:
    Code:
    seq 1 10 > numlist
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Mar 2012
    Posts
    15
    For a more generic example, you can do this
    Code:
    echo <string/number> >> filename
    and you can use it on multiple lines.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16

    ending input stream

    I believe your problem is that you used control-C instead of control-D to end the input. Control-C gives the 'cancel' signal, while Control-D gives the 'End of file/End of Input' signal.
    (Note the '^C' at the end of your input.)

    Quote Originally Posted by colfaxrev View Post
    so i have this file genNumList.sh with this content

    Code:
    #! /bin/bash
    echo starting to generate numbers...
    
    cat > numList
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ^C
    
    echo "should have created numList file with 0-10 inside"
    when i run this in the shell like so

    # ./genNumList.sh

    i get this output

    Code:
    starting to generate numbers...
    it just sits there. it creates an empty numList file. so it starts but never closes. I'm sure there is a way to emulate Ctrl+C in a script like this but i'm just not sure how to do it

  6. #5
    Just Joined!
    Join Date
    Sep 2007
    Location
    Silver Spring, MD
    Posts
    95

    Please review

    Code:
    #! /bin/bash
    echo "Starting to generate numbers..."
    
    seq 1 10 > numList
    
    cat numList
    
    echo "Should have created numList file with 0-10 inside"
    I think the first respondent gave you a good answer
    Last edited by tdsan; 09-02-2012 at 06:33 PM.

  7. #6
    Just Joined!
    Join Date
    Sep 2004
    Location
    belgium
    Posts
    10
    Quote Originally Posted by tdsan View Post
    Code:
    #! /bin/bash
    echo "Starting to generate numbers..."
    
    seq 1 10 > numList
    
    cat numList
    
    echo "Should have created numList file with 0-10 inside"
    I think the first respondent gave you a good answer
    The above code will work but if you really want to explictely enumerate what will be written into the file, you can do this :
    Code:
    #! /bin/bash
    echo "Starting to generate numbers..."
    
    cat > numList <<EOF
    1
    2
    3
    4
    5
    6
    ...
    10
    EOF
    
    cat numList
    
    echo "Should have created numList file with 0-10 inside"
    hth

  8. #7
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16
    Or, you could use a 'HERE' document:

    Code:
    echo Starting to generate numbers...
    
    cat <<HERE >numList
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    HERE
    
    echo "should have generated numlist with numbers inside"

Posting Permissions

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