写一个文件,如果它存在则追加,否则在bash中创建
发布时间:2020-12-16 01:11:40 所属栏目:安全 来源:网络整理
导读:我试图用变量的内容写一个文件,但如果该文件不存在,请创建它: 到目前为止我得到了这个: echo "$Logstring" $fileLog 我失踪的是因为当文件不存在时会出错. if条件是否必要? #! /bin/bash VAR="something to put in a file" OUT=$1 if [ ! -f "$OUT" ]; th
我试图用变量的内容写一个文件,但如果该文件不存在,请创建它:
到目前为止我得到了这个: echo "$Logstring" >> $fileLog 我失踪的是因为当文件不存在时会出错. if条件是否必要? #! /bin/bash VAR="something to put in a file" OUT=$1 if [ ! -f "$OUT" ]; then mkdir -p "`dirname "$OUT"`" 2>/dev/null fi echo $VAR >> $OUT # the important step here is to make sure that the folder for the file exists # and create it if it does not. It will remain silent if the folder exists. $sh out hello/how/are/you/file.out geee: ~/src/bash/moo $sh out hello/how/are/you/file.out geee: ~/src/bash/moo $sh out another/file/lol.hmz geee: ~/src/bash/moo $find . . ./out ./another ./another/file ./another/file/lol.hmz ./hello ./hello/how ./hello/how/are ./hello/how/are/you ./hello/how/are/you/file.out geee: ~/src/bash/moo $cat ./hello/how/are/you/file.out something to put in a file something to put in a file geee: ~/src/bash/moo $cat ./another/file/lol.hmz something to put in a file 如果文件夹在名称中有空格,则需要转义“for dirname”. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |