Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Unix Shell script

Posted on 2007-12-02
7
Medium Priority
?
1,140 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
Industry Leaders: 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

609 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