0

I have a script, where there is a line:

eval for i in \{"$1".."$2"\}\; do [ ! -e "$3"/\$i.\* ] \&\& echo \"\$i\" \;  done \| shuf \| mycommand "$3"

which means: first create a sequence of numbers where no files named after the numbers exist, pipe them to shuf, and then pipe them to mycommand which is an ELF executable.

Most of time the script runs fine, but sometimes it gets segment fault error, i.e. The segment fault error is not reproducible.

$ myscript 0001 734  XMJ

/home/tim/bin/myscript: line 25: 10170 Exit 1                  for i in {0001..734};
do
    [ ! -e XMJ/$i.* ] && echo "$i";
done
     10171 Done                    | shuf
     10172 Segmentation fault      (core dumped) | mycommand XMJ

Does that mean that the segment fault is raised when running shuf?

What can we deduce from the error message and possibly correct it?

Thanks.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Tim
  • 98,580
  • 191
  • 570
  • 977
  • 3
    That's a horrible use of `eval`. Try `for i in $(seq "$1" "$2"); do...` and trade an external process for readability. – roaima Oct 29 '18 at 21:43
  • Thanks. What do you mean by "trade an external process "? Can you be more specific of how to write the script without eval? – Tim Oct 29 '18 at 21:43
  • you need `seq -w` for those leading zeroes (and seq would be the external process, vs. {x..y} being done by bash itself) – frostschutz Oct 29 '18 at 21:47
  • Some people to prefer to use shell builtin commands where possible instead of taking the performance hit of spawning an external command. – glenn jackman Oct 29 '18 at 21:47
  • @Tim: you can probably identify the core dump like so https://stackoverflow.com/a/13308949/2876682 https://stackoverflow.com/a/48956950/2876682 – frostschutz Oct 29 '18 at 21:50
  • @frostschutz I don't know where the core dump file was created? I remember by default of ulimit, no dump created. The segment fault error is not reproducible. – Tim Oct 29 '18 at 21:56
  • 1
    @glennjackman then `i="$1"; while [[ $i -le "$2" ]]... ((i++))` and still avoid that horrible `eval`. – roaima Oct 29 '18 at 22:02
  • 1
    If the message says “(core dumped)” then core really was dumped. – Stephen Kitt Oct 29 '18 at 22:42
  • @StephenKitt what decides whether a process can create a core stump? Where can I look up the dump file? – Tim Oct 29 '18 at 22:48
  • @Tim see [How to view Core file (general)](https://unix.stackexchange.com/q/316348/86440). – Stephen Kitt Oct 30 '18 at 08:12

0 Answers0