Solved

Basic Unix help

Posted on 2007-12-05
15
356 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Samsung memory install did'nt recognize by new hardware 5 28
AIX  NFS  question 1 138
Using Grep to Find a file 8 93
mobaxterm not able to change directory 28 157
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
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…
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…
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…

831 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