0

As a part of my bash routine I am printing some message in terminal regarding status of the workflow. The message in splited into two parts (part 1: begining of the task, part 2: status of its finishing)

echo -n "Dataset is being processed ! "; execution of some AWK script; echo " Processing has been COMPLETED!"

Here is realisation in bash contained a part of the AWK code:

# print pharase 1: initiation of the process
echo -n "Dataset is being rescored.. Please wait"; sleep 0.5 
# this is the process: makedir for the result and execute AWK code to process input file
mkdir ${results}
# Apply the following AWK code on the directory contained input file
while read -r d; do
awk -F, '
}'  "${d}_"*/target_file.csv > "${results}/"${d%%_*}".csv"
done < <(find . -maxdepth 1 -type d -name '*_*_*' | awk -F '[_/]' '!seen[$2]++ {print $2}')
# print pharase 2: finish of the result, which would appear in the terminal near phrase 1
# this will print "COMPLETED" letter-by-letter with the pause of 0.2 sec between each letter
echo -n " C"; sleep 0.2; echo -n "O"; sleep 0.2; echo -n "M"; sleep 0.2; echo -n "P"; sleep 0.2; echo -n "L"; echo -n "E"; sleep 0.2; echo -n "T"; echo -n "E"; sleep 0.2; echo "D!"

While executing this script in bash, everything seems to be OK and I have not noticed any problems related to the parts of the code between both 'echo -n' blocks. May such splitting of the status phrase using "echo -n" lead to some bugs of the routine in bash ?

Hot JAMS
  • 197
  • 2
  • 6
  • Do you have anything specific in mind that could be a problem? By definition such status messages are always split since they occur as/when things happen in your script. You should be printing them to stderr instead of stdout though (add >&2s) and the usual advice is to use printf instead of echo for any printing (see https://unix.stackexchange.com/a/65819/133219). – Ed Morton Apr 29 '21 at 13:47
  • 1
    Are you planning to annoy only yourself with those added sleep delays? or other people? because I guarantee you that no matter how cool you think they are, other users will hate them (and you) for costing them valuable seconds, and the more often they have to run the script the greater their hate will grow. 300bps modems were not fun, and 0.2 seconds per char is ~5 times slower. If you insist on doing it, at least write a function. eg. slowprint () { for c in "$@" ; printf "%s" "$c" ; sleep 0.2 ; done ; printf "\n" ; }. Then you can run slowprint C O M A T O S E whenever you want. – cas Apr 29 '21 at 14:55
  • bravo, thank you very much very cool sollution! but there is a bug somewhere: ./rmyscript.sh: line 107: syntax error near unexpected token `printf' Also should I add somewhere there >&2 (according to the previous comment) in order to send the message to stderr instead of stdout ? – Hot JAMS Apr 29 '21 at 15:19
  • slowprint () { for c in "$@" ; do printf >&2 "%s" "$c" ; sleep 0.2 ; done ; printf >&2 "\n" ; } – Hot JAMS Apr 29 '21 at 15:36
  • I suppose it should be like this! – Hot JAMS Apr 29 '21 at 15:36
  • Btw I’ve just noticed that there is bug with this printing function when you use it with * characters like slow print * * * *, in that case it print the names of the directiries processes by ask part if my bash script ;-) – Hot JAMS Apr 29 '21 at 16:19
  • 2
    it's not a bug in the function, it's a bug in your use of it. Use single-quotes around asterisks and other shell meta-characters, to prevent the shell from expanding them. e.g. slowprint '*' '*' '*' '*' '*'. and, yes, i forgot the do in the for loop, your version fixed it. – cas Apr 29 '21 at 17:10
  • 3
    Always paste your script into https://shellcheck.net, a syntax checker, or install shellcheck locally. Make using shellcheck part of your development process. – waltinator Apr 29 '21 at 17:19
  • Right , thank you very much again ! Cheers – Hot JAMS Apr 29 '21 at 18:10

0 Answers0