Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
Hi to all.I need a shell script that will save in a file each line I write and then exits,while my file will be stored in my home dir.I have ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    25

    Shell Script help..

    Hi to all.I need a shell script that will save in a file each line I write and then exits,while my file will be stored in my home dir.I have created something,but I must use Ctrl+C to exit.Here is my code:
    Code:
    #!/bin/bash
    read -p "give the name of the file: " NAME
    echo "Write your text"
    echo "press Ctrl+C to save and exit"
    i=1
    while [ true ]
    do
        echo -n "$i  "
        read line
        echo $line>>$NAME
        i=$((i+1))
    done
    This script also counts the number of lines of my text.I could your help...

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    while [true] = forever

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    25
    well,I know this but when I tried to use while with an escape character(ex. press 000 to exit) in the file created there was also a line with the sequence 000.

  4. #4
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    how do you wanna stop the input(i mean there must be a way to tell the shell that you are done typing).
    you can use here-documents(google for it in case) so that you give a special word to tell the shell that you are done typing the file content.
    for example
    Code:
    cat >my_file <<done
    would let you create a file called my_file then it puts everything you type in till you give it the word done.
    Linux and me it's a love story

  5. #5
    Just Joined!
    Join Date
    Jun 2008
    Posts
    25
    Yes.I want to give a special word or symbol so that the input will stop and the word given won't be saved in my file.

  6. #6
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    so you can go for a here-document.
    some googling and you are on track.
    Linux and me it's a love story

  7. #7
    Just Joined!
    Join Date
    Jun 2008
    Posts
    25
    Thanks...it seems I'm finally on track...

  8. #8
    Just Joined!
    Join Date
    Jun 2008
    Posts
    25
    On the contrary it seems I'm not on track as what I want is to input the text via the terminal.Any ideas..?

  9. #9
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    lol.

    what do you mean by input text via the terminal?
    Linux and me it's a love story

  10. #10
    Just Joined!
    Join Date
    Jun 2008
    Posts
    25
    I don't want to write a script that will redirect a text file to another file.I want a script that after the execution will ask for my input and it will save it to a file.What I really want is to find how I can use an escape character when I won't want to write anymore and this character won't be saved in my file.

Page 1 of 2 1 2 LastLast

Posting Permissions

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