Shell Script error: “head: invalid trailing option — 1”

后端 未结 3 543
旧时难觅i
旧时难觅i 2021-01-27 02:04

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         


        
3条回答
  •  再見小時候
    2021-01-27 02:30

    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
    

提交回复
热议问题