[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Script Problem!!

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??
0
FirstMan
Asked:
FirstMan
  • 9
  • 6
  • 2
  • +1
1 Solution
 
sanjoozCommented:
file="./file.txt"
firstLine=`head -1 $file`
echo "this first line in $file is: "$firstLine
0
 
mish33Commented:
echo 'line' | cat - file
0
 
FirstManAuthor 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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sanjoozCommented:
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
0
 
omarfaridCommented:
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!
0
 
omarfaridCommented:
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
0
 
FirstManAuthor 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?
0
 
omarfaridCommented:
Hi,

Do you mean in the middle of the file?

I will try something and then come back to you.
0
 
omarfaridCommented:
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

0
 
omarfaridCommented:
Hi,

Typo

Replace omar1 by $1
0
 
FirstManAuthor 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
0
 
omarfaridCommented:
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
0
 
FirstManAuthor 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??
0
 
omarfaridCommented:
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.

0
 
omarfaridCommented:
Hi,

Here again I will describe to you step by step what to do in order for the script to work as you need:

1- Create a file called middleline

To do so use editor like vi

2- Cut and paste the lines below into the new 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
.
w
q
END
       else
              echo $1 does not exists  or is not a file
       fi
fi

3- make the new file executable

chmod +x middleline

4- To add the line

my new line is this one

to the file called myfile

run the script as follows:

./middleline my new line is this one myfile

The above will add the line are needed to the file

I am assuming that you have a text file called myfile and has some text in it already.

 
0
 
FirstManAuthor 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??

0
 
omarfaridCommented:
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.
0
 
FirstManAuthor Commented:
ok cool :). I will post my question in a few minutes
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!

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