Solved

Basic Unix help

Posted on 2007-12-05
15
338 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
  • 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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 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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now