Solved

Unix Shell script

Posted on 2007-12-02
7
1,101 Views
Last Modified: 2013-12-20
I need to write a small sample script with the next features, it shouldn't be more thatn 10 lines of code:

I need a program that, for all the files (whose name is given as a parameter), will create a copy of them, changing the last 2 lines of each file for the content of an existing file in the current directory called ABC. The name of the new file should be the same as in the original file, but adding the string "mod" at the end.
0
Comment
Question by:axtur
  • 3
  • 2
  • 2
7 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 20393031
Is this an assignment question?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 20393084
please post what you`ve done so far and tell us where you have problems
0
 

Author Comment

by:axtur
ID: 20397410
This is for a friend who needs help, I should know how to do this, but I've forgotten completely!!

I've done this so far:

for i
do
echo "$i = "
cat $i
done

This code shows on screen the content of the files given as parameters, show the filename before the content of the file.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 20397531
for i in "$*"; do
  cp "$i" ABC/
done
0
 

Author Comment

by:axtur
ID: 20397656
it looks like this won't change the content of the  last two lines of each file and won't rename the file to existing_filenameMOD (concatenate the string "MOD" to the filename), how can I add such features to the existing script?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 20398565
for i in "$*"; do
  cp "$i" ABC/"$i"mod
  cat existing-file-with-2-lines >> ABC/"$i"mod
done
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 20398863
ahoffmann, you've misinterpreted the requirements.  Here's a solution


#!/bin/sh

for i in $*

do

  [ -f $i ] || continue
 

  l=`wc -l <$i`

  l=`expr $l - 1`

  sed $l,\$d $i >${i}mod

  cat ABC >>${i}mod

done

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Utilizing an array to gracefully append to a list of EmailAddresses
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

920 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

16 Experts available now in Live!

Get 1:1 Help Now