Linux - NewbieThis 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

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

1) Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.

2) How do you know that sed is the correct tool to use here? It may indeed be, but it's a good idea not to limit your options when asking how to do something.

3) How about some more details? What is the format of the file, and exactly where does the new text need to be inserted? How is the input point determined, by line number, text pattern, just tacked onto the end, or what?

@273: VB??? I'm pretty sure that will just over complicate everything.

@Cedrik: I'm not really sure what you are saying here...and I'm not really sure you understood my question.

Pretty simple piece of code here.

The first part of my script all I do is create the username, change the ownership, the file permissions. After that is done the script logs into our NIS server. From here there are 3 files that need to be edited. 1. passwd file, 2. domain.map 3. shadow.

So again the 2nd half of my script will look like this:
#######################################
code:
ssh nisserver
cd /var/yp/source
echo -n " please add userline to passwd file and press [ENTER]: "
read $passwd
sed "THIS IS WHERE IM LOOKING FOR HELP WITH THE SYNTAX"
#######################################

So basicly I want what ever the user types in to be added to the passwd file. Now like I said before I did a lot of research on this before I even posted on here and everything says sed is the best tool...and since I have been doing this awhile I would have to agree. Sed is extremely powerful.

The reason for using /code tags is not because we don't know when reading your post which parts are code, but it makes things a lot easier to read. The tags are included for a reason.

As for how you do it, your question is simply not specific enough. We need to know exactly what information you are expecting the user to input and in what format (" please add userline to passwd file and press [ENTER]: " doesn't tell us enough) and we need to know the exact format of the line where the information is expected to be inserted (and the insertion point on that line). sed is indeed very powerful, but it isn't psychic and needs to be told precisely what it's supposed to do.

As already mentioned the straightforward method to append a new line to a file is by means of echo with >> double redirection. To accomplish the same in sed, you need the a command and match the end of file with $

Code:

sed '$a new line' file

However, since you have the user's input stored in a shell variable, you cannot use single quotes (that prevent shell expansion). Using double quote you have to protect the $ pattern from the shell, anyway. Here is an example:

Code:

sed "\$a $passwd" /var/yp/source/passwd

By default sed sends the result to the standard output. In order to edit the file in place, that is to actually change its content, you need the -i option.

Code:

sed -i "\$a $passwd" /var/yp/source/passwd

You can also save a backup copy of the original file, using a custom suffix as argument of the -i option, e.g.

Code:

sed -i.bck "\$a $passwd" /var/yp/source/passwd

Just a final note on your script. The line

Code:

read $passwd

is incorrect. It should be

Code:

read passwd

without the $ sign. This means you have to specify the variable name in the read statement, not its value! Hope this helps.