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:



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

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

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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.