Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1147
  • Last Modified:

Unix Shell script

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
axtur
Asked:
axtur
  • 3
  • 2
  • 2
1 Solution
 
TintinCommented:
Is this an assignment question?
0
 
ahoffmannCommented:
please post what you`ve done so far and tell us where you have problems
0
 
axturAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ahoffmannCommented:
for i in "$*"; do
  cp "$i" ABC/
done
0
 
axturAuthor Commented:
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
 
ahoffmannCommented:
for i in "$*"; do
  cp "$i" ABC/"$i"mod
  cat existing-file-with-2-lines >> ABC/"$i"mod
done
0
 
TintinCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now