DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 1 Week Ago
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,011
Default Pasting multiple lines in a shell script.

Challenge: Write a shell script to accept multiple lines of copied text and store these text lines in a shell variable.

Example text to be copied and pasted:
Code:
Europe
=======

Amsterdam
Brussels
Copenhagen

Africa
======

Tunis
Cairo
The empty lines must be preserved! Only exception are the lines after 'Cairo', these should not be copied and thus also not pasted into the variable.

I wrote a script myself, but wonder how somebody else would do it. After at least one person submitted a script, I will share mine here too.
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #2   (View Single Post)  
Old 1 Week Ago
Head_on_a_Stick's Avatar
Head_on_a_Stick Head_on_a_Stick is offline
Real Name: Matthew
Bloaty McBloatFace
 
Join Date: Dec 2015
Location: London
Posts: 387
Default

Code:
#!/bin/sh
echo 'Paste text, use <ctrl>+D to indicate the end of the text'
variable=$(cat)
echo "$variable"
EDIT: useful use of cat

Last edited by Head_on_a_Stick; 1 Week Ago at 07:37 PM. Reason: added joke.
Reply With Quote
  #3   (View Single Post)  
Old 3 Days Ago
Head_on_a_Stick's Avatar
Head_on_a_Stick Head_on_a_Stick is offline
Real Name: Matthew
Bloaty McBloatFace
 
Join Date: Dec 2015
Location: London
Posts: 387
Default

Bump!

C'mon J65nko — I've shown you mine so can I see yours? :P

Did you also use cat? The only other way I could think of was to use bash with GNU's non-standard read -d extension to set the null character as a delimiter with IFS cleared but that's just nasty...
Reply With Quote
  #4   (View Single Post)  
Old 2 Days Ago
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,011
Default

No I did not use cat. Your use of cat is very clever, but I just did not think of it because I was refining my script that used read.

I am writing a little script to automate entries for the forum News section. I use the shell read to prompt for the title, url and quote.

I did not immediately realize that some news quotes were multiple lines. so I replaced the single read with a shell function that uses multiple reads. In other words I stayed within the limits of my self-imposed programming paradigm

Code:
#!/bin/sh

pastetext() {
    echo Paste text. To end paste, just type a '!' as only text at begin of line .....

    while read LINE ; do
      if [ "x$LINE" = "x!" ] ; then break ; fi
      if [ "x$TEXT" = "x" ]; then
           TEXT="$LINE"
      else     
           TEXT="$TEXT\n$LINE"
      fi
    done
}

cat <<END
Make News for daemonforums.org
------------------------------------------------------------------------
END



printf "\nURL of article/source : " 
read URL

printf "\nTitle : " 
read TITLE

printf "\nQuotation or description  : "
pastetext

echo ================= TEXT to paste =============
echo $TITLE
printf "From [url]$URL[/url]:[quote]$TEXT[/quote]\n"
echo ================= END OF TEXT ===============

# end of script
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ask for a shell script Simon Programming 5 27th April 2010 01:07 AM
incrementing within a shell script? spiderpig Programming 5 29th September 2008 08:12 PM
Shell Script. bsdnewbie999 Programming 21 15th July 2008 07:54 AM
shell script with input c0mrade Programming 5 13th July 2008 04:33 AM
Adding multiple lines into file c0mrade Programming 4 7th June 2008 11:03 PM


All times are GMT. The time now is 10:35 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2023, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick