Solved

Unix Shell script

Posted on 2007-12-02
7
1,096 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
Comment Utility
Is this an assignment question?
0
 
LVL 51

Expert Comment

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

Author Comment

by:axtur
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 51

Expert Comment

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

Author Comment

by:axtur
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

771 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

14 Experts available now in Live!

Get 1:1 Help Now