FirstMan
asked on
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??
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??
echo 'line' | cat - file
ASKER
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.
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
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
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!
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!
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
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
ASKER
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?
Now suppose that i want to insert the first argument into the middle line.
How will i go about that?
Hi,
Do you mean in the middle of the file?
I will try something and then come back to you.
Do you mean in the middle of the file?
I will try something and then come back to you.
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
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
Hi,
Typo
Replace omar1 by $1
Typo
Replace omar1 by $1
ASKER
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
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
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
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
ASKER
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??
#!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??
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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??
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??
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.
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.
ASKER
ok cool :). I will post my question in a few minutes
firstLine=`head -1 $file`
echo "this first line in $file is: "$firstLine