Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Mostly agree, but I add more.

1. end all your lines C-style; this may save your life many times;

2. declare -is variables and -r CONSTANTS at the beginning, again, C-style;

3. print TIMESTAMP="$(date +%Y-%m-%d\ %H:%M:%S)"; where appropriate if your script logs its job;

4. Contrary to OPs reommendation I strongly try to stick to pure SH compatibility in smaller acripts so they can run on routers, TVs, androids and other busybox-like devices; BASH isn't everywhere.



I like to use:

    date -u +%Y-%m-%dT%TZ
because the time zone is unambiguous, the command works with POSIX date, and it's valid under both ISO 8601 and RFC 3339.


how do you make sure your scripts are SH compatible?


Make sure? Well, aside from keeping sh feature subset in my head, I usually run them like "sh myscript.sh" on target or limited environment (they have #!/bin/sh shebang) for testing. Other people here probable have better suggestions though. )




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: