Friday, January 30, 2015

Some lessons I learnt while scripting using Perl and Bash

1. Use '>' instead of '&>' by default for output redirection. While executing commands in Perl scripts, it is not wise to redirect errors/output to a file with &> operator. Errors must be thrown back to the calling program. &> should only be used for logging the output of the root/main command because when we close the terminal we still want to program to print the errors to the same file.
2. Avoid using 'rm -rf ' because it is a dangerous command. Instead use 'rm -f folder/*'. If 'rm -rf $abc' is needed, make sure that $abc has some literals and is not totally driven by used input. In case the user input is null, the rm -rf command can turn into 'rm -rf /'.
3. Export bash scripts (eg. into the job folder and execute them from there. This allows to run only that job during debugging and testing.
4. Unlike Bash scripting, Perl scripts can be edited while the program is running. Sadly, without even knowing that the script is running, I have edited bash scripts many times causing the running program to fail. For this reason, I stopped bash scripting totally. I use Perl for almost everything now.
5. Somewhere I read that for readability using underscore instead of capitalization or hyphens is better. This practice has been useful to me.
6. Use 2 instead of 'to' in identifiers. For example tbl2chimera, rr2tbl, etc.