• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

Basic Unix help

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
Mrdogkick
Asked:
Mrdogkick
  • 7
  • 6
  • 2
1 Solution
 
omarfaridCommented:
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
 
MrdogkickAuthor Commented:
do u think u could explain what u have done there a little?
0
 
omarfaridCommented:
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!

 
MrdogkickAuthor Commented:
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
 
omarfaridCommented:
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
 
MrdogkickAuthor Commented:
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
 
MrdogkickAuthor Commented:
does this mean I need to change the paths somewhere?
0
 
omarfaridCommented:
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
 
MrdogkickAuthor Commented:
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
 
omarfaridCommented:
The file name does not change
and the line is added on top of the file
0
 
MrdogkickAuthor Commented:
doh, thanks very much for your help mate!
0
 
omarfaridCommented:
Welcome :)
0
 
TintinCommented:
A more basic solution would be:

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

0
 
omarfaridCommented:
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
 
TintinCommented:
My solution is based purely on the original question.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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