Reputation: 141110
I want to put TextA to the beginning of TextB by
cat TextA A TextB
The problem is that I do not know how to refer to the first and second parameters, that is TextA and TextB in the following script called A:
#!/bin/bash
cat TextA > m1
cat TextB > m2
cat m1 m2 > TextB
where m1 and m2 are temporary files.
How can you refer to the two files in the shell script?
Upvotes: 1
Views: 212
Reputation: 141110
I am surprised that nobody suggests the following result
cat TextA TextB | tee > TextB
This way, you can avoid the hassle of creating a temporary file.
Upvotes: 1
Reputation: 24926
I would do the following:
#!/bin/bash
if [ $# -ne 2 ]
then
echo "Prepend file with copyright notice"
echo "Usage: `basename $0` <copyright-file> <mainfile>"
exit 1
fi
copyright=$1
mainfile=$2
cat $mainfile > /tmp/m.$$
cat $copyright /tmp/m.$$ > $mainfile
#cleanup temporary files
rm /tmp/m.$$ /tmp/m2.$$
Upvotes: 1
Reputation: 1608
In a bash script is the first parameter is $1, the second is $2 and so on.
If you want a default value for example the third parameter you can use:
var=${3:-"default"}
Upvotes: 3
Reputation: 84149
Looks like you can just do the following:
TextA="text a"
TextB="text b"
echo "$TextA $TextB" > file1
Or use the append (>>) operator.
Upvotes: 0
Reputation: 127428
You can use $0
, $1
, $2
etc. to refer to the variables in the script.
$0
is the name of the script itself
$1
is the first parameter
$2
is the second parameter
and so on
For instance, if you have this command:
a A1 A2
Then inside a
you'll have:
$0 = a
$1 = A1
$2 = A2
Upvotes: 4
Reputation: 2715
you could just use append (>>)
cat TextB >> TextA
result is that TextA precedes text TextB in TextA
Upvotes: 2