2

I made a bash script which can tree a whole directory structure recursively, putting into every directory a HTML file generated by the aha. The script read the list of directories to be indexed. find search for every directory inside the directory and generate the complete list of directories. If directory /media/veracrypt1 contains 50 directories in the hierarchical structure, tree makes a tree of everything in the given directory and "below", write the HTML file with the recorded tree structure inside the directory, then goes down, repeat the action down to the bottom.

I would like the script to be fired on the defined time by the cron. The script works but the colorization does not and the output of the pipe is black on white. I believe it is the result of cron not having access to the LS_COLOR system variable. (This is what I suspect)

How to correct the script to make it produce the desired effect?

The vital fragment of the script:

tree -axC "$file" | aha --title $(basename "${file// /_}") > "$file"/[z9][tree]_$(basename "${file// /_}").html; done

It works also without aha:

tree -axC "$file" > "$file"/[z9][tree]_$(basename "${file// /_}").html; done

but the same problem with the colorization (only in cron) holds.

The text of the full script:

#!/bin/bash



  List_make_R_general=/track/to/location_1.txt
   List_R_gen_general=/track/to/location_2.txt
             cron_log=/track/to/location_3.txt





echo > $cron_log
cat "$List_make_R_general" | while read file; do find "$file" -type d; done | tee "$List_R_gen_general"
 cat "$List_R_gen_general" | while read file; do tree -axC "$file" | aha --title $(basename "${file// /_}") > "$file"/[z9][tree]_$(basename "${file// /_}").html; done
 cat "$List_R_gen_general" | while read file; do tree -axC "$file" -I "*.JPG" | aha --title $(basename "${file// /_}") > "$file"/[z9][tree]_$(basename "${file// /_}")_[excl].html; done
echo -e "Tree done successfully: $(date) \n" >> $cron_log
exit
muru
  • 197,895
  • 55
  • 485
  • 740
Christianus
  • 571
  • 1
  • 3
  • 16

1 Answers1

4

I worked at a similar problem with htop lately, you need to set TERM=xterm in your script:

#!/bin/bash
export TERM=xterm
…

Instead of using export you can also set the variable for every tree invocation directly:

…; do TERM=xterm tree -axC …

The TERM variable tells tree which type of terminal you are using. What probably1 matters in this case is the text window’s capability of displaying color: xterm is built with 8 colors while e.g. xterm-256color – you guessed it – is built with 256 colors. You can get a list of possible values for your system with ls -1 /lib/terminfo/x and view and compare their capabilities with infocmp, e.g.

infocmp xterm                              # view capabilities
infocmp xterm xterm-256color               # compare
infocmp xterm xterm-256color | grep colors # compare only colors

1 As noted in the comments tree actually just tests for TERM to be set to anything at all, so TERM=my_precious tree works as well. Giving it a valid value seems a good idea though.

Further reading:

dessert
  • 39,982