We help IT Professionals succeed at work.

Script Problem!!

FirstMan
FirstMan asked
on
247 Views
Last Modified: 2010-04-21
Hi,

I have a question. i want to create a script called 'firstline' which contains two arguments. the first argument being a line of text and the second argument being a newly created file. The main purpose of script is to take the first argument (a line of text) and insert it into the top line in the second argument.

I want to achieve this in BASH using 'echo'

I have entered some script and it doesnt seem to work

file=$HOME/file1

{read $1

} < $2

echo "this first line in $2 is "$1".

can you help??
Comment
Watch Question

Commented:
file="./file.txt"
firstLine=`head -1 $file`
echo "this first line in $file is: "$firstLine

Commented:
echo 'line' | cat - file

Author

Commented:
ok i have viewed the solution and they don't work properly. i need to write a shell script for firstline which has the 2 arguments.

Commented:
You description is not whole lot clear.  Do you want to pass two arguments when you actually run the script? or do you want to ask user for input in the script itself?  

1. To pass line and the argument while executing the script you can do something like this
echo $1 > $2

Here first argument is a line and second argument is a file to be inserted this line into

2. To read these arguments from the user you can do this
echo "Please enter the line"
read line

echo "please enter file name"
read fileName

echo $line > fileName
CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

Try

echo "Enter new line of text:"

read line

echo "Enter file name:"

read file

if test -f $file
then
       echo $line > temp.$$
       cat $file >> temp.$$
       mv temp.$$ $file
else
       echo $file does not exists  or is not a file
fi

Please note that is it difficult to take the line and file name from the arguments since line words are
separated by space which is the delimiter between shell arguments. But it is not impossible!
CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

Try this script

if test $# -gt 1
then
        num=$#
#       line=$1
        while test $num -gt 1
        do
                line=$line" "$1
                shift
                num=`expr $num - 1`
        done
        if test -f $1
        then
               echo $line > temp.$$
               cat $1 >> temp.$$
               mv temp.$$ $1
       else
              echo $1 does not exists  or is not a file
       fi
fi

Author

Commented:
my friend you have done it again, thank you.

Now suppose that i want to insert the first argument into the middle line.

How will i go about that?
CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

Do you mean in the middle of the file?

I will try something and then come back to you.
CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

Try

if test $# -gt 1
then
        num=$#
#       line=$1
        while test $num -gt 1
        do
                line=$line" "$1
                shift
                num=`expr $num - 1`
        done
        if test -f $1
        then
               len=`wc -l < $1`
               num=`expr $len / 2`
               ed omar1 << END
$num
a
$line
.
w
q
END              
       else
              echo $1 does not exists  or is not a file
       fi
fi

Note: Please do not try to indent the lines above, run as is

CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

Typo

Replace omar1 by $1

Author

Commented:
ok i have tried this script over and over again

echo "Enter new line of text:"
read line

echo "Enter file name:"
read file

if test $# -gt 1
then
        num=$#
#       line=$1
        while test $num -gt 1
        do
                line=$line" "$1
                shift
                num=`expr $num - 1`
        done
        if test -f $1
        then
               len=`wc -l < $1`
               num=`expr $len / 2`
               ed $1 << END
$num
a
$line
.
w
q
END
       else
              echo $1 does not exists  or is not a file
       fi
fi
and get

'enter new line'

'enter file'

and then this

'middleline: line 35: syntax error: unexpected end of file'
 
and if i run the script nothing happens to the file no insert line in the middle
CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

Why you are mixing the two solutions!

The second solution I provided needs you to provide the line and filename as arguments to the script.

e.g.

./middleline this line will be added in the middle mytextfile

here the line is "this line will be added in the middle" and will be added to to the middle of the file mytextfile

So,

Try the script:

if test $# -gt 1
then
        num=$#
        while test $num -gt 1
        do
                line=$line" "$1
                shift
                num=`expr $num - 1`
        done
        if test -f $1
        then
               len=`wc -l < $1`
               num=`expr $len / 2`
               ed $1 << END
$num
a
$line
.
w
q
END
       else
              echo $1 does not exists  or is not a file
       fi
fi

Author

Commented:
I have entered this code and there is still no difference in the outcome.

#!bin/bash

echo "Enter new line of text in the middle:"
read middleline

echo "Enter file name:"
read file

if test $# -gt 1
then
        num=$#
        while test $num -gt 1
        do
                line=$line" "$1
                shift
                num=`expr $num - 1`
        done
        if test -f $1
        then
               len=`wc -l < $1`
               num=`expr $len / 2`
               ed $1 << END
$num
a
$line

END
       else
              echo $1 does not exists  or is not a file
       fi
fi

where am i going wrong?? do i need to add the number of lines in the mytextfile to this shell script??
CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

FirstMan for get about this script that you keep posting. Please see my previous comment.

You need to follow that script, and the way to run it is also described.

CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi,

Thank you for your help.

I want to reward you with my points ,but is there a way to reward you the points and you can still help me with some more questions??

CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

Keep asking and EE will keep answering you, including me  :)

When you close any question, still you can post comments, so I can reply to you.

Author

Commented:
ok cool :). I will post my question in a few minutes

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.