4

I've been trying to create a script that can read a certain line off of a file given some variables I've created.

SCRIPTNUM=$(tail -1 leet.txt)
LINE=$(echo $SCRIPTNUM | python leetSolver.py)
PART1=$(head "-$LINE" leet.txt)
FLAG=$(printf "$PART1" | tail -1)
FLAGFORMAT="$FLAG\n"
printf $FLAGFORMAT

From this the biggest problem I face is that I get this error:

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

The code works just fine when inputted through the terminal one line at a time. Is there a way to make this code work? It's worth noting that using a constant (ie head -5) works.

Klaus
  • 538
  • 8
  • 26
user3892426
  • 43
  • 1
  • 1
  • 3

2 Answers2

1

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.
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • I tried commands to remove the whitespace, but they didn't seem to do anything in the script. Something to note, I tried replacing the echos with printf, but that doesn't seem to do anything. LINE=$(echo $SCRIPTNUM | python leetSolver.py | tr -d '\r\n') printf "$LINE" – user3892426 Jul 30 '14 at 17:31
  • The echo is not the problem. The output from the python script is. What commands did you try exactly? Any of the ones in @MarkSetchell's answer should work for this I think. What output do you get if you add `echo "$LINE" | xxd` after the assignment? – Etan Reisner Jul 30 '14 at 17:33
  • Um... What did you try exactly to get that? I literally meant insert that line exactly as I wrote it in your script after the `LINE=...` line. If that's what you did then your script uses dos line-endings and that might be part of your problem. – Etan Reisner Jul 30 '14 at 17:41
  • I just added it directly underneath the assignment. LINE=$(echo $SCRIPTNUM | python leetSolver.py | tr -cd '[:digit:]' ) echo "$LINE" | xxd – user3892426 Jul 30 '14 at 17:43
  • Try running your script through `dos2unix` or similar tool to convert it to unix line endings and try it again. – Etan Reisner Jul 30 '14 at 17:46
  • That worked to fix the head issue! There are some other formatting issues, but I can fix those. Thank you! – user3892426 Jul 30 '14 at 17:49
0

Change this line

LINE=$(echo $SCRIPTNUM | python leetSolver.py)

to

LINE=$(echo $SCRIPTNUM | python leetSolver.py | tr -d '\r\n ')

that will remove any trailing line feeds or carriage returns or spaces.

Or, if you prefer sed

LINE=$(echo $SCRIPTNUM | python leetSolver.py | sed 's/[^0-9]//g' )

Or, if you like tr

LINE=$(echo $SCRIPTNUM | python leetSolver.py | tr -cd '[:digit:]' )

will remove all non-digits.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432