Q:

How to create a file in shell script

I need to write a shell script where I read a variable from environment. If the file pointed by it doesn't exist, I want to create it.
This file path could contain some intermediate non-existent directories, so these also need to be created. So neither mkdir -p works here nor simple touch works here.
What is the workaround?
Thanks!

A:

mkdir -p "`dirname $foo`"
touch "$foo"

dirname works on arbitrary paths; it doesn't check whether the path is in use (whether the file pointed to exists).

