LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-07-2013, 05:16 PM   #1
tkinsella
Member
 
Registered: Dec 2005
Distribution: CentOS 6.5
Posts: 45

Rep: Reputation: 0
Bash Scripting - Write user input to file


Hey All, I am trying to take user input and write it inside a file I am creating but I keep running into issues. Thoughts on another way to go about this?

Code:
read -p "Enter Text : " user1

cat >>/tmp/someFile.php <<'EOF'
<?PHP
/$var 1 = $user1
?>
EOF
However when I open the file it is displaying exactly what is above.

Thanks for your Help!!!
 
Old 02-07-2013, 05:25 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,404

Rep: Reputation: Disabled
I believe the single quotes around EOF causes everything between that line and EOF to be interpreted literally (without variable expansion).

Try changing line 3 to:
Code:
cat >>/tmp/someFile.php <<EOF
You will also need to escape the first $ in line 5 to keep $var from being expanded to the contents of the (potentially non-existent) variable "$var".

Last edited by Ser Olmy; 02-07-2013 at 05:28 PM.
 
Old 02-07-2013, 05:31 PM   #3
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Rep: Reputation: 2
Does this give you want you want?
Code:
read -p "Enter Text : " user1

echo "<?php 
\$var1 = \"$user1\";
?>" > "test.php"
 
Old 02-08-2013, 01:02 AM   #4
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
this is what you want:

Quote:
#!/bin/bash

read -p "Enter Text : " user1

cat << EOF > try.txt

<?PHP
\$var 1 = $user1
?>
EOF
 
Old 02-08-2013, 01:37 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Why making it complicated? It can be done without using cat, EOF and all that stuff, like:-
Code:
#!/bin/bash
read -p "Enter Text : " user1
echo $user1 >> /tmp/someFile.php
In addition, if you get any error in a script, add set for debugging, so when you invoke script, it can show execution of each line and any error(s).
Code:
#!/bin/bash
set -xv
(Rest script)
 
Old 02-08-2013, 09:08 AM   #6
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
[QUOTE=shivaa;4886797]Why making it complicated? It can be done without using cat, EOF and all that stuff, like:-
Code:
#!/bin/bash
read -p "Enter Text : " user1
echo $user1 >> /tmp/someFile.php
This doesnt do what the OP wants,re-read the question
 
Old 02-08-2013, 09:16 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by tkinsella View Post
Hey All, I am trying to take user input and write it inside a file I am creating but I keep running into issues. Thoughts on another way to go about this?
@cbtshare:
What other sense does it make?
 
Old 02-08-2013, 10:19 PM   #8
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
you left this part out

Quote:
<?PHP
\$var 1 = $user1
?>
 
Old 02-09-2013, 03:06 AM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by cbtshare View Post
you left this part out
Alright. I don't know what this part is. But initially I though that OP wanted to take input from user only once and write it to a file.

Anyway, if OP wants to take input repeatedly, then a loop can do that like:-
Code:
#!/bin/bash
while true
do
read -p "Enter some text: " text
echo $text >> /path/to/outputfile.txt
done
And use CTRL+C to exit from it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux Scripting with user input and outputs data from csv file... Deke602 Linux - Newbie 2 10-11-2012 05:49 PM
Bash scripting with sudo and password prompt, and accepting user input. Jetso Programming 14 07-31-2011 01:31 PM
Bash Scripting handling user input sections automatically Gavin Harper Programming 3 06-18-2010 11:35 PM
bash scripting... taking user input and adding it to a particular area of a file... dagummit Programming 7 01-13-2010 01:07 AM
Bash scripting and user input Woodsman Slackware 13 11-02-2005 03:20 PM


All times are GMT -5. The time now is 09:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration