Solved

Append input to file from script

Posted on 2008-06-24
7
294 Views
Last Modified: 2010-04-21
Previously you all helped me create a script to add a user with some custom home folder stipulations. Now I'd like to modify the script to append $2 to /etc/ftpusers.

I've commented out the line I hoped would work for this purpose, but it overwrites the ftpusers file, instead of appending. Do I need to use cat instead?

Thanks!

****

mkuser:
if test $# -eq 0
then
      echo "Usage: ./mkuser firstname_lastname_location userid"
      exit 1
fi
#echo $2 >> /etc/ftpd/ftpusers
useradd -d /usr/igi/avante95/flodata/live/LIVE.DATA -c $1 $2
0
Comment
Question by:crcevans
  • 3
  • 2
  • 2
7 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 125 total points
ID: 21857510

echo $2 >> /etc/ftpd/ftpusers

Should (does) NOT overwrite the file.
0
 

Author Comment

by:crcevans
ID: 21857645
Now I have the following:

****
echo $2 >> /etc/ftpd/ftpusers
useradd -d /usr/igi/avante95/flodata/live/LIVE.DATA -c $1 $2
****
I need $2 on a new line in ftpusers, instead of appended to the previous line. How do I do that?
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 21858819

Try:
echo -e "\n$2" >> /etc/ftpd/ftpusers


0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:crcevans
ID: 21859328
For whatever reason, the original echo statement is what is working.

Thanks for your help!

****final script****
# more /home/UN/mkt
if test $# -eq 0
then
      echo "Usage: ./mkuser firstname_lastname_location userid"
      exit 1
fi
echo $2 >> /etc/ftpd/ftpusers
useradd -d /usr/igi/avante95/flodata/live/LIVE.DATA -c $1 $2
0
 
LVL 16

Expert Comment

by:Hanno Schröder
ID: 21863297
Usually, the original file should have a newline (\n) at the end of it's
last line. Therefore, when you add something using
   echo "some stuff to add" >> myfile
it will get appended as another line, including another newline at
the end.
0
 

Author Comment

by:crcevans
ID: 21865190
Ah, so perhaps the fact that I edited the file in a windows editor and then FTPd it over is the reason it didn't have the /n on that last line the first time I tried your suggestion?
0
 
LVL 16

Expert Comment

by:Hanno Schröder
ID: 21865712
Windows (DOS) and Unix have different line endings:
- Unix has a newline (\n) at the end
- DOS has carriage return and newline at the end (\r\n)

If you transfer files from DOS (windows) to Unix using FTP in ASCII (or text)
mode, it will do the conversion for you.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now