简体   繁体   中英

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!

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).

Why not combine both? mkdir -p /directories/.... && touch /directories/file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM