It is currently Mon May 28, 2018 1:49 am

Need help with script that writes user input to file

All times are UTC - 6 hours

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Thu Jan 07, 2010 11:31 pm   

Joined: Wed May 30, 2007 9:22 pm
Posts: 39
Location: California
I posted this in the Sed/Awk forum yesterday and wound up completely confused and couldn't get it working. I'm now wondering if sed is really what I need for this. Here's what I'm trying to do:

I have an xml file called playlist.xml the contents look like this:

charset utf-8
     <title>01-07-10 - First Show of the New Year!</title>

     <title>12-25-09 - Christmas Special!</title>


I am trying to write a script that asks the user what the path is and what the title is and then writes the necessary change to the xml file. The changes have to start on line 3 of playlist.xml. The script so far looks like this:



read -p "What is the path of the show? " showpath;

read -p "What is the title of the show? " showtitle;

sed -i 3i\
'\<track>\<path>'"$showpath"'\</path>\</title>'"$showtitle"'\</title>\</track>' $xmlpath

cat $xmlpath;

When I run this, it asks the questions and takes my input, but then I get an error saying that -i is an invalid option for sed. Do I need sed for this? Is there another way to do it? Any help would be much appreciated!

 PostPosted: Fri Jan 08, 2010 1:06 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 643
-i is a GNU sed option. If you don't have this option, then you're not using GNU sed.

The only solution is to use a temporary file
sed 3i\
'\<track>\<path>'"$showpath"'\</path>\</title>'"$showtitle"'\</title>\</track>' $xmlpath > /tmp/${xmlpath##*/}.copy
mv /tmp/${xmlpath##*/}.copy $xmlpath
where ${xmlpath##*/} is Parameter Expansion to get xml_file's basename.

Last edited by Watael on Thu Mar 04, 2010 7:15 pm, edited 1 time in total.

 PostPosted: Wed Mar 03, 2010 5:17 pm   

Joined: Wed Mar 03, 2010 5:08 pm
Posts: 1
#! /bin/bash

#this first echo command makes a file called playlist.xml with
# the utf and xml headers in it.
echo "charset utf-8
" > ~/desktop/playlist.xml

#this bit says until the person types a zero keep asking questions and reading the answers into variables
while [ "$thepath" != "0" ]
echo "*************************************************"
echo "type a ZERO to finish this infernal questioning!"
echo "*************************************************"
echo "What is the PATH of the show dude?"
# the -n at the start  stops echo from adding a return character so that the use can just add to the http:// bit
#if you use /sh not /bash you need to add \\c at the end instead.
echo -n http://
read thepath

#if the text typed in isn't a zero (!= means does not equal)
#then keep asking what the title is

if [ "$thepath" != "0" ]
echo "What is the TITLE of the show huh?"
read theshow
# a fi closes the if statement

# this bit does the body of the text writing the lines to the text file the >> means "add to" the file not overwrite it.
# but only thepath is NOT zero!
if [ "$thepath" != "0" ]

echo "
</track>" >> ~/desktop/playlist.xml

# add the footer bit to close the xml statement and add (>>) to the text file
echo "
</xml>" >> ~/desktop/playlist.xml

# tell people its done!

this looks a bit long winded but totally works on os x snow leopard (i've just tried it)
I like to write this way because i am a simple soul who just likes things to work kindof how they seem they ought to.
my comments in the code are what I do for myself as my brain is full and old bash knowledge stuff keeps getting replaced by new bash stuff.
I've learnt so much from the clever people here (and elsewhere on the interwebs)
this is just another approach to your problem I guess and may help spark your imagination as to where you want to go with an approach.
bash is cool.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: Google [Bot] and 6 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group