Shell Script Error: "Head: Invalid Trailing Option -- 1"

Shell Script error: head: invalid trailing option -- 1

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

Shell Script error: head: invalid trailing option -- 1

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

Bash cat on the last created remote file

Your command is failing is because the backticks in $CMD are expanded locally when you create the variable, rather than being expanded on the remote side. So ssh@XX.XX.XX.XX $CMD is actually going to look something like ssh@XX.XX.XX.XX "cat /mypath/local_file" (and local_file may not exists on the remote host, and is probably not the file you want).

You can prevent this local expansion by providing the command directly to ssh.

ssh user@host 'cat /mypath/$(ls -t /mypath/*.txt | head -1)'

ls returns the pathname relative to the directory so you will also need to include the path of the base directory /mypath/ in your cat invocation. To avoid this hardcoding pass the -d flag to ls.

ssh user@host 'cat $(ls -dt /mypath/*.txt | head -1)'

How can I use 'head' in a bash script with a variable?

A quick test here seems to indicate that the problem is that your $LINE variable has trailing spaces (i.e. '5 ' instead of '5').
Try removing them.

$ head '-5g' file
head: invalid trailing option -- g
Try `head --help' for more information.

$ head '-5.' file
head: invalid trailing option -- .
Try `head --help' for more information.

$ head '-5 ' file
head: invalid trailing option --
Try `head --help' for more information.

Cut command including a space which is confusing head command?

Answer to revised question

Replace:

cut -c5-19 filelist | grep -n "$input" | cut -c1-2 > cat

With:

cut -c5-19 filelist | grep -n "$input" | cut -d: -f1 >cat

grep -n places a colon between the line number and the text of the line. So, it is natural to use a colon as field delimiter for cut and ask cut to return the first field.

From what you have shown, the script can be further simplified to:

read -p "Enter your name: " input
linenum=$(cut -c5-19 filelist | grep -n "$input" | cut -d: -f1)
head -$linenum filelist | tail -1 > filelist2

Answer to original question

Since you haven't shown us filelist, we can only guess at the problem. If you are right about filelist containing spaces, then this is the solution. Replace:

head -$cat filelist | tail -1 > filelist2

With:

head -${cat## } filelist | tail -1 > filelist2

The construction ${cat## } removes all leading spaces from the varialbe cat.

How to fix 'script returned exit code 1' when running Jenkins pipeline

I'm surprised that more people are looking for this problem than I think.

Use set +e if you intend to ignore the error code exit 1 of code run as a shell script.



Related Topics



Leave a reply



Submit