linux - 创建空文件,提示文件是否已存在?

标签 linux bash shell scripting

如何使用脚本询问我是否要覆盖该文件(如果该文件已存在)来触摸或创建新文件?脚本应该是什么样子?

最佳答案

可能是这样的:

#!/bin/bash

if [ -e "$1" ]; then
    echo "Want to overwrite? (Y/n)"
    read ANSWER
    [ "$ANSWER" = "Y" ] && touch "$1" || echo "Ok"
else
    echo "Creating"
    touch "$1"
fi

将其称为:./script <filename>

关于linux - 创建空文件,提示文件是否已存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31717408/

相关文章:

linux - Redhat Linux操作系统打补丁悄无声息触发软件包升级?

regex - Ubuntu、Bash、awk、正则表达式 -> 在文本文件中找到所有 [numbers] 并打印它们

python - 如何禁用 Cython?

bash - 在 shell 中使用docker命令显式使用别名

c - 将 shell "*"实现到 C 程序中

python - 如何在终端中的 Jupyter 上运行 python 脚本?

bash gnu 并行帮助

bash - 如何保护 mongodump 从远程计算机进行备份?

bash - 带有参数扩展的 Shell 通配符

shell - 在shell脚本中打印文件时间