Solved

Unix Shell script

Posted on 2007-12-02
7
1,114 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

730 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