Solved

Basic Unix help

Posted on 2007-12-05
15
368 Views
Last Modified: 2010-04-21
Hello all,
I am attempting to teach myself Unix from a book and have come upon my first script question. How would you go about this?? (SED and AWK are NOT to be used)

Pre-requisites
Create a file with x amount of lines in it, the content of your choice.

Write a script named 'firstline' that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the very top (the first line) of the file named in your second argument.
0
Comment
Question by:Mrdogkick
[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
  • 7
  • 6
  • 2
15 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 20412047
Try the following

count=$#
line=$1
while test $count -ge 2
do
   shift
   line=$line" "$1
   count=`expr $count - 1`
done
echo $line > /tmp/f.$$
if test -f $1
then
      cat $1 >> /tmp/f.$$
fi
mv /tmp/f.$$ $1
0
 

Author Comment

by:Mrdogkick
ID: 20412157
do u think u could explain what u have done there a little?
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20412201
ok

$# is the number or args passed to the script
so $# -1 args are the line and the last arg is the file name
so I keep constructing the line and shifting the args to the left
once I reach to the last arg which will be in $1 I create a temp file with line at the top (1st line) then I check if the file exists and if exits I concatenate to the end of temp file then I move temp file to the file name given.
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!

 

Author Comment

by:Mrdogkick
ID: 20413206
so i made a file called lines with numerous amounts of lines in it. Then I madea file called firstLine to put the script in. Looking at the script, I can not see where it indicates the name of the text file (lines)
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20413266
OK,

Try this create a file call it myfile that contain 10 lines (any 10 lines)
now put the code posted earlier in a script call it myscript
make the script executable: chmod +x myscript
run it as

./myscript this text will be added to myfile

nof if you cat myfile you will see 11 lines with " this text will be added to" as the fist line
0
 

Author Comment

by:Mrdogkick
ID: 20413310
I did as you said and ran the script and typed in hello! the did ctrl d to end it and got the following error

 mv: missing destination file operand after `/tmp/f.8066'
> Try `mv --help' for more information
0
 

Author Comment

by:Mrdogkick
ID: 20413317
does this mean I need to change the paths somewhere?
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 125 total points
ID: 20413438
Hi,

I have modified the script a little bet:

count=$#
line=$1
while test $count -gt 2
do
   shift
   line=$line" "$1
   count=`expr $count - 1`
done
echo $line > /tmp/f.$$
if test -f $2
then
      cat $2 >> /tmp/f.$$
fi
mv /tmp/f.$$ $2


- put the above code in a file called myscript
- run
chmod +x myscript
- run
./myscript add this text to myfile1

You should see that a file called myfile1 is created and contains

 add this text to

- run

./myscript add this 2nd text line to myfile1

You should see that  myfile1 contains

add this text to
add this 2nd text line to

I could not understand what you mean by "ran the script and typed in hello! the did ctrl d to end it"


0
 

Author Comment

by:Mrdogkick
ID: 20413481
forget what I said about ctrl D, was being stupid!!

I have added the code and it works fine. Only problem is, the text input by the user should be added to the first line of the file myfile (which I have already created and contains lines of text already). The filename should also stay the same, so in effect the script needs to add this line of user input to the begining of 'myfile' and save it.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20413524
The file name does not change
and the line is added on top of the file
0
 

Author Comment

by:Mrdogkick
ID: 20413564
doh, thanks very much for your help mate!
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20413607
Welcome :)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20415082
A more basic solution would be:

#!/bin/bash
(echo "$1";cat $2) >/tmp/$$ && mv /tmp/$$ $2

0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20415134
Hi Tintin  :)

How this will take care of the dynamic number of args where the last one is the file name and the rest are the line to add to the file?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20415494
My solution is based purely on the original question.
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AIX check for filesystems not mounted but mount = true 4 42
Skill Development 15 195
Log File Creation with Header and Footer 17 161
Field name with special character (Ñ) in Oracle 11 141
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.

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