0

I am trying to create a file which is manually created by opening a vi editor then Esc + i and then you paste a column of entries to it and then Esc :wq!, I don't want user to even open vi editor, the script should as to enter list of data and it should create the file and add the entries to it.

count=1
read total
while [ "$count" -le "$total" ]; do read -p Name  ; echo $Name > /tmp/cl; count=$(($count + 1)); done

But it doesn't work as it expects an Enter after each input which is not there when I paste an output at the first "Name" prompt, moreover I don't want to even enter the total no of names.

Sid
  • 161
  • 1
  • 10

2 Answers2

0

Well instead of writing a script you can directly paste the output into the file using below :

    cat > filename 
    Right Click 

    Ctlr+C
Nikhil Fadnis
  • 787
  • 5
  • 14
  • This won't work either as when I cat > filename, it will prompt me for next command if i type Paste command and then do a right click to paste the copied data it pushes, the Paste as text also goes to the file. – Sid May 18 '17 at 12:20
  • Updated the answer... The previous answer was not that clear. – Nikhil Fadnis May 19 '17 at 09:19
0

Make a rm line to purge the file content before run may you not need it. Since you not mentioned the shell variant I prefer use sh in this case.

The problem with your script maybe you use > operator insted of >> . > purge the file and put the text into it. and at the next loop it purge again and put the variable content into again.

I used for instead of while because it loot faster, and code is smaller.

#!/bin/sh

read -p "Total: " total    # Input for loop cycle count
rm ./temp.txt               # delete old file before run
for (( i=1 ; i <= $total ; i++ ))
do
    read -p "Name: " Name       # get name
        echo $Name >> ./temp.txt   # put it into file named temp.txt
done

Then this is the one you looking for, Will exit when you input empty line without adding the empty line to the end of the file. Just copy paste and hit enter once or twice depend about the input. Unfortunately if you copy paste content where two section separated by empty line this will exit also there.

#!/bin/sh

rm ./temp.txt               # delete old file before run

while read inputline
do
  [[ $inputline == "" ]] && exit 0
  echo "$inputline" >>  ./temp.txt
done
lw0v0wl
  • 664
  • 4
  • 12
  • This didn't work as the problem is somewhere else, in a Unix machine if you just do a right click it dumps all the data copied on the screen, I am trying to use this feature for creating a file in one go. – Sid May 18 '17 at 12:17
  • Sorry, I missed the point of you question. Please check my update. – lw0v0wl May 18 '17 at 12:46