Re: trouble with a script
- From: "Chris F.A. Johnson" <cfajohnson@xxxxxxxxx>
- Date: Wed, 12 Jul 2006 02:20:10 -0400
On 2006-07-12, sterling@xxxxxxxxxxxx wrote:
Chris F.A. Johnson wrote:
On 2006-07-12, Chris F.A. Johnson wrote:
On 2006-07-12, sterling@xxxxxxxxxxxx wrote:
I am a total newb at this so have patience lol.
I am trying to simplify a process that my job requires me to do on our
freeBSD servers by writing a script. We use vi for editing. I am
currently using sh as my scripting shell.
Your snippet of code doesn't look like sh on FreeBSD. You appear to
be using bash.
I am not the administrator so
these are my "constants" if you will. I do have root access, but It is
a sensitive system. I have my admin's blessing however, but he is too
busy to help me at all.
Basically, I want my script to prompt me for two inputs, then append to
a file a predefined block of text, the input, another block of text,
the input again, etc etc. I have run into a few issues however.
One issue is that I want to maintain the formatting present as it is
an XML file, and would like to maintain the user-friendly tabs etc. I
have tried many ways to write a newline character in the script, but it
doesnt seem to work. I tried storing the predefined blocks as
variables, then echo the block then the 'read' variable values then the
block etc,etc, but it ends up stripping the formatting and writing it
to one long line. SO:
I have since moved onto assigning each individual line of predefined
text to a variable. There are 20 lines that I need to constantly yank
and put, so I assigned them to PIECE1, PIECE2 etc etc.
So I am hoping to write a type of loop that does something like this:
counter=0
while [ counter -lt 20 ]; do
echo $PIECE$COUNTER
let COUNTER=COUNTER+1
That will not work with the FreeBSD sh. Use the portable form:
COUNTER=$(( COUNTER + 1 ))
Sorry, that should be:
COUNTER=$(( $COUNTER + 1 ))
The previous one works with all POSIX shells EXCEPT *BSD sh.
done
I want it to write each line (and each line is it's own PIECE). I am
planning on having the areas I want to be user inputs be written as,
say, USERINPUT1 and USERINPUT2, then do a find/replace for each and
write the 'read' inputs. I am positive this is probably the most
cumbersome way of doing this though. Any thoughts or suggestions would
be greatly appreciated. Also, the PIECEs are going to contain some
somewhat funky characters. Below is a sample of what I need written.
Sensitive information has been changed.
<VirtualHost 4.1.1.1:80>
ServerName www.domain.com
JkMount /sb/* ajp13
RewriteEngine on
RewriteRule ^/$ /sb/deux.jsp [L,PT]
RewriteRule ^/sb/(.*) /sb/$1 [L,PT]
RewriteRule ^/(.*) /sb/$1 [L,PT]
</VirtualHost>
Is that the input or the output? What is the other?
To read a line and preserve all whitespace and funky characters:
IFS= read -r line
If you want more help, you will have to be clearer about what you
want.
First, thank you for your response.
You're welcome, but please don't top post.
The example at the end (<VirtualHost 4.1.1.1:80>...</VirtualHost>) is
what I need the end output to be. The user input would be "domain.com",
no www. Basically, I have a starting point, as I am also having it back
up the file before editing with a timestamp. But I need it to append
the whole VirtualHost section with all formatting intact, while also
allowing for the "domain.com" portion to be a user-defined input. This
is how we set up a client's site on our servers. So, in abstract form:
-get user input
write block of code(
<newline character if possible>
<VirtualHost 4.1.1.1:80>
ServerName
)
-write user input
-write remainder of code(
JkMount /sb/* ajp13
RewriteEngine on
RewriteRule ^/$ /sb/deux.jsp [L,PT]
RewriteRule ^/sb/(.*) /sb/$1 [L,PT]
RewriteRule ^/(.*) /sb/$1 [L,PT]
</VirtualHost>
)
and all this appended to the end of the file.
IFS= read -r user_input
printf "%s\n" "" "<VirtualHost 4.1.1.1:80>" " ServerName" \
"$user_input" \
" JkMount /sb/* ajp13
RewriteEngine on
RewriteRule ^/$ /sb/deux.jsp [L,PT]
RewriteRule ^/sb/(.*) /sb/$1 [L,PT]
RewriteRule ^/(.*) /sb/$1 [L,PT]
</VirtualHost>" >> FILE
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.
- Follow-Ups:
- Re: trouble with a script
- From: sterling
- Re: trouble with a script
- References:
- trouble with a script
- From: sterling
- Re: trouble with a script
- From: Chris F.A. Johnson
- Re: trouble with a script
- From: Chris F.A. Johnson
- Re: trouble with a script
- From: sterling
- trouble with a script
- Prev by Date: Re: A question on file descriptors
- Next by Date: Re: while displaying a file using tail command, I need to cut one word, how to do?
- Previous by thread: Re: trouble with a script
- Next by thread: Re: trouble with a script
- Index(es):
Relevant Pages
|
Loading