I have this code in my shell(bash) script for splitting a file into smaller parts:
for (( i=$start; i<=$lineCount; i=i+$interval))
do
temp=`expr $i + $int
When you put the pipe |
in a variable, the shell interprets it as an ordinary character and not as a pipe. Ditto for redirection operators like >
, <
, ...
An ugly way would be to use eval
.
A better approach would be to split your command into different parts so as to get rid of pipes and redirection operators in it.
For example:
command="head -$temp $fileName | tail -$lastLines > tmpFileName";
would be written as:
cmd1="head -$temp $fileName";
cmd2="tail -$lastLines";
and executed by saying:
"$cmd1" | "$cmd2" > tmpFileName;
Moreover, you don't need backticks to execute a command that is stored in a variable. Simply say:
$command